Closed tianming-lu closed 1 year ago
please include a complete reproducer so we can understand what you're trying to do.
是go调用c++的dll 吗, 我开源的一个GUI库 xcgui 就是调用c++的dll, 有1700个dll函数我都封装了, 很多数据类型都有涉及到, 我或许可以帮你解决, 不过还需要你提供更多信息
是这样的,我自己的一个项目,使用C++开发核心功能,编译为动态库,可以编译为linux下的so或者windows下的dll文件。 我打算用golang用来编写业务代码,运行过程中调用so或者dll提供的接口,动态库中需要若干回调函数,通过回调函数从C++调用golang代码。 linux下我在golang中使用 cgo实现调用so文件,经过测试功能正常,没有发现问题。
但在windows下golang实现方式不同,并发现C++回调golang时没有达到预期结果,表现为C++回调golang传递参数时固定得某个int类型参数错误,在golang中获取到得值像是一个指针值,并不是我从C++中要传递给golang得值。 这些回调函数我都使用syscall.NewCallback生成,大部分回调函数都正常的,每次总是在固定的地方产生错误
golang中函数定义如下,如果dll采用debug编译,则每次conn_type错误,其他正确;如果dll采用release编译,则每次data_len错误,其他正确 func event_callback(U *BaseUserCall, ip uintptr, port int, data uintptr, data_len int, conn_type int) int { fmt.Println(U, ip, port, data, data_len, conn_type) return 0 }
C++中回调函数定义如下 typedef int (__STDCALL func_event_callback)(void user, const char ip, int port, const char data, int len, int type);
我不明白这是为什么,我对golang并不是很了解
------------------ 原始邮件 ------------------ 发件人: "golang/go" @.>; 发送时间: 2023年1月25日(星期三) 下午2:57 @.>; @.**@.>; 主题: Re: [golang/go] Use syscall.NewCallback to generate errors (Issue #57573)
是go调用c++的dll 吗, 我开源的一个GUI库 xcgui 就是调用c++的dll, 有1700个dll函数我都封装了, 很多数据类型都有涉及到, 我或许可以帮你解决, 不过还需要你提供更多信息
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>
Please show us a small complete program that demonstrates the problem. Not just a discussion of the problem, but a complete program. Thanks.
今天又研究了一下,问题已经解决了
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I wrote the following code
After I pass the callback to the dynamic library, I call back the callback in the dynamic library. At this time, an error occurs in the go code. I print and find that the fifth parameter is not the length value passed in the dynamic library, but a pointer value. Other parameter values are correct. I printed the value of len in the dynamic library. I can be sure that there is no error in the dynamic library. I don't know how this error occurred
What did you expect to see?
I hope to get the parameter value correctly in the go code
What did you see instead?