首页 > 应用开发 > IT综合 > Go语言调用dll动态链接库的简单例子
1、 C写的简单例子。
/* File : example2.c */
/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
int g;
g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
}
2、 编译dll包。
gcc -c -fpic example2.c
gcc -shared example2.o -o example2.dll
3、 Go的访问代码。
// hello.go
package main
import (
"syscall"
)func callDll(){
dll32 := syscall.NewLazyDLL("example2.dll")
println("call dll:",dll32.Name)
g:=dll32.NewProc("gcd")
ret, _, _ :=g.Call(uintptr(4),uintptr(8))
println()
println("get the result:",ret)
}func main() {
callDll()
}
4、 运行例子。
go build hello.go
把example2.dll拷贝到hello.go 一样的目录下。然后执行hello.exe.
于此类似的,C++代码也可以通过这样的方式被Go所调用。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/88031/viewspace-1058687/ ,如需转载,请注明出处,否则将追究法律责任。