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

Instantiate class in DLL throws a 127 error in LoadLibrary in QT

本文关键字:错误 LoadLibrary 实例化 DLL QT      更新时间:2023-10-16

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

页眉:

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

似乎您注释掉的代码创建了系统无法解析的依赖项。 例如,使用 new 的代码要求 new 的系统实现已加载到进程中,或者可以找到并加载提供它的 DLL。 如果不能,则加载库调用将失败。

找出缺少的依赖项的方法:

  • 在记录所有已加载和卸载模块的调试器中运行程序。
  • 使用依赖步行者程序。 (现在很少见,因为 SxS 使该过程复杂化。
  • 在程序运行时运行类似进程监视器的内容。 这将准确显示正在搜索的 DLL 以及搜索的位置。