C++ LoadLibrary() 相对于 dll 的地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15063058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:58:39  来源:igfitidea点击:

LoadLibrary() relative address to dll

c++windowsvisual-studio-2010winapidll

提问by A23149577

I am trying to load a dll in my code in windows, I load my dll successfully with LoadLibrary() function but I have a question, I give the path to my dll like:

我正在尝试在 Windows 的代码中加载一个 dll,我使用 LoadLibrary() 函数成功加载了我的 dll 但我有一个问题,我给出了我的 dll 的路径,例如:

LoadLibrary(C:\path\to\my\dll);

I wonder if I can give the relative path to my dll. I mean for example:

我想知道是否可以提供我的 dll 的相对路径。我的意思是例如:

LoadLibrary(.\my dll directory\my dll.dll)

Is it possible? if not, how can I develop my project which it can be portable without changing the path to dll in different machines?

是否可以?如果没有,我如何开发我的项目,它可以在不更改不同机器中 dll 路径的情况下进行移植?

回答by selbie

It's most likely failing because you forgot to escape the backslashes in your second call to LoadLibrary. Maybe that was a typo when wrote your question, because you also forgot the quote marks for the file name. ;) That is, change this line:

它很可能失败,因为您在第二次调用 LoadLibrary 时忘记转义反斜杠。也许在写你的问题时这是一个错字,因为你也忘记了文件名的引号。;) 也就是说,改变这一行:

LoadLibrary(.\my dll directory\my dll.dll);

To be this:

要这样:

LoadLibrary(L".\my dll directory\my dll.dll");

(And I'm not sure if the leading .\\is needed)

(我不确定是否.\\需要领导)

And if that doesn't fix it, then this most likely will do what you need:

如果这不能解决问题,那么这很可能会满足您的需求:

wchar_t szFullPath[MAX_PATH] = {};
GetCurrentDirectory(MAX_PATH, szFullPath);
PathCchAppend(szFullPath, MAX_PATH, L"my dll directory\my dll.dll");
HMODULE hDLL = LoadLibrary(szFullPath);

And finally, LoadLibrary has different behaviors for searching for dependent DLLs. And it varies based on how you specify the path. That might be what's impacting your ability to load the DLL from a relative search path. Read the MSDN page on itand consider looking at the various options calls such as LoadLibraryExand SetDllDirectorycan do for making search paths easier to deal with. This pageon DLL search paths as well.

最后,LoadLibrary 有不同的搜索依赖 DLL 的行为。它根据您指定路径的方式而有所不同。这可能是影响您从相对搜索路径加载 DLL 的能力的原因。阅读其上的 MSDN 页面,并考虑查看LoadLibraryExSetDllDirectory等调用可以使搜索路径更易于处理的各种选项。DLL 搜索路径上的此页面也是如此。

回答by David Heffernan

First of all, I assume that you meant to write:

首先,我假设你打算写:

LoadLibrary(".\my dll directory\my dll.dll");

The documentationanswers your question:

文档回答您的问题:

If a relative path is specified, the entire relative path is appended to every token in the DLL search path list. To load a module from a relative path without searching any other path, use GetFullPathName to get a nonrelative path and call LoadLibrary with the nonrelative path.

如果指定了相对路径,则整个相对路径将附加到 DLL 搜索路径列表中的每个标记。要从相对路径加载模块而不搜索任何其他路径,请使用 GetFullPathName 获取非相对路径并使用非相对路径调用 LoadLibrary。

So yes, you can specify a relative path. But the way it is interpreted is perhaps not what you were expecting. The DLL search will take each path in the DLL search path in turn, combine that with your relative path, and try to load that DLL.

所以是的,您可以指定一个相对路径。但是它的解释方式可能不是您所期望的。DLL 搜索将依次获取 DLL 搜索路径中的每个路径,将其与您的相对路径结合,并尝试加载该 DLL。

So, if you want your relative path to be relative to the current working directory, call GetFullPathNameto expand it to an absolute path, and then load that. If you want your relative path to be interpreted relative to some other directory, then combine with that directory and load the DLL with an absolute path.

因此,如果您希望相对路径相对于当前工作目录,请调用GetFullPathName将其扩展为绝对路径,然后加载它。如果您希望相对于某个其他目录解释相对路径,请与该目录结合并使用绝对路径加载 DLL。