CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2012
    Posts
    58

    Resolved [solved] wired dll locating problem

    I encounter a wired problem when dynamically loading a .dll file under windows xp sp3.


    dll.cpp

    HTML Code:
    #include <windows.h>
    
    BOOL APIENTRY DllMain( HANDLE hModule,
    					  DWORD  ul_reason_for_call,
    					  LPVOID lpReserved )
    {
    	return TRUE;
    }
    caller.cpp

    HTML Code:
    int main(int argc, char *argv[])
    {
    	HMODULE h = LoadLibraryA("c:\\x.dll");
    	DWORD gla = GetLastError();
    }

    when i compile dll.cpp with visual studio 2008, copy the resulting .dll file to c:\\x.dll and step over caller.cpp in VS, h has the base address of the dll and gla is zero.
    when i compile dll.cpp with g++ (mingw), copy the resulting .dll file to c:\\x.dll and step over caller.cpp in VS, h is zero and gla is 0x7E.

    g++ -shared dll.cpp -o x.dll

    Code:
    ERROR_MOD_NOT_FOUND
        126 (0x7E)
        The specified module could not be found.
    Great.

    But get this: if i run caller.exe outside of VS, it works just fine IN BOTH CASES!


    What wired black magic is going on here?
    Last edited by tuli; August 12th, 2013 at 03:30 AM. Reason: solved

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: wired dll locating problem

    Quote Originally Posted by tuli View Post
    But get this: if i run caller.exe outside of VS, it works just fine IN BOTH CASES!
    What wired black magic is going on here?
    And why would it NOT work "IN BOTH CASES"?
    Your code does "work" since it does nothing after trying to LoadLibrary!
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2012
    Posts
    58

    Re: wired dll locating problem

    by "does work" i imply the library loaded correctly - i modified caller.cpp to dump h and gla to a file. Guess i should have mentioned that.


    My best bet is that somehow the init routine (DllMain) doesnt return TRUE with g++ ... and VS somehow takes offense...Though it is beyond me what the problem may be.

    I continue to appreciate any input.

  4. #4
    Join Date
    Jul 2002
    Posts
    2,543

    Re: wired dll locating problem

    "The specified module could not be found" - this may apply to x.dll or one of its dependencies. How exactly do you run caller.exe outside of VS? Maybe from MinGW command line, in this case x.dll is able to find all its dependencies.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: wired dll locating problem

    Quote Originally Posted by tuli View Post
    by "does work" i imply the library loaded correctly - i modified caller.cpp to dump h and gla to a file. Guess i should have mentioned that.


    My best bet is that somehow the init routine (DllMain) doesnt return TRUE with g++ ... and VS somehow takes offense...Though it is beyond me what the problem may be.

    I continue to appreciate any input.
    Why not place a breakpoint or output a message in the DllMain() function? Then there is no need to guess what is happening when you call LoadLibrary. Just to warn you -- do not put calls to MessageBox() or similar calls to debug DllMain -- actually use the debugger, or use OutputDebugString() to display messages.

    In addition, use a tool such as DependencyWalker to see exactly what the difference is between the two DLL's in terms of the dependencies. Both DLLs should theoretically be exactly the same as far as what other modules the DLL is dependent on.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 11th, 2013 at 11:13 AM.

  6. #6
    Join Date
    Jun 2012
    Posts
    58

    Re: wired dll locating problem

    You guys were right: the problem was with dependencies.


    Apparently MinGW was silently updated, adding an additional sub-sub-dependency. I checked for that but somhow missed it. Sorry about that and thanks for your time!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured