golang調用dll

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

//調用方式

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傳入傳出資料時,要小心底層資料類型的一致性問題.



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.