winapi - 在 Windows Go 中使用 DLL

标签 winapi go dll

我正在尝试在我的 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/

相关文章:

c++ - WinAPI 窗口立即关闭

c# - 点网 : Static variables in DLL

winapi - 试图通过仪表板更新 hudson ,但现在它不起作用(Windows)

c++ - 使用CreateProcess()和CreatePipe()从cmd.exe重定向I/O

json - 在 golang 中解码 json

go - 如何在Elasticsearch中更新之前检查属性?

java - WinScard 智能卡 dll 32 位 64 位兼容性

c++ - 在 pdfium 中创建一个 dll

winapi - 为什么 WinAPI 与 "normal"C 如此不同?

google-app-engine - 通过指定 GAE 数据存储中的值列表来查询实体