DLL中的实例化类在QT中的LoadLibrary中引发127错误

时间:2012-10-10 15:07:53

标签: c++ qt dll eclipse-cdt

我正在尝试使用LoadLibrary在qt中加载DLL(仅用于测试而不是QLibrary),dll是在eclipse CDT中编译的,但奇怪的是当我尝试在Dll中的任何函数中实例化任何类时,LoadLibrary失败错误127(使用GetLastError),但如果我没有实例化任何LoadLibrary是成功的,为什么会这样呢?我的代码是下一个,标题和你的实现:

标题

#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();
    }

1 个答案:

答案 0 :(得分:1)

您正在评论的代码似乎会创建系统无法解析的依赖关系。例如,使用new的代码要求将新的系统实现加载到进程中,或者可以定位和加载提供它的DLL。如果不能,则LoadLibrary调用将失败。

找出遗漏的依赖关系的方法:

  • 在调试程序中运行程序,该调试程序记录所有已加载和卸载的模块。
  • 使用依赖性walker程序。 (现在很少见,SxS使这个过程变得复杂。)
  • 在程序运行时运行Process Monitor之类的操作。这将准确显示正在搜索的DLL和位置。