locked
DLL search order oddity RRS feed

  • Question

  • I have read 

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

    BUt am confused by something.  Recently, one of our field engineers ran into this problem.

    1.  Executable is in c:\foo\bin\foo.exe

    2.  foo.exe spawn bar.exe

    3. bar.exe depends on baz.dll

    3. baz.dll is not in the system directories, nor is it on the PATH

    4. We launch foo.exe, which in turns launches bar.exe, which loads baz.dll.  However, while baz.dll is not on the PATH, it is found in a location we do not epect (say in c:\quux\bin\baz.dll)

    5. The problem is c:\quuz\bin\baz.dll does not contain an entry point needed by bar.exe

    Why would this happen?  According to the link I sent above, this should just not happen, unless I am missing something

    Thanks,

    Tom

    Friday, May 1, 2020 4:11 PM

All replies

  • Are you asking about .Net applications and class libraries? 

    Friday, May 1, 2020 5:05 PM
  • These are binaries compiled from C and C++ code, not .NET
    Friday, May 1, 2020 5:08 PM
  • The rules are elaborate, but they are pretty clear.  It will check the directory where the base application came from, so if you launched \quuz\bin\bar.exe, that would do it.  It will check the current directory, so if bar.exe changes the current directory to \quuz\bin, that would do it.  It will check the PATH.  Our PATHs sometimes get very long, so that's worth double-checking.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, May 1, 2020 5:57 PM
  • The PATH I have set is very minimal.  Just c:\foo\bin, and c:\foo\bin contains baz.dll (I had erroneously reported that baz.dll is not on the PATH, sorry about that).  So given the search order rules, baz.dll should be found in PATH when bar.exe is launched, but it is not.  It is coming from some location that is not even in PATH, and it is not in the system directories, etc...

    Friday, May 1, 2020 6:04 PM
  • Are you calling a DLL with the same name as a system DLL?  Have you checked the KnownDLLs list in the registry?  You didn't mention where bar.exe loads from -- that will be first on the list.  And you are quite sure that neither app has changed the current directory?  Remember, the PATH is the last place on the search order.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Saturday, May 2, 2020 5:07 AM
  • Yet another possibility is that the PATH environment variable in the foo.exe process (or the bar.exe process) has been changed so that c:\quuz has been placed in the string prior to c:\foo\bin.

    If you execute bar.exe directly instead of spawning it from foo does the issue occur?

    You can also use Process Explorer to examine the content of the PATH environment variable of a running process.
    • Edited by RLWA32 Saturday, May 2, 2020 11:51 AM added process explorer suggestion
    Saturday, May 2, 2020 11:22 AM
  • Hello Tom Kacvinsky,

    Has this issue been solved. If yes, you can accept the reply helpful. If not, feel free let me know if you have any concern.

    Best regards,

    Rita


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 5, 2020 9:17 AM
  • With PATH set to a minimal value (just the location of foo.exe and baz.dll), spawning bar.exe has the exact same problem.  Also, baz.dll is not part of the known DLLs registry setting.
    Tuesday, May 5, 2020 10:57 AM
  • No resolved yet.
    Tuesday, May 5, 2020 10:58 AM
  • Hello Tom Kacvinsky,

    I put foo.exe and bar.exe and baz.dll all at path: C:\foo\bin.

    At this condition, the baz.dll is found at C:\foo\bin. I check it using GetModuleFileName.

    foo.cpp

    int main()
    {
    	STARTUPINFOW si = { 0 };
    	si.cb = sizeof(si);
    
    	PROCESS_INFORMATION pi = { 0 };
    
    	if(!CreateProcess(L"bar.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    	{
    		printf("CreateProcess fails with error: %d\n", GetLastError());
    	}
    	
    	getchar();
    }

    bar.cpp

    int main()
    {
    	HMODULE hm = LoadLibrary(L"baz.dll");
    	if (NULL == hm)
    	{
    		printf("LoadLibrary fails with error: %d\n", GetLastError());
    	}
    	WCHAR dllPath[256];
    	if(!GetModuleFileName(hm, dllPath, sizeof(dllPath)))
    	{
    		printf("GetModuleFileName fails with error: %d\n", GetLastError());
    	}
    	else
    	{
    		wprintf(L"dllPath: %s\n", dllPath);
    	}
    	getchar();
    }

    I set no path for c:\foo\bin. I change directory to path c:\foo\bin from command line.

    For trying to reproducing this issue, could you show the following information?

    • Where your bar.exe located?
    • How do you launche bar.exe?

    Best regards,

    Rita


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Wednesday, May 6, 2020 9:11 AM
  • Did you check the PATH string in the running processes with Process Explorer?

    Do you have some other software running that has hooked LoadLibrary/LoadLibraryEx functions?

    Wednesday, May 6, 2020 9:29 AM