小编典典

在Windows Go中使用DLL

go

我正在尝试在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使用情况的坏案例,我应该找到一些软件包甚至语言?


阅读 217

收藏
2020-07-02

共1个答案

小编典典

您的路径可能需要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)
  )
}
2020-07-02