這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
//調用方式
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, 0, 0, 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 ", major, ".", minor, " (Build ", build, ")\n")}
//調用c++ dll
使用時將go產生的exe和c++產生的dll放在同一目錄下即可.
C++:
//vs 2010,c++嵌入"::MessageBox(NULL, str, _T("..."), MB_OK);"用以調試時會引起程式崩潰.
//dlltest.hDLL_API int __stdcall test1(LPTSTR s);DLL_API int __stdcall test2(int *m);//dlltest.cppint __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 int32m = 10p := &mfmt.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傳入傳出資料時,要小心底層資料類型的一致性問題.