【发布时间】:2018-05-22 09:49:59
【问题描述】:
我想用 go-code 创建一个库并在 C# winforms 项目中使用它。
错误滚动到底部。
设置
- 去 1.10.2
- tdm-gcc-5.1.0-3
- Windows 10 / x64
- 调用项目
exprt
我的尝试
我创建了一个在工作目录中创建文件的最小 go-tool:
package main
import (
"os"
"C"
)
func main() {
// nothing here
}
//export Test
func Test() {
os.OpenFile("created_file.txt", os.O_RDONLY|os.O_CREATE, 0666);
}
接下来的步骤来自Building a dll with Go 1.7。
然后,我使用以下命令编译为c-archive:go build -buildmode=c-archive,这给了我exprt.a 和exprt.h。
之后,我创建了一个名为 goDLL.c 的文件(如上面的链接中的 1:1)并插入了以下代码:
#include <stdio.h>
#include "exprt.h"
// force gcc to link in go runtime (may be a better solution than this)
void dummy() {
Test();
}
int main() {
}
最后我运行了这个命令来创建我的最终 dll:gcc -shared -pthread -o goDLL.dll goDLL.c exprt.a -lWinMM -lntdll -lWS2_32
这给了我“goDLL.dll”。
我的问题
在 C# 中,我创建了一个带有 1 个按钮的 winforms 项目,该按钮调用此声明的函数(将 dll 复制到调试文件夹):
[DllImport("goDLL.dll")]
private static extern void Test();
错误
System.BadImageFormatException: "An attempt was made to load a program with an incorrect format. (HRESULT: 0x8007000B)"
很抱歉这么大的文字,但这是我能想到的最小的测试。
感谢这里的每一个帮助。
【问题讨论】:
-
为什么引用的外部函数的名字是
File而不是Test?您得到的错误与 32 位 WOW 与 64 位图像有关,可能是由于-lWS2_32编译器标志。两者必须具有相同的位数。 -
@dlatikay 只是一个错字。在我的第一次测试中,两者都被称为
File()。编辑了问题。 -
我只是不明白
WS2_32对应的 64 位..
标签: c# go compilation shared-libraries shared