3
我刚刚了解了dll的。我在Qt中试过。首先,我发布了与dll相关的文件。Qt -Dll相关问题
首先DLL - 亲文件
TEMPLATE = lib
SOURCES += \
check.cpp
HEADERS += \
check.h
这一个是DLL-头文件 “check.h”
#ifndef CHECK_H
#define CHECK_H
#include <iostream>
extern "C++" __declspec(dllexport) std::string check();
#endif // CHECK_H
这一个是DLL - 源文件 “check.cpp”
#include <iostream>
#include "check.h"
extern "C++" __declspec(dllexport) std::string check()
{
return "dll applied";
}
我编译了上面的工程文件,得到了dll。该dll的名称是“dll.dll”
现在是主文件。在这里我尝试通过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();
}
但是当我运行上面的应用程序,我得到了“解决没有发生”。这意味着,函数指针变为NULL。我不知道哪部分是错的。来人帮帮我?
你是说我必须把__declspec(dllexport)放在“check.h”中,当我用“main”文件编译它时。 – prabhakaran 2011-05-27 14:28:21
@prabhakaran:我在你的真实代码中澄清了你需要用DLL魔术做什么。 – rubenvb 2011-05-27 14:57:14
我得到这个错误“invalid or corrupt file:can not read at 0x2C0” – prabhakaran 2011-05-28 06:26:18