Using DLL in windows Go

无人久伴 提交于 2020-01-06 07:12:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!