Is it possible to add a directory to DLL search path from a batch file or cmd script?

53,118

Solution 1

You can place the DLL in the same path as the executable, which is searched first before %WINDIR%. There's no way to call SetDllDirectory from a batch file directly.

But, you can insert your DLL directory in the %PATH% variable, and Windows will then find the DLL there.

set PATH=C:\path to your dll;%PATH%

Solution 2

The aim is to have our development version of a dll found before a pre-existing older one in %WINDIR% etc. without having to write a program just for that.

If the DLL is not in the same folder as the executable Windows will search for the file in the folders specified in the system path. So all you need to do is put your folder at the start of the path.

You can do this using the following batch command:

 set PATH=c:\MyDLLFolder;%PATH%

If your path contains white space you need to use the following batch command:

 set PATH="C:\My DLL Folder";%PATH%

But remember this path change is only made to the PATH of the current console session. If you close and reopen the console these path changes will be lost.

Solution 3

To clear up dispute on the dll search order (in the comments on @jussij's answer), here's the list, drawn from Microsoft's doc:

If SafeDllSearchMode is enabled, the search order is as follows:

  1. The directory from which the application loaded.
  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.

If SafeDllSearchMode is disabled, the search order is as follows:

  1. The directory from which the application loaded.
  2. The current directory.
  3. The system directory. Use the GetSystemDirectory function to get the path of this directory.
  4. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
  5. The Windows directory. Use the GetWindowsDirectory function to get the path of this 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.

See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx#standard_search_order_for_desktop_applications

Share:
53,118
matt wilkie
Author by

matt wilkie

hewer of maps, old time techie, newbie developer personal - www.maphew.com, @maphew in-between - yukongis.ca work - www.env.gov.yk.ca, @mhw-at-yg

Updated on July 31, 2022

Comments

  • matt wilkie
    matt wilkie almost 2 years

    MSDN says that the function SetDllDirectory() can be used to insert a directory into the DLL Search Path. Can this function be accessed from a batch file or cmd script, perhaps using via cscript?

    The aim is to have our development version of a dll found before a pre-existing older one in %WINDIR% etc. without having to write a program just for that.

    Thanks in advance for your time and thoughts.

  • matt wilkie
    matt wilkie over 15 years
    This jumps steps in the middle. After searching the 1) directory the calling .exe is in, Windows searches 2) the system directory, 3) the 16bit system directory, 4) the windows directory, 5) the current directory, 6) and NOW finally searches %path%. See the /DLL Search Path/ link in the question.
  • jussij
    jussij over 15 years
    Matt, that is why I qualified my comment with 'If the DLL is not in the same folder as the executable'. As to the people who are stupid enough to put their non-system dll's into system folders then good luck to them ;)
  • matt wilkie
    matt wilkie about 15 years
    the 'If DLL is not in same folder as executable' qualification is correct, but "all you need to do is put your folder at the start of the path" is an error. PATH is not searched until AFTER the system and windows directories.
  • jussij
    jussij about 15 years
    Why would you ever put a non-system dll in the system folder? I've never needed to do that and would go so far as to say it's a pretty stupid thing to do. I've always use this approach and it has never let me down. Just leave the system folder to the system and everything works just fine ;)
  • jussij
    jussij about 15 years
    I don't recall the reference to %WINDIR% when I first replied to question so I'm assuming it was added with a later edit. In any case I still don't understand why the dll has to be in the windows system folder? Are you saying the dll has to be in the system folder? If so what type of dll is it?
  • matt wilkie
    matt wilkie over 14 years
    Sorry, I missed your follow on question somehow: "I still don't understand why the dll has to be in the windows system folder?" Simply because sometimes other applications which aren't part of our project put their dll's in the system directory, and theirs are found before ours, which screws us up. The solution we've adopted is here: trac.osgeo.org/osgeo4w/wiki/dllupdate