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