Qt - Dll related issue

I just learn about the DLL. I tried this in Qt. First I send dll related files.

First dll file - pro

TEMPLATE = lib

SOURCES += \
    check.cpp

HEADERS += \
    check.h

This file is dll-header "check.h"

#ifndef CHECK_H
#define CHECK_H

#include <iostream>

extern "C++" __declspec(dllexport) std::string check();

#endif // CHECK_H

This is the dll source file "check.cpp"

#include <iostream>
#include "check.h"

extern "C++" __declspec(dllexport) std::string check()
{
    return "dll applied";
}

I compiled the above project file and got a dll. The dll name is "dll.dll"

Now comes the main file. Here I tried to access the "check" function via dll.

#include "check.h"
#include <iostream>
#include "MyMessageBox.h"
#include <QApplication>
#include <QLibrary>
#include <QMessageBox>

typedef std::string (*CheckType) (void);

class MyMessageBox:public QMessageBox
{
public:
    MyMessageBox(std::string message,QWidget*parent=0):
       QMessageBox(QMessageBox::NoIcon,QString("ErrorMessage"),QString(message.c_str()),QMessageBox::Ok,parent,Qt::Widget)
    {
    }
};

int main(int argc,char * argv[])
{
    QApplication app(argc,argv);
    CheckType myCheck;
    QLibrary myLib("dll");
    myLib.load();

    bool ok = myLib.load();
    if(ok)
    {
        MyMessageBox mm("Load is done");
        mm.exec();
    }
    ok = myLib.isLoaded();
    if(ok)
    {
        MyMessageBox mm("Loaded");
        mm.exec();
    }
    myCheck = (CheckType) (myLib.resolve("check"));
    if(!myCheck)
    {
        MyMessageBox m0("Resolving isn't happened");
        m0.exec();
    }
    std::string result = myCheck();

    MyMessageBox mm(result);
    mm.exec();
    return app.exec();
}

But when I launched the specified application, I got: "Permission did not happen." This means that the function pointer has become NULL. I don’t know which part is wrong. Does anyone help me?

+3
source share
1 answer

DLL __declspec (dllimport). :

// Windows DLL magic
#if defined(USE_DLL)
# if defined(BUILD_DLL)
#  define DLL_EXPORT  __declspec(dllexport)
# else // BUILD_DLL
#  define DLL_EXPORT  __declspec(dllimport)
# endif // BUILD_DLL
#else // USE_DLL
# define DLL_EXPORT
#endif // USE_DLL

__declspec(dllexport) DLL_EXPORT , .

DEFINES += USE_DLL BUILD_DLL

dll ( DLL, !)

DEFINES += USE_DLL

DLL-. , .

+6

All Articles