Passing CString to Cgo function

i am getting error on the following function :

func write_file_dll(input string) {
	cStr := unsafe.Pointer(C.CString(input))
	defer C.free(cStr)
	C.face(cStr)
}

error is :cannot use cStr (type unsafe.Pointer) as type *_Ctype_char in argument to _Cfunc_face

but when i remove unsafe.Pointer() , error changes to :
cannot use cStr (type *_Ctype_char) as type unsafe.Pointer in argument to func literal

my header is :

#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

extern void face(const char *da);

it connects to external so file

You do not need to make cStr a unsafe.Pointer.

Try:

func write_file_dll(input string) {
	cStr := C.CString(input)
	defer C.free(unsafe.Pointer(cStr))
	C.face(cStr)
}

You will also need a wrapper C side to cast the char* to const char*

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.