dll - 使用 dll api 时出现意外错误地址 0x0

标签 dll go

我正在尝试在go语言中使用一些第三方dll。但面临着巨大的困难。我更喜欢纯 Go 实现,但这不是一个选择。

这是我的示例程序

import (
"fmt"
"github.com/andlabs/dl"
)

type GetObject func(string, string) int

func main() {
    gro := new(GetObject)
    d, err := dl.Open("/home/vaishnavi/lib/libVsphere.so", dl.Lazy)
    if err!=nil{
        panic(err)
    }
    fmt.Println("DLL Loaded::", d)

    s, err := d.Symbol("GetSum")
    if err != nil {
        panic(err)
    }

    gro = (*GetObject)(s)

    p := (*gro)("1","2")
    fmt.Println("Got returned result::", p)
    fmt.Println("Converted::", gro)
    fmt.Println("Loaded at::", s)
    if s == nil {
        fmt.Println("Error loading from the dll")
    }

}

运行此程序时,我收到以下错误

DLL Loaded:: 15028848
unexpected fault address 0x0
fatal error: fault
[signal 0xb code=0x80 addr=0x0 pc=0x401923]

goroutine 1 [running]:
runtime.gothrow(0x4dcef0, 0x5)
    /usr/local/go/src/runtime/panic.go:503 +0x8e fp=0xc20805fda0   sp=0xc20805fd88
runtime.sigpanic()
/usr/local/go/src/runtime/sigpanic_unix.go:29 +0x265 fp=0xc20805fdf0 sp=0xc20805fda0
main.main()
        /home/vaishnavi/ESXI_VHBS/src/main/main.go:25 +0x343     fp=0xc20805ff98 sp=0xc20805fdf0
runtime.main()
        /usr/local/go/src/runtime/proc.go:63 +0xf3 fp=0xc20805ffe0     sp=0xc20805ff98
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1 fp=0xc20805ffe8 sp=0xc20805ffe0

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
Error: process exited with code 2.

任何帮助将不胜感激。

最佳答案

package documentation说:

Symbol looks up the given named symbol in the Module. Note that the value of Symbol can be nil, so checking symbol for nil will not indicate an error; checking err for nil is.

(强调已添加。)

来自package's source code似乎只有两个原因,Symbol 和 err 都可以为 nil。该符号要么确实为空,要么库中不存在。确保该符号确实存在,并且您没有拼错名称。通常,您可以使用 readelf -Wsnm -g 列出 .so 文件中的所有符号。

关于dll - 使用 dll api 时出现意外错误地址 0x0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31543415/

相关文章:

windows - 对从 DLL 或 SO 导出的符号的写入权限

C++ 将命令行参数传递给 dll

c# - 如何获取 dll 函数的 id(内存地址)?

postgresql - 如何在postgresql中跟踪sql查询

assembly - Go 没有链接我的程序集 : undefined external function

c# - 从 C# 程序调用 c++ DLL,未处理的异常

java - 从 Java 调用扫描仪 Win32 DLL

go - Go 语言中的副作用

go - 使用模块时在工作区中制作 VSCode 安装包

multithreading - golang线程模型比较