VC++-MFC: LoadLibrary 返回无效句柄 0x10000000

发布于 2024-08-09 03:00:54 字数 977 浏览 4 评论 0原文

我无法使用 LoadLibrary() API 加载 test.dll(在 VC++ - MFC 中)。我使用 GetLastError() 收到错误代码 126(未找到模块)。 通过dependency walker,我知道我的test.dll依赖于“xerces-c_2_7.dll”和“Xalan-C_1_10.dll”。这些 dll 已经存在于与 exe 相同的路径上。 我仍然收到错误。 因此,我尝试使用 LoadLibrary() 加载上述第三方 dll,它返回的句柄为 0x10000000。通过 GetLastError() 我得到了

error code 6 (Invalid Handle).

任何人都可以指导我为什么会收到无效句柄错误吗?

下面是代码片段:

HINSTANCE hLib = LoadLibrary(_T("Xalan-C_1_10"));
TCHAR szMessage[MAX_PATH];
FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS|
    FORMAT_MESSAGE_FROM_SYSTEM,
    NULL, GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
    szMessage, MAX_PATH, NULL);
    hLib = LoadLibrary(_T("xerces-c_2_7"));
FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS|
    FORMAT_MESSAGE_FROM_SYSTEM,
    NULL, GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
    szMessage, MAX_PATH, NULL);

I am unable to load my test.dll (in VC++ - MFC) using LoadLibrary() API. I get error code 126 (Module not found)using GetLastError().
By dependency walker I have come to know that my test.dll depends on "xerces-c_2_7.dll" and "Xalan-C_1_10.dll". These dlls were already present on the same path as the exe.
Still I am getting the error.
So I tried to load both the above mentioned third party dlls using LoadLibrary() which returned handle as 0x10000000. By GetLastError() I am getting

error code 6 (Invalid Handle).

Can anyone please guide me on why I am getting the Invalid Handle error?

Below is the code snipet:

HINSTANCE hLib = LoadLibrary(_T("Xalan-C_1_10"));
TCHAR szMessage[MAX_PATH];
FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS|
    FORMAT_MESSAGE_FROM_SYSTEM,
    NULL, GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
    szMessage, MAX_PATH, NULL);
    hLib = LoadLibrary(_T("xerces-c_2_7"));
FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS|
    FORMAT_MESSAGE_FROM_SYSTEM,
    NULL, GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
    szMessage, MAX_PATH, NULL);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北音执念 2024-08-16 03:00:54

提炼您的问题,我收集到:

  • 您可以单独加载 Xerces 和 Xalan,但不能单独加载 test.dll 文件(引用其他 dll)

  • 加载 Xerces 或 Xalan 时 GetLastError() 返回的值可能会误导您

请记住,您必须调用 GetLastError()LoadLibrary 调用之后立即:来自 MSDN 文档:

当函数的返回值指示此类调用将返回有用数据时,您应该立即调用 GetLastError 函数。这是因为某些函数在成功时会用零调用 SetLastError,从而清除最近失败的函数设置的错误代码。

这很可能是依赖项/路径解析问题(即 LoadLibrary 找不到请求的图像)。因此,您有两个选择:

  • test.dll/Xerces/Xalan 是否具有您尚未复制到可执行文件文件夹的其他依赖项?

  • 将dll放入Windows\System32文件夹中,然后重试。如果这有效,那么您可以确定遇到了路径问题。阅读 LoadLibrary——这有点令人困惑,但有一些细节很容易被遗漏。

  • 使用GetCurrentDirectorySetCurrentDirectory来切换应用程序的执行文件夹和dll的驻留文件夹

  • 在对 LoadLibrary 的调用中指定 dll 的完整路径。如果路径名包含空格,您可能会遇到问题(我凭记忆回忆起这一点,请查阅 MSDN)。

  • 完成后,做一个好公民并致电 FreeLibrary

Distilling your problem, I gather:

  • You are able to load Xerces and Xalan individually, but not the test.dll file (which references the other dlls)

  • You may be misled by the value GetLastError() returns when loading either Xerces or Xalan

Remember that you must call GetLastError() immediately after the LoadLibrary call: From MSDN docs:

You should call the GetLastError function immediately when a function's return value indicates that such a call will return useful data. That is because some functions call SetLastError with a zero when they succeed, wiping out the error code set by the most recently failed function.

This is most probably a dependency/path resolution issue (i.e. LoadLibrary can't find the requested image). So, you have two options:

  • Does test.dll/Xerces/Xalan have other dependencies that you have not copied to your executable's folder?

  • Put the dll in Windows\System32 folder and try again. If this works, then you can be sure you had hit a path problem. Read the MSDN page on LoadLibrary -- it's a bit confusing but has some details that's easy to leave out.

  • Use GetCurrentDirectory and SetCurrentDirectory to switch to and from the application's executing folder and the dll's residing folder

  • Specify the full path to the dll in the call to LoadLibrary. You may face issues if the path name contains whitespace (this I recall from memory, please check with MSDN).

  • Once done, be a good citizen and call FreeLibrary!

吹泡泡o 2024-08-16 03:00:54

test.dll 是您的 DLL 还是第三方 DLL?

If it is a your DLL, 

您应该能够轻松地看到代码中的依赖 DLL它的确切路径。

If it is third party DLL,

你需要尝试 Windows 路径、程序文件路径、测试 DLL 路径等...记住这只是一次尝试。
有时他们可能会引用只有 DLL 创建者才知道的路径如果没有记录

希望这个答案有帮助!

Is test.dll your DLL or third party DLL?

If it is a your DLL, 

you should be easily able to see the depent DLLs in the code & its exact path.

If it is third party DLL,

you need to try windows path, program files path, your test DLL path etc... Remember it is only a try.
Sometimes they may refer to the path which is only known to the creator the DLL if not documented)

Hope this answer helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文