将指针从 go 传递到 c-dll

Passing pointer from go to c-dll

我正在尝试将双指针传递给由 C 在制作 dll 包装程序包时制作的 dll。

下面是C代码。

int getData(uint32_t ** buffer, uint32* sizeofBuffer) {
  //...
  *sizeofBuffer = n;
  *buffer = new uint32_t[n];

  //do something 
   return 0; 
}

void releaseBuf(void* buffer) {
     delete buffer;
}

下面是go代码。我希望下面的代码进展顺利,但事实并非如此。

var (
   Handle =  syscall.LoadLibrary(`library\myDll.dll`)
   procGet,_ = syscall.GetProcAddress(Handle, "getData")
   procRelease,_ = syscall.GetProcAddress(Handle, "releaseBuf")
)
func main() {
   var bufferPtr *uint32
   var size uint32
   _,_,err := syscall.Syscall(proc,uintptr(2), uintptr(unsafe.Pointer(&bufferPtr )), uintptr(unsafe.Pointer(&size)),uintptr(0))
   if 0 != err {log.Fatal(syscall.Errno(err))}

var i uintptr = 0
for ; i<(uintptr)(size); i++ {
    log.Printf("%d", (**uint32)(uintptr(unsafe.Pointer(bufferPtr ))+uintptr(i)*unsafe.Sizeof(*bufferPtr )))
}

   _,_,err := syscall.Syscall(proc,uintptr(1), uintptr(unsafe.Pointer(bufferPtr)), uintptr(0), uintptr(0))
   if 0 != err {log.Fatal(syscall.Errno(err))}    
}

任何人都知道我错过了什么。

重新检查发现是我的错

将 for-routine 替换为以下

log.Printf("%d", *(*uint32)(unsaef.Pointer(uintptr(unsafe.Pointer(buffer))+i*unsafe.Sizeof(*buffer))))

:)