18

On linux, we have LIBRARY_PATH and LD_LIBRARY_PATH environment variables in order for programs to search for libraries. Do we have similar thing on windows? Particularly Windows 7?

Also, I would like to know best practices for DLL use (where to put them, use envs or not, etc.), since I want to work on windows like everyone does, and not to sloth myself on workarounds :)

Luís Guilherme
  • 2,620
  • 6
  • 26
  • 41

4 Answers4

17

Edit: As explained by Bob, this answer describes the Alternate Search Order, which is not what most applications would see. The full rules are quite complex. I don't think I can summarize them here. Instead, read the Microsoft docs - https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order

My original answer was:

This MSDN article explains the default search order. I quote:

  1. The directory specified by lpFileName.
  2. The system directory. Use the GetSystemDirectory function to get the path of this directory.
  3. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
  4. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
  5. The current directory.
  6. The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.

In (1), if you statically linked against the DLL's stub library, I think "the directory specified by lpFileName" is the process's exe's path.

Andrew Bainbridge
  • 4,651
  • 3
  • 35
  • 50
  • 2
    This answer describes the *Alternate Search Order*. Most applications would use the *Standard Search Order* by default, which searches the application directory first, and whether the current working directory is searched before or after the system directories depends on `SafeDllSearchMode`. This is not a recent change; it was the case [back in 2011](https://web.archive.org/web/20110529192020/http://msdn.microsoft.com/en-us/library/ms682586(v=vs.85).aspx) when the answer was written and [still applies](https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order). – Bob Mar 23 '20 at 13:10
  • Fair point. I've updated the answer accordingly. In my defense, Windows XP was still the most common version at the time I wrote the answer, and I had no access to anything newer. However, I should still have pointed out the scope of my answer. For historical interest, here's a graph of Windows version popularity over time https://gs.statcounter.com/windows-version-market-share/desktop/worldwide/#monthly-200901-202002 – Andrew Bainbridge Mar 23 '20 at 14:26
  • 1
    Looking [at the historical version](https://web.archive.org/web/20110529192020/http://msdn.microsoft.com/en-us/library/ms682586(v=vs.85).aspx), the distinction between Standard and Alternate search orders depends purely on the API call used (and XP also used Standard by default, as the name implies). The *only* difference with early (pre-SP2) XP was SafeDllSearchMode, which shifts the order of the current directory location but again does not affect Standard vs Alternate. And pre-SP2 XP is ancient and even more insecure - XP mostly gained popularity post-SP2. – Bob Mar 23 '20 at 17:12
  • 1
    I mean, thanks for updating - but unless I completely misread the article, I don't think this is correct for XP either. As much as I hope people aren't looking for XP answers in 2020, it's probably still best to be accurate. – Bob Mar 23 '20 at 17:13
  • It's 9 years since I thought about this stuff. I really thought this answer was correct at the time, but it seems it never was. Thanks for the feedback. – Andrew Bainbridge Mar 23 '20 at 21:44
2

Take a look at the help for the LoadLibrary and CreateProcess functions. These describe the paths used to locate DLLs, and how you can modify them.

0

It looks on currentDir first then WinDir and SystemDir also in your path

Luca Rocchi
  • 6,272
  • 1
  • 23
  • 23
  • 4
    I don't think that is true. The current directory is 5th on the search list. The first thing on the list is the folder the exe was found in, which often isn't the current directory. – Andrew Bainbridge Jul 01 '11 at 14:56
  • looks like it looks in systems folder first now – thang Jul 17 '18 at 00:45
0

According to what @andrew has mentioned in his answer, the order of folders that are used on Windows to search a DLL may be different from one configuration to another. I think the simplest way to check this order on Windows is to use the Dependency Walker tool. After opening the tool, and pressing the "Configure Module Search Order" button on the toolbar, you will see a window like this:

enter image description here

This window shows you the current search order on your machine. The interesting part is that by pressing "Expand", you can see the whole folders in the search path one by one. You may also change the order if you want, to be used for loading an specific module.

TonySalimi
  • 8,257
  • 4
  • 33
  • 62