LoadLibrary fails; GetLastError no help

社会主义新天地 提交于 2019-12-25 08:58:03

问题


I have a project that I converted from Visual Studio 2003 .NET to Visual Studio 2010. It's NOT a .NET project; it's Visual C++ (unmanaged).

The DLL pulls in additional DLLs. If I link an executable with this DLL, then the executable dies during the initialization of the DLL. (I can tell, there are constructors for static objects that are being called, and I can see their operation.) I've removed ALL VS 2010-created DLLs from my path, except for one of them, which causes the error. Replacing that one with the VS.NET-created version allows the program to run.

Since I'm not getting any useful information, I decided to write a test application that doesn't directly link to the DLL, but instead uses LoadLibrary to load the library. The idea was that I could use GetLastError() to help figure the issue with the library. No go; I get an error code -529697949, which isn't a Windows error code at all! (If I change the DLL to the VS.NET-created version, the program loads the DLL properly.)

I used the Dependency Walker (www.dependencywalker.com) to check the DLL, and it tells me that "At least one delay-load dependency module was not found," highlighting IESHIMS.DLL and WER.DLL. I am seeing no other error with that tool. Running it on the VS.NET-created DLL shows the same two warnings, so I figure this is a red herring.

static void showMessage(const wchar_t *wmsg)
{
        std::wcout << wmsg << std::endl;
        ::MessageBox(NULL, wmsg, TEXT("Message"), MB_OK);
}

static void testLoadLibrary(const wchar_t *lib)
{
        ::SetLastError(0L);
        ::SetErrorMode(0);

        std::wstringstream wss;

        wss << "LoadLibrary: " << lib;
        showMessage(wss.str().c_str());
        HINSTANCE LoadME = ::AfxLoadLibrary(lib);
        if (LoadME == NULL) {
                DWORD dw = ::GetLastError();
                wss << "Failed: Error code " << dw;
                showMessage(wss.str().c_str());
                ErrorExit(lib, dw);
        } else {
                wss << "LoadLibrary of " << lib << " succeeded.";
                showMessage(wss.str().c_str());
                ::FreeLibrary(LoadME);
        }
}

Finally, I ran Process Monitor (sysinternals.com) to monitor the test program, looking at all entries with Path containing the string "dll." I don't see anything particularly informative in this list--no idea why the DLL is failing to load.

If I use LoadLibraryEx with DONT_RESOLVE_DLL_REFERENCES, the library loads, so this really looks like a dependency issue, which is why I'm surprised that the dependency walker isn't being particularly helpful.

I've tried this on Windows 2008 R2 and Windows 2003; same behavior.

Any suggestions?


回答1:


There is an OS tool to help diagnose problems like this. Download the SDK or DDK and use gflags.exe to set 'Show Loader Snaps' (+sls) for the process. That should reveal why the DLL load fails.

The loader snaps output will appear in the debugger output window.

Martyn



来源:https://stackoverflow.com/questions/6154600/loadlibrary-fails-getlasterror-no-help

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!