#include <iostream> #include <windows.h> #include<string.h> //extern int OutPutQrCode(int version, int width, const char* outfile, unsigned char* data) typedef int(_cdecl *OutPutQrCode)(int, int,const char*, unsigned char*); typedef struct { int version; ///< version of the symbol int width; ///< width of the symbol unsigned char* data; ///< symbol data } QRcode; int main() { std::cout << "Hello World!\n"; HMODULE hMod = LoadLibrary("LLQrencode.dll"); if (hMod != NULL) { /* 如果加載成功,則可通過GetProcAddress函數獲取DLL中需要調用的函數的地址。 獲取成功,sum指針不爲空。 */ OutPutQrCode getCodeImg = (OutPutQrCode)GetProcAddress(hMod, "OutPutQrCode"); GetProcAddress(hMod, "OutPutQrCode"); if (getCodeImg != NULL) { getCodeImg(3, 130, "e:\\a.png", (unsigned char*)"www.baidu.com"); } FreeLibrary(hMod); /*在完成調用功能後,不在需要DLL支持,則可以通過FreeLibrary函數釋放DLL。*/ } }
c++調用動態dll庫
首先把需要調用的動態庫dll和它依賴的對象都要放入到運行目錄,debug環境就是debug目錄下了。
然後就寫代碼:
構建一個方法指針:typedef int(_cdecl *OutPutQrCode)(int, int,const char*, unsigned char*);
要和需要使用的接口參數相同。
使用LoadLibrary加載庫
尋找庫裏的接口並轉換
OutPutQrCode getCodeImg = (OutPutQrCode)GetProcAddress(hMod, "OutPutQrCode");
調用:
getCodeImg(3, 130, "e:\\a.png", (unsigned char*)"www.baidu.com");
釋放:
FreeLibrary(hMod);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.