• c++调用动态dll库


    首先把需要调用的动态库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);
  • 相关阅读:
    网站图片轮播效果
    图片处理类
    字符串处理帮助类
    css3高级选择器
    JQuery选择器大全
    ASCII码表
    jQuery选择器大全
    OpenFileDialog无法弹出的解决方法
    socket学习目录
    ps-抠图
  • 原文地址:https://www.cnblogs.com/HelloQLQ/p/16364842.html
Copyright © 2020-2023  润新知