windows 为 win32 控制台应用程序设置库路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/694351/
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-09-09 06:12:29  来源:igfitidea点击:

Setting Library path for win32 console applications

windowsdllmsdn

提问by maximus1986

I am getting "dll not found:restarting the application may fix the problem" error when i try to execute a simple "HelloWorld" win32 console application. I know the location of the .dll. How to specify its location when executing the .exe from command prompt?

当我尝试执行一个简单的“HelloWorld”win32 控制台应用程序时,出现“找不到 dll:重新启动应用程序可能会解决问题”错误。我知道 .dll 的位置。从命令提示符执行 .exe 时如何指定其位置?

PS: copying the .dll to the .exe's current dir seems to solve the problem, but this approach is not suitable in this case.

PS:将 .dll 复制到 .exe 的当前目录似乎可以解决问题,但这种方法不适用于这种情况。

回答by Bevan

DLL loading happens deep in the plumbing of windows.

DLL 加载发生在 Windows 管道的深处。

If the DLL is not found in the same directory as the application, the PATH is automatically scanned in order to find the directory.

如果在与应用程序相同的目录中找不到 DLL,则会自动扫描 PATH 以找到该目录。

So, the simplest answer to your problem is to add the directory containing the DLL to your PATH. Depending on when the DLL needs to be loaded by your code, you may be able to (temporarily) modify the PATH from inside your "HelloWorld" application.

因此,解决您的问题的最简单方法是将包含 DLL 的目录添加到您的 PATH。根据您的代码何时需要加载 DLL,您可以(临时)从“HelloWorld”应用程序内部修改 PATH。

回答by Emilio M Bumachar

To manually, permanently add your path to Windows PATH (permanently = until you remove it), right click My Computer>Properties>Advanced>Environment Variables>System Variables>Path>Edit>Variable Value, add a semicolon (which means "in addition to all before") and paste the full path of your dll.

要手动、永久地将您的路径添加到 Windows PATH(永久=直到您删除它),请右键单击我的电脑>属性>高级>环境变量>系统变量>路径>编辑>变量值,添加一个分号(这意味着“另外之前的所有内容”)并粘贴 dll 的完整路径。

Windows will search the path every time it can't find something in the current directory.

每次在当前目录中找不到东西时,Windows 都会搜索路径。

回答by R?zvan Flavius Panda

From: http://msdn.microsoft.com/en-us/library/7d83bc18.aspx

来自:http: //msdn.microsoft.com/en-us/library/7d83bc18.aspx

With both implicit and explicit linking, Windows first searches for "known DLLs", such as Kernel32.dll and User32.dll. Windows then searches for the DLLs in the following sequence:

  1. The directory where the executable module for the current process is located.

  2. The current directory.

  3. The Windows system directory. The GetSystemDirectory function retrieves the path of this directory.

  4. The Windows directory. The GetWindowsDirectory function retrieves the path of this directory.

  5. The directories listed in the PATH environment variable.

通过隐式和显式链接,Windows 首先搜索“已知 DLL”,例如 Kernel32.dll 和 User32.dll。然后 Windows 按以下顺序搜索 DLL:

  1. 当前进程的可执行模块所在的目录。

  2. 当前目录。

  3. Windows 系统目录。GetSystemDirectory 函数检索此目录的路径。

  4. Windows 目录。GetWindowsDirectory 函数检索此目录的路径。

  5. PATH 环境变量中列出的目录。

回答by Matt Ellis

The documentation for LoadLibraryExhas some discussion on how Windows searches for your dll. You might try using the LOAD_WITH_ALTERED_SEARCH_PATH flag if you can construct a full path to your DLL or use the SetDllDirectoryfunction to add a directory to the search path.

LoadLibraryEx的文档对Windows 如何搜索您的 dll 进行了一些讨论。如果可以构建 DLL 的完整路径或使用SetDllDirectory函数将目录添加到搜索路径,则可以尝试使用 LOAD_WITH_ALTERED_SEARCH_PATH 标志。