JC Ahn
JC Ahn

Reputation: 366

Passing pointer from go to 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.

Upvotes: 2

Views: 130

Answers (1)

JC Ahn
JC Ahn

Reputation: 366

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

:)

Upvotes: 1

Related Questions