英文:
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
中使用syscall调用这两个函数时:
hd:=syscall.NewLazyDLL(dll_path)
proc:=hd.NewProc(dll_func_name)
ret:=proc.Call()
我们发现:
FnRetInt
正常工作,但FnRetString
没有。proc.Call
的返回类型是uintptr
,我们如何将其更改为我们想要的类型(例如:char*或string)?
英文:
We designed C/C++ DLL just like this:
WIN32_DLL_EXPORT int FnRetInt(int i)
{
....
return 32 ;
}
WIN32_DLL_EXPORT char* FnRetString()
{
return "THIS IS A TEST STRING" ;
}
when we invoke these two functions in Go
by using syscall:
hd:=syscall.NewLazyDLL(dll_path)
proc:=hd.NewProc(dll_func_name)
ret:=proc.Call()
we found:
FnRetInt
worked ok, but FnRetString
didn't. proc.Call
return type is uintptr
, how can we change it to the type we wanted (for exsample: char* or string)?
答案1
得分: 2
一个uintptr
是Go语言中表示指针的类型。你可以使用unsafe
包将其转换为unsafe.Pointer
,然后可以将unsafe.Pointer
转换为任何Go指针类型。所以你可以像这样做:
str := (*uint8)(unsafe.Pointer(ret))
来获取一个*uint8
类型的指针。
英文:
A uintptr
is a Go type that represents a pointer. You can use the unsafe
package and convert it to unsafe.Pointer
, and then you can convert an unsafe.Pointer
into any Go pointer type. So you could do something like
str := (*uint8)(unsafe.Pointer(ret))
to get a *uint8
back.
答案2
得分: 1
看一下syscall.Getwd的Windows实现http://code.google.com/p/go/source/browse/src/pkg/syscall/syscall_windows.go#323。它与你的问题不同:
- 它将缓冲区传递给dll,而不是从dll接收它;
- 数据是uint16s(Microsoft WCHARs),而不是uint8s;
- GetCurrentDirectory告诉我们结果字符串的长度,而你的示例可能希望你在末尾搜索0;
但应该给你足够的线索。
Alex
英文:
Look at syscall.Getwd windows implementation http://code.google.com/p/go/source/browse/src/pkg/syscall/syscall_windows.go#323. It is different from your problem:
- it passes buffer to the dll, instead of receiving it from dll;
- the data is uint16s (Microsoft WCHARs), instead of uint8s;
- GetCurrentDirectory tells us how long resulting string is going to be, while your example, probably, expects you to search for 0 at the end;
But should give you enough clues.
Alex
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论