• Qt调用dll


    直接上代码

    extern "C"{
    DLLSHARED_EXPORT Dll* getDllObject(); //获取类Dll的对象
    DLLSHARED_EXPORT void releseDllObject(Dll*); //获取类Dll的对象
    
    DLLSHARED_EXPORT void helloWorld();
    DLLSHARED_EXPORT int add(int a,int b);
    }
    
    #include "mainwindow.h"
    #include <QApplication>
    #include <iostream>
    #include <QLibrary>
    #include <windows.h>
    #include "dll.h"  //头文件还是需要加的,否则无法解析Dll类
    
    typedef Dll* (*CreatDll)();//定义函数指针,获取类Dll对象;
    typedef bool (*ReleseDll)(Dll*);
    typedef void (*fun)();
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        QLibrary mylib("dll.dll");   //声明所用到的dll文件
        //判断是否正确加载
        if (mylib.load())
        {
            std::cout << "DLL  loaded!"<<std::endl;
            CreatDll creatDll = (CreatDll)mylib.resolve("getDllObject");
            ReleseDll decDll=(ReleseDll)mylib.resolve ("releseDllObject");
            fun hello=(fun)mylib.resolve ("helloWorld");
            if(hello)hello();
            if(creatDll&&decDll)
            {
                Dll *testDll = creatDll();
                testDll->GetStrAdd ("abc","ABD");
                testDll->Print ();
                decDll(testDll);
            }
        }
        //加载失败
        else
            std::cout << "DLL is not loaded!"<<std::endl;
    
        mylib.unload ();
    
        return a.exec();
    }
    

    工程文件加入

    #LIBS += "D:/qt_work/build-dllTest-Desktop_Qt_5_11_1_MinGW_32bit-Debug/debug/dll.dll" 或者
    LIBS += D:qt_workuild-dllTest-Desktop_Qt_5_11_1_MinGW_32bit-Debugdebugdll.dll
    

    否则会报错:

    D:qt_workuild-dllTest-Desktop_Qt_5_11_1_MinGW_32bit-Debugdebugmain.o:-1: In function
      `Z5qMainiPPc':
    D:qt_workdllTestmain.cpp:30: error: undefined reference to
      `_imp___ZN3Dll9GetStrAddENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES5_'
    D:qt_workdllTestmain.cpp:31: error: undefined reference to `_imp___ZN3Dll5PrintEv'
    
  • 相关阅读:
    Poj 2104 K-th Number(主席树&&整体二分)
    Bzoj 3262: 陌上花开(CDQ分治)
    Bzoj 2683: 简单题(CDQ分治)
    ZOJ2314 Reactor Cooling(无源汇上下界可行流)
    Cogs 12. 运输问题2(有上下界的有源汇最大流)
    Cogs 461. [网络流24题] 餐巾(费用流)
    Codevs 1227 方格取数 2(费用流)
    Cogs 13. 运输问题4(费用流)
    Poj 2195 Going Home(费用流)
    开学第二测
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11709331.html
Copyright © 2020-2023  润新知