我正在尝试在我的 Go 项目中使用专有 DLL。
其中一个 DLL 的方法描述如下所示:
BYTE* Init(const BYTE* path, int id);
在我的测试 Go 项目中,我正在做类似的事情:
import (
"golang.org/x/sys/windows"
)
var (
lib = windows.MustLoadDLL("dllname.dll")
init = lib.MustFindProc("Init")
)
func main() {
path := "some"
bytePath = []byte(path)
init.Call(
uintptr(unsafe.Pointer(&bytePath)),
uintptr(9)
)
}
库被调用,出现错误消息“路径不存在”,但我认为我的路径类型不正确。这就是图书馆看不到该文件夹的原因。
也许有更好的方法?也许这是 Go 用法的一个糟糕案例,我应该找到一些包甚至语言?
最佳答案
您的路径可能需要以 NUL 终止:
import (
"golang.org/x/sys/windows"
)
var (
lib = windows.MustLoadDLL("dllname.dll")
init = lib.MustFindProc("Init")
)
func main() {
path := "some"
bytePath = []byte(path + "\x00")
init.Call(
uintptr(unsafe.Pointer(&bytePath[0])),
uintptr(9)
)
}
关于winapi - 在 Windows Go 中使用 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47979699/