[英]How to invoke an exported function from DLL written in C/C++ which return type is char* or string?
我们设计了C / C ++ DLL,如下所示:
WIN32_DLL_EXPORT int FnRetInt(int i)
{
....
return 32 ;
}
WIN32_DLL_EXPORT char* FnRetString()
{
return "THIS IS A TEST STRING" ;
}
当我们使用系统调用在Go
调用这两个函数时:
hd:=syscall.NewLazyDLL(dll_path)
proc:=hd.NewProc(dll_func_name)
ret:=proc.Call()
我们找到:
FnRetInt
运行正常,但FnRetString
没有。 proc.Call
返回类型是uintptr
,我们如何将其更改为我们想要的类型(例如:char *或string)?
uintptr
是表示指针的Go类型。 您可以使用unsafe
包并将其转换为unsafe.Pointer
,然后您可以将unsafe.Pointer
转换为任何Go指针类型。 所以你可以做点什么
str := (*uint8)(unsafe.Pointer(ret))
得到一个*uint8
。
查看syscall.Getwd windows实现http://code.google.com/p/go/source/browse/src/pkg/syscall/syscall_windows.go#323 。 它与您的问题不同:
但应该给你足够的线索。
亚历克斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.