VC++-MFC: LoadLibrary 返回无效句柄 0x10000000
我无法使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
提炼您的问题,我收集到:
您可以单独加载 Xerces 和 Xalan,但不能单独加载 test.dll 文件(引用其他 dll)
加载 Xerces 或 Xalan 时
GetLastError()
返回的值可能会误导您请记住,您必须调用
GetLastError()
在LoadLibrary
调用之后立即:来自 MSDN 文档:这很可能是依赖项/路径解析问题(即 LoadLibrary 找不到请求的图像)。因此,您有两个选择:
test.dll/Xerces/Xalan 是否具有您尚未复制到可执行文件文件夹的其他依赖项?
将dll放入Windows\System32文件夹中,然后重试。如果这有效,那么您可以确定遇到了路径问题。阅读
LoadLibrary
——这有点令人困惑,但有一些细节很容易被遗漏。使用
GetCurrentDirectory
和SetCurrentDirectory
来切换应用程序的执行文件夹和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 XalanRemember that you must call
GetLastError()
immediately after theLoadLibrary
call: From MSDN docs: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
andSetCurrentDirectory
to switch to and from the application's executing folder and the dll's residing folderSpecify 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
!test.dll 是您的 DLL 还是第三方 DLL?
您应该能够轻松地看到代码中的依赖 DLL它的确切路径。
你需要尝试 Windows 路径、程序文件路径、测试 DLL 路径等...记住这只是一次尝试。
有时他们可能会引用只有 DLL 创建者才知道的路径如果没有记录)
希望这个答案有帮助!
Is test.dll your DLL or third party DLL?
you should be easily able to see the depent DLLs in the code & its exact path.
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!