英文:
Golang: call Windows DLL functions
问题
我正在编写一个调用Windows DLL的Go应用程序。该DLL是使用MSVC编写的,并使用__declspec(dllexport)进行extern "C"导出。
我的Go应用程序的顶部如下所示:
//#cgo CFLAGS: -IC:/Repos/Module/include
//#cgo LDFLAGS: -L. C:/Repos/Module/go/bin/MyModule.dll
//#include <MyModule.h>
import "C"
我像这样调用模块中的函数:
nRet := C.moduleImpl_len()
问题是,当我尝试运行此应用程序时,出现以下错误:
C:\Users\MINDO~1\AppData\Local\Temp\go-build836751819\mod\modimpl\_obj\modimpl.cgo2.o: In function `_cgo_e2aaf076ab69_Cfunc_moduleImpl_len':
C:/Repos/Module/go/src/mod/modimpl/modimpl.go:90: undefined reference to `moduleImpl_len'
我使用DUMPBIN查看了导出的DLL符号,它确实显示为"moduleImpl_len"。这也是在MyModule.h中定义的符号。
有没有建议如何在不使用syscall的情况下使符号链接起来?
英文:
I am writing a Go app that calls into a Windows DLL. The DLL was written with MSVC and has extern "C" exports using __declspec(dllexport).
The top of my Go app is as follows:
//#cgo CFLAGS: -IC:/Repos/Module/include
//#cgo LDFLAGS: -L. C:/Repos/Module/go/bin/MyModule.dll
//#include <MyModule.h>
import "C"
I'm calling functions from the module like so:
nRet := C.moduleImpl_len()
The problem is that when I try to run this app, I get the following error:
C:\Users\MINDO~1\AppData\Local\Temp\go-build836751819\mod\modimpl\_obj\modimpl.cgo2.o: In function `_cgo_e2aaf076ab69_Cfunc_moduleImpl_len':
C:/Repos/Module/go/src/mod/modimpl/modimpl.go:90: undefined reference to `moduleImpl_len'
I looked at the DLL exported symbols with DUMPBIN and it showed exactly "moduleImpl_len" as the exported symbol. This is also the symbol as defined in MyModule.h.
Any suggestions on how I can get the symbols to link without having to go the syscall route?
答案1
得分: 2
如果你想链接到所需/想要的dll,你需要在LDFLAGS中添加"-lMyModule"。
英文:
If you're trying to link against the dll you need/want "-lMyModule" in the LDFLAGS? https://stackoverflow.com/a/15853231/32453
答案2
得分: 0
上述代码将在编译时链接动态链接库(DLL),在某些情况下可能是可取的。
要在运行时链接,需要使用一个syscall包。我曾经在Windows上需要这样做,这是我找到的第一个简单快速的示例,帮助我第一次尝试。
英文:
The above will link the DLL in at compile-time which can be desirable in some situations.
In order to link at runtime, one would need to use a syscall package. I was needing to do this on Windows once and this was the first easy and quick example I found to help me try doing it the first time
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论