如何解决如何制作以go结构或接口作为参数的Go dll函数
我正在使用go和dll,需要创建一个go dll,该dll导出一个接受go结构的函数。
//test.go
package main
import "fmt"
//export TestFunc
func TestFunc(s TestStruct) {
fmt.Println(s.a)
}
func main() {}
//teststruct.go
package main
type TestStruct struct {
a int
}
并使用
进行构建go build -buildmode=c-shared -o test.dll .\test.go .\teststruct.go
我得到了一个完美的dll文件。 现在,当我使用此Go代码进行调用
//testcall.go
package main
import (
"log"
"syscall"
"unsafe"
)
func main() {
l,e := syscall.LoadLibrary("test.dll")
if e != nil {
log.Fatal(e)
}
p,e := syscall.GetProcAddress(l,"TestFunc")
if e != nil {
log.Fatal(e)
}
_,_,_ = syscall.Syscall(p,1,uintptr(unsafe.Pointer(&TestStruct{
a: 1002,})),0)
}
并运行
go run .\testcall.go .\teststruct.go
我收到一个致命错误:
找不到指定的过程。
当我在C语言中执行相同操作时,它工作正常:
//test.c
#include <stdio.h>
#include "struct.h"
__declspec(dllexport) void TestFunc(struct TestStruct s) {
printf("%d\n",s.a);
}
//testcall.c
#include <windows.h>
#include "struct.h"
int main() {
HINSTANCE l = LoadLibrary("test.dll");
FARPROC p = GetProcAddress(l,"TestFunc");
struct TestStruct s;
s.a = 1002;
p(s);
}
//teststruct.h
struct TestStruct {
int a;
};
并使用:
gcc -c test.c
gcc -shared -o test.dll test.o
gcc .\testcall.c
我没有收到任何错误,但它给了我
1002
此外,如果我在test.go文件中使用import "C"
,它将给我
无法识别的Go类型TestStruct
这应该发生,因为C不知道该类型是什么。那么有什么方法可以将go结构用作参数,为什么它会给我一个procedure not found
错误?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。