问题
I'm trying to use proprietary DLL in my Go project.
One of DLL's method description looks like this:
BYTE* Init(const BYTE* path, int id);
in my test Go project I'm doing something like:
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)
)
}
Library gets called, there is an error message "path isn't exist", but I think that type of my path is not right. That's why library can't see the folder.
Maybe there is a better way of doing this? Maybe it's a bad case of Go usage and I should find some package or even language?
回答1:
Your path likely needs to be NUL terminated:
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)
)
}
来源:https://stackoverflow.com/questions/47979699/using-dll-in-windows-go