dsaaqdz6223
2018-07-12 08:18
浏览 19
已采纳

将指针从转到c-dll

I'm trying to pass a double pointer to a dll which was made by C on making dll wrapper pacakge.

bellow is C code.

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

  //do something 
   return 0; 
}

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

below is go code. I hope below code is going well but it's not.

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

anyone knows what I missed out.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwan5381 2018-07-12 12:01
    已采纳

    re-checked and found my fault.

    replace for-routine with below

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

    :)

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题