【问题标题】:LoadLibrary is not working加载库不工作
【发布时间】:2013-12-17 12:35:32
【问题描述】:

我为我的项目创建了*.exe*.dll。 我已经提供了所有正确的路径和数据。 Myexe.cpp:

#include "stdafx.h"
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    HMODULE hInstLibrary = LoadLibrary(L("..\\Debug\\LoadDLL\\LoadDLL.dll"));// I have checked with complete path as well.
    if(hInstLibrary)
    {
        printf("Hello World");
    }
    return 0;
}

MyDLL.cpp:

#include "MyDLL.h"
#include <stdio.h>

    MyDLL::MyDLL(void)
    {

}


MyDLL::~MyDLL(void)
{
}

extern "C" __declspec(dllexport) void HelloWorld()
{
    printf("Hello DLL");
}

MyDLL.h:

#pragma once
class __declspec(dllexport) MyDLL
{
public:
    MyDLL(void);
    ~MyDLL(void);
};

extern "C" __declspec(dllexport) void HelloWorld();

我也尝试过提供complete path。但它仍然失败。 hInstLibrary 设置为 0x00000。我也试过Release mode,但问题仍然存在。 但是当我尝试过:

HMODULE hInstLibrary = LoadLibrary(_T("C:\\Windows\\System32\\aeinv.dll"));

它确实加载了DLL。所以,请帮助我哪里出错了。 DLL 得到正确构建,构建 DLL 绝对没有错误。那我为什么会遇到这个问题??

Debug是否需要设置。

【问题讨论】:

    标签: c++ visual-studio-2010 dll


    【解决方案1】:

    您需要致电GetLastError 找出问题所在。

    编辑:

    你得到了 0x7e,这意味着:

    ERROR_MOD_NOT_FOUND

    126 (0x7E)

    找不到指定的模块。

    你的路径是错误的。你需要解决这个问题。

    【讨论】:

    • 当我有wprintf(L"Format message failed with 0x%x\n", GetLastError());。它打印Format message failed with 0x7e
    • 当我将LPCSTR str = "..\\Debug\\LoadDLL\\LoadDLL.dll" 传递给strLoadLibrary 时,它确实起作用了。那么,到底哪里出了问题?
    • 当您的项目被编译为非 unicode 时,您可能使用了 unicode,尽管这会产生警告。如果您想稍后选择您的程序是否使用 unicode,您可以使用 _T 宏,如另一个答案中所述。
    【解决方案2】:

    使用DependencyWalker 检查您的可执行文件,并从那里检查它的配置文件。您将更好地了解 dll 加载失败的原因以及依赖关系树的外观。

    当您开始分析 (F7) 时,请确保选中“记录 LoadLibrary 函数调用”。

    您还可以查看Dynamic-Link Library Search Order 以查看是否有任何适用于您的具体情况。

    【讨论】:

      【解决方案3】:

      L("..\\Debug\\LoadDLL\\LoadDLL.dll") 中的 L 是什么?您可能的意思是 L"..\\Debug\\LoadDLL\\LoadDLL.dll" 没有宽字符字符串的括号。除非您创建了 L 宏,否则我不确定它是如何编译的?顺便说一下,_T(x) 宏扩展为 L ## x(在宽字符版本中)...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-17
        相关资源
        最近更新 更多