golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.98k stars 17.67k forks source link

Use syscall.NewCallback to generate errors #57573

Closed tianming-lu closed 1 year ago

tianming-lu commented 1 year ago

What version of Go are you using (go version)?

$ go version
go version go1.19.4 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=auto
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\LTM\AppData\Local\go-build
set GOENV=C:\Users\LTM\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\LTM\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\LTM\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.19.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\LTM\AppData\Local\Temp\go-build2846873693=/tmp/go-build -gno-record-gcc-switches

What did you do?

I wrote the following code

func event_callback(U uintptr, ip uintptr, port int, data uintptr, len int, conn_type int) int{
}
callback := syscall.NewCallback(event_callback)

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?

seankhliao commented 1 year ago

please include a complete reproducer so we can understand what you're trying to do.

twgh commented 1 year ago

是go调用c++的dll 吗, 我开源的一个GUI库 xcgui 就是调用c++的dll, 有1700个dll函数我都封装了, 很多数据类型都有涉及到, 我或许可以帮你解决, 不过还需要你提供更多信息

tianming-lu commented 1 year ago

是这样的,我自己的一个项目,使用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: @.***>

ianlancetaylor commented 1 year ago

Please show us a small complete program that demonstrates the problem. Not just a discussion of the problem, but a complete program. Thanks.

tianming-lu commented 1 year ago

今天又研究了一下,问题已经解决了