首先把需要调用的动态库dll和它依赖的对象都要放入到运行目录,debug环境就是debug目录下了。
然后就写代码:
#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。*/
    }
   
}
构建一个方法指针: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);