Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does golang create a buffer to pass to a C dll function?

Tags:

go

I need to invoke a C API from golang, which is from a dll. The problem is the C func need a buffer, how to create the buffer in golang, then i can pass the buffer to the C func?

void fooGetString(char* buffer, int buffer length)
like image 411
bzhu Avatar asked Apr 21 '12 15:04

bzhu


1 Answers

Something like this should work:

s := make([]byte, 256)
C.fooGetString((*C.char)(unsafe.Pointer(&s[0])), C.int(len(s)))
like image 96
Alec Thomas Avatar answered Nov 09 '22 16:11

Alec Thomas