在Windows Go中使用DLL [英] Using DLL in windows Go

查看:102
本文介绍了在Windows Go中使用DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Go项目中使用专有的DLL。

I'm trying to use proprietary DLL in my Go project.

其中一个DLL的方法描述如下:

One of DLL's method description looks like this:

BYTE*   Init(const BYTE* path, int id);

在我的测试Go项目中,我正在做类似的事情:

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.

也许有更好的方法呢?也许这是Go使用情况的坏情况,我应该找到一些软件包甚至语言?

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?

推荐答案

您的路径可能需要为NUL终止:

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)
  )
}

这篇关于在Windows Go中使用DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆