Go语言调用dll
1、Go语言调用dll
user32 := syscall.NewLazyDLL("imobiledevice.dll")
MessageBoxW := user32.NewProc("idevice_event_subscribe")
MessageBoxW.Call(uintptr(C.test), uintptr(s))
imobiledevice.dll
为调用的dll名字
idevice_event_subscribe
为dll内函数名。
通过MessageBoxW.Call
调用函数,需要把参数转指针类型.
2、传递参数
传递整数型参数
func IntPtr(n int) uintptr {
return uintptr(n)
}
传递字符串型参数
func StrPtr(s string) uintptr {
return uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(s)))
}
3、实例: Go调用dll
go代码:
package main
import (
"fmt"
"syscall"
"unsafe"
)
func main() {
test := syscall.NewLazyDLL("Win32Project1.dll")
add := test.NewProc("add")
outRand := make([]byte, 2)
a := 1
b := 2
sum, _, _ := add.Call(uintptr(a), uintptr(b), uintptr(unsafe.Pointer(&outRand[0])))
fmt.Printf("sum:%d\noutRand:%s\n", sum, outRand)
}
uintptr(a)
,uintptr(b)
,uintptr(unsafe.Pointer(&outRand[0]))
参数1、参数2、参数3:整数,整数,char*
c++代码(.cpp)
#include "Win32Project1.h"
#include "stdafx.h"
int _stdcall add(int a, int b,char* c) {
*c ='e' ;
*(c + 1) = 't';
return a + b;
}
c++代码(.h)
extern _declspec(dllexport) int _stdcall add(int a,int b);
c++代码(.def)
LIBRARY Win32Project1
EXPORTS
add @ 1
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!