描述:
调用一个.dll
C文件内容如下
int testfuc(u8 * a, u8 * b){
printf("%s",*a);
return 1;
}
其中.h文件如下
typedef unsigned char u8;
编译生成test.dll文件在.go文件中的调用如下
dll32 := syscall.NewLazyDLL(“test.dll”)
g := dll32.NewProc(“testfuc”)
ret, _, _ := g.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("abc"))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("asf"))))
fmt.Println("get the result:", ret)
运行报错, Exception 0xc0000005.....
感觉传入的指针类型有问题,或者有正确的写法的求解答
这里要使用Cgo进行变量的转换
carg1 := C.CString("abc")
defer C.free(unsafe.Pointer(carg1))
carg2 := C.CString("asf")
defer C.free(unsafe.Pointer(carg2))
ret, , := g.Call(uintptr(unsafe.Pointer(carg1)), uintptr(unsafe.Pointer(carg2)))