我試圖加載使用調用LoadLibrary在QT一個DLL(僅用於測試,而不是QLibrary),該dll被編譯在Eclipse CDT的,但奇怪的是當我嘗試在DLL中的任何函數實例化任何類LoadLibrary失敗,錯誤127(使用GetLastError),但如果我沒有實例化任何LoadLibrary成功,爲什麼會發生這種情況?我的代碼是下一個,頭和您的實現:實例化類拋出的LoadLibrary一個127錯誤QT
標題:
#ifndef DESKTOPWINUTILS_H_
#define DESKTOPWINUTILS_H_
#ifdef __dll__
#define DESKTOPUTILSEXP __declspec(dllexport)
#else
#define DESKTOPUTILSEXP __declspec(dllimport)
#endif // __dll__
#include <iostream>
#include <stdio.h>
#include <string.h>
#include "ximage.h"
#include "IDesktopUtils.h"
class DesktopUtils:public IDesktopUtils{
public:
DesktopUtils();
~DesktopUtils(void);
char* sayHello();
};
extern "C" DESKTOPUTILSEXP bool create(IDesktopUtils**);
#endif /* DESKTOPWINUTILS_H_ */
實現:
#define __dll__
#include "DesktopUtils.h"
DesktopUtils::DesktopUtils(){
sayHello();
}
char* DesktopUtils::sayHello(){
return (char *)("I say Hello");
}
bool create(IDesktopUtils** desktoputils){
//DesktopUtils *desktoputils = new DesktopUtils();
if(!desktoputils)
return false;
*desktoputils =(IDesktopUtils*) new DesktopUtils; //if comment this the load is successful
return true;
}
在Qt的項目中,我用這個加載DLL,僅如果加載知道,我甚至不使用GetProcAddress的:
typedef char*(*createInst)(void);
HINSTANCE dll;
dll = LoadLibrary(TEXT("libDesktopWinUtils.dll"));
if(dll){
message.setText("library loaded");
message.exec();
}else{
char error[10];
itoa(GetLastError(),error,10);
message.setText(error);
message.exec();
}
請表明調用LoadLibrary和GetProcAddress的代碼。 –
謝謝阿德里安,那就是我只是在測試LoadLibrary。 –