//调用方式
func WindowVersion1() { h,err := syscall.LoadLibrary("kernel32.dll") if err != nil { fmt.Printf("Error: %s\n",err) return } defer syscall.FreeLibrary(h) proc,err := syscall.GetProcAddress(h,"GetVersion") if err != nil { fmt.Printf("Error: %s\n",err) return } r,_,_ := syscall.Syscall(uintptr(proc),0) major := byte(r) minor := uint8(r >> 8) build := uint16(r >> 16) print("windows version ",major,".",minor," (Build ",build,")\n") } func WindowVersion2() { dll32 := syscall.NewLazyDLL("kernel32.dll") g := dll32.NewProc("GetVersion") r,_ := g.Call() major := byte(r) minor := uint8(r >> 8) build := uint16(r >> 16) print("windows version ",")\n") }
//调用c++ dll
使用时将go生成的exe和c++生成的dll放在同一目录下即可.
C++:
//vs 2010,c++嵌入"::MessageBox(NULL,str,_T("..."),MB_OK);"用以调试时会引起程序崩溃.
//dlltest.h DLL_API int __stdcall test1(LPTSTR s); DLL_API int __stdcall test2(int *m); //dlltest.cpp int __stdcall test1(LPTSTR s) { CStringA sz(s); cout<<sz<<endl; return 0; } int __stdcall test2(int *m) { *m=*m*3+1; return 123; }
go:
func callDll1() { dll32 := syscall.NewLazyDLL("dlltest.dll") fmt.Println("dll name:",dll32.Name) g := dll32.NewProc("_test1@4") s := "1k中国fd" r1,_ := g.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(s)))) fmt.Println("callDll1结果:",r1) } func callDll2() { dll32 := syscall.NewLazyDLL("dlltest.dll") g := dll32.NewProc("_test2@4") var m int32 m = 10 p := &m fmt.Printf("%#v\n",p) r1,_ := g.Call(uintptr(unsafe.Pointer(p))) fmt.Println("callDll2结果:",(int32)(r1)) //获取修改后的m值 fmt.Println(*(*int32)(unsafe.Pointer(p))) //返回31 }
dll尝试返回*string 报错原因:golang的string是struct以len判断结束,c/c++的string是以'\0'表示结束.,底层数据结果不同导致.所以使用dll传入传出数据时,要小心底层数据类型的一致性问题.