Setting Library path for win32 console application

2019-03-31 21:39发布

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?

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

标签: windows dll msdn
4条回答
爷、活的狠高调
2楼-- · 2019-03-31 22:16

The documentation for LoadLibraryEx has 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 SetDllDirectory function to add a directory to the search path.

查看更多
够拽才男人
3楼-- · 2019-03-31 22:24

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 will search the path every time it can't find something in the current directory.

查看更多
趁早两清
4楼-- · 2019-03-31 22:30

DLL loading happens deep in the plumbing of 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.

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.

查看更多
我只想做你的唯一
5楼-- · 2019-03-31 22:37

From: 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.

查看更多
登录 后发表回答