我正在尝试将双指针传递到C制作的dll包装器pacakge上的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))}
}

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

最佳答案

重新检查,发现我的错。

用下面的命令替换常规

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

:)

07-24 20:23