LoadLibrary() 错误代码 127
我在使用 LoadLibrary() 时遇到问题,并收到一个对我来说没有意义的错误:
I'm having trouble with LoadLibrary() and getting an error that doesn't make sense to me:
::SetLastError(0);
m_hDll = ::LoadLibrary(szName);
if (m_hDll == NULL) // Failure to load the DLL.
{
DWORD err = GetLastError();
}
错误是 127(找不到指定的过程.")这对我来说在调用 LoadLibrary() 时没有任何意义. 我还没有调用 GetProcaddress() 然而.
The error is 127 ("The specified procedure could not be found.") That doesn't make any sense to me on a call to LoadLibrary(). I haven't called GetProcaddress() yet.
DLL(和应用程序)都是用 VS++ 2005 SP1 编译的.
The DLL (and the application) are both compiled with VS++ 2005 SP1.
可能出了什么问题?
让我们一步一步来:
错误消息意味着找到了 dll,但缺少所需的函数.(抖动是对的.)这意味着您拥有所需的 dll,但没有正确的版本.(Davefiddes 是对的,尽管问题可能出在任何 dll 上,而不仅仅是 Microsoft 运行时库.而且,至少对于主要更新,Microsoft 为其运行时库提供了不同的名称,因此在这种情况下,它不会成为问题.)
The error message means that the dll was found but a required function is missing. (Jitter is right.) This implies that you have the dll you need, but not the right version. (Davefiddes is right, although the problem can be any dll, not just the Microsoft runtime library. And, at least for major updates, Microsoft gives its runtime libraries different names, so in that case it wouldn't be an issue.)
这没有意义,因为没有从正在加载的 dll 请求任何函数.(亚当是对的.)
This doesn't make sense, because no function has been requested from the dll being loaded. (Adam is right.)
因此,预计丢失的函数不会在 LoadLibrary 命令显式加载的 dll 中找到,而是在同时隐式加载的依赖 dll 中找到,因为第一个 dll 需要它.(Zebrabox 很近.)
Therefore, the missing function was expected to be found not in the dll which is being explicitly loaded by the LoadLibrary command, but in a dependent dll which is being implicitly loaded at the same time, because the first dll requires it. (Zebrabox was close.)
依赖 dll 是静态"链接到显式加载的库的 dll,通过导入库或 .lib 文件,包含在显式加载的 dll 的链接器步骤中.(我敢打赌你不知道动态链接库"可以静态链接".好吧,现在你知道了.)
A dependent dll is a dll that is "statically" linked to the library being explicitly loaded, via an import library, or .lib file, included on the linker step of the explicitly loaded dll. (I bet you didn't know that a "dynamic link library" could be "statically linked." Well, now you do.)
如果您在不同的文件夹中有相同 dll 的多个版本,那么这也可能是搜索路径问题(正如 zebrabox 所建议的那样).Dll 路径搜索顺序本身就是一个复杂的主题:参见 http://msdn.microsoft.com/en-us/library/ms682586(VS.85).aspx.这取决于操作系统等.在可行的情况下,最安全的方法是将所有潜在问题的 dll 与您的 exe 放在同一文件夹中.
If you have multiple versions of the same dll in different folders, then this could also be a search path problem (as zebrabox suggests). Dll path search order is a complicated subject in itself: see http://msdn.microsoft.com/en-us/library/ms682586(VS.85).aspx . It depends on operating system, among other things. The safest bet, where practical, is to put all the potential problem dlls in the same folder as your exe.
依赖的dll也可以有自己的依赖dll,这使得这个问题很难解决.视情况而定可能会有所帮助,但如果没有,请尝试 filemon.在您的错误消息之前成功读取的最后一个 dll 是错误版本的那个.
Dependent dlls can also have their own dependent dlls, which can make this problem very difficult to resolve. Depends might help, but if it doesn't, try filemon. The last dll that's successfully read before your error message is the one that's the wrong version.