windowsdll

current directory vs directory from which the application loaded


In the Microsoft documention the dll loading order is defined under the following link

https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order

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.

I don't understand the difference between

  1. directory from which the application loaded
  2. current directory

Let's say the application is installed in %programfiles% and I have a shortcut on the desktop to start it. In that scenario what is "The directory from which the application loaded."? The current directory must be the dektop, right?

Any clarification is greatly appreciated.


Solution

  • The comments basically answer your question, but let me answer it directly, and then provide examples.

    1. The "directory from which the application loaded" is the directory that contains the .exe for the application. Note that the location of a shortcut doesn't affect this--the shortcut just points to the location of the .exe.

    2. The "current directory" is, as @ErykSun mentioned, the process's working directory; it can change as the process runs (the developer can make a call to SetCurrentDirectory). The process's working directory will often start simply as the directory containing the .exe though.

      However, a shortcut can modify the initial working directory to be a directory other than that containing the .exe, using the shortcut property "Start in".

    Here's an example:

    1. Application shortcut exists in "C:\Users\Me\Desktop"
    2. Application .exe exists in "C:\Program Files\App" - This is the dir from which app loads
    3. Shortcut's "Start In" directory is specified as "C:\Users\Me\RunDir". - This is the curr dir at process start

    Or a simpler situation:

    1. Application .exe exists in "C:\Program Files\App" and you run it by simply opening it in that folder (either programmatically or through Windows GUI) - At the time the process starts, both of the dirs in question refer to this dir

    Services

    One common example of when these two directories are different is with services. A service that runs under the SYSTEM account will generally start the process from the "C:\Windows\System32" directory, while the application executable will generally reside in some other directory, e.g. within "C:\Program Files".