LoadLibrary failed at 2nd time
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: LoadLibrary failed at 2nd time

  1. #1
    Join Date
    May 2002
    Location
    China
    Posts
    22

    Exclamation LoadLibrary failed at 2nd time

    I have a dll named as "hello.dll", which created under Cygwin(gcc 2.95)
    by this way: gcc -shared -o hello.dll hello.o

    then I load the dll in Visual C++ program, At the first time, LoadLibrary() successfully,
    and I can call the function in the dll, at last I called FreeLibrary(),
    But when I called LoadLibrary() the second time, the program crashed.

    I don't konw why and how to solve the problem,
    Thanks for any direction!

  2. #2
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    2,147

    Re: LoadLibrary failed at 2nd time

    FreeLibrary should be returning non-zero if successful - does it?

    If not, perhaps the hModule you're storeing is not correct.

    You should call GetLastError if LoadLibrary fails. This should indicate the source of the problem.

    Also you could try loading, unloading then loading again before calling the DLL functions. If it is only after calling the DLL function that you see the problem, then the problem is within the DLL, not your calling application.

    You're not doing any of this loading within InitInstance or ExitInstance are you? If so, this could be the problem (can't load or unload DLLs there)...

    Hope this helps,

    - Nigel

  3. #3
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: LoadLibrary failed at 2nd time

    You can use GetLastError() and check against the error code table to understand what goes wrong.

    http://msdn.microsoft.com/library/de...rror_codes.asp

  4. #4
    Join Date
    May 2002
    Location
    China
    Posts
    22

    Lightbulb Re: LoadLibrary failed at 2nd time

    I've tried as the following:

    try
    {
    hd = LoadLibrary("hello.dll");
    if (hd)
    {
    (..)aa = GetProcAddress(...);
    aa(); //successful
    int a = FreeLibrary(hd); //a is not-zero
    }
    }
    catch(...)
    {
    int a = GetLastError();
    }

    //but at the 2nd time calling loadlibrary(), the program crashed, never come into catch()

  5. #5
    Join Date
    May 2002
    Location
    China
    Posts
    22

    Lightbulb Re: LoadLibrary failed at 2nd time

    I've tried as the following:

    try
    {
    hd = LoadLibrary("hello.dll");
    if (hd)
    {
    (..)aa = GetProcAddress(...);
    aa(); //successful
    int a = FreeLibrary(hd); //a is not-zero
    }
    }
    catch(...)
    {
    int a = GetLastError();
    }

    //but at the 2nd time calling loadlibrary(), the program crashed, never come into catch()

  6. #6
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: LoadLibrary failed at 2nd time

    LoadLibrary doesn't throw any exception, so the catch block will never be reached.

    Regarding your problem: what else does your application do between the 2 LoadLibrary calls?

    PS: use code tags as it is easier to read your post.
    Har Har

  7. #7
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647

    Re: LoadLibrary failed at 2nd time

    The LoadLibrary() function doesn't throw any exceptions when an error occurs.
    If its return value is NULL, then you should call GetLastError() to get an error code.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  8. #8
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: LoadLibrary failed at 2nd time

    In other words, call GetLastError() right after LoadLibrary().

  9. #9
    Join Date
    May 2002
    Location
    China
    Posts
    22

    Arrow LoadLibrary() then crash

    LoadLibrary() the 2nd time, then the program will crash.
    GetLastError() right after LoadLibrary() will useless.

    then I can't get any error code

    the program and the dll are both simple

    CXXX::OnBtnAA()
    {
    LoadLibrary();
    ...
    FreeLibrary();
    }

    when I clicked the button the 2nd time, the programe crashed after LoadLibrary()

    Notice: the dll is created under Cygwin/gcc;
    gcc -shared -o hello.dll hello.o

  10. #10
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: LoadLibrary failed at 2nd time

    Have you debugged your app to see if the crash occurs in the DLL or in the application?
    Har Har

  11. #11
    Join Date
    May 2002
    Location
    China
    Posts
    22

    Arrow the 2nd time of LoadLibrary() then crash

    LoadLibrary() the 2nd time, then the program will crash.
    GetLastError() right after LoadLibrary() will useless.

    then I can't get any error code

    the program and the dll are both simple

    CXXX::OnBtnAA()
    {
    LoadLibrary();
    ...
    FreeLibrary();
    }

    when I clicked the button the 2nd time, the programe crashed after LoadLibrary()

    Notice: the dll is created under Cygwin/gcc;
    gcc -shared -o hello.dll hello.o

  12. #12
    Join Date
    May 2002
    Location
    China
    Posts
    22

    Arrow info in call stack window

    the 2nd time when the program stop at the line of "LoadLibrary()"
    then I press F11,
    one error message popped up "The thread 0x**** has exited with code 1"

    and the info in call stack window is that:
    NTDLL! ****
    KERNEL32! ****
    CYGWIN1! ****
    KERNEL32! ****

  13. #13
    Join Date
    May 2004
    Location
    Germany
    Posts
    655

    Re: LoadLibrary failed at 2nd time

    Quote Originally Posted by NigelQ
    You're not doing any of this loading within InitInstance or ExitInstance are you? If so, this could be the problem (can't load or unload DLLs there)...
    why you can't load/freelibrary in Init/exitInstance of your CWinApp/Thread derived classes?
    it if for example a normal (and only suitable) way fpr loading/freeing resource dlls... so why yo you think one can't load/freelibrary dlls in init/exitinstance?


    @astar:
    why do you want to load/freelibrary the dll in a OnXXX Handler? why you don't load it on OnCreate and free it in OnDestroy or in the constructor/destructor?
    why such overhead?
    (LoadLibrary maps the desired dll (or at least tries it) in the processes adress space and FreeLibrary unmaps it from the processes adress space, so every time the handler is called windows has to map and unmap the dll)
    there are 10 kinds of people. those who understand binary and those who don't...

    rate a post if you find it usefull, thx
    check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/

  14. #14
    Join Date
    May 2004
    Location
    Germany
    Posts
    655

    Re: LoadLibrary failed at 2nd time

    what are you doing in your dlls DllMain Function? maybe there is a problem... (LoadLibrary calls the DllMain function)
    there are 10 kinds of people. those who understand binary and those who don't...

    rate a post if you find it usefull, thx
    check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/

  15. #15
    Join Date
    Apr 1999
    Posts
    27,434

    Re: LoadLibrary failed at 2nd time

    Quote Originally Posted by astar
    But when I called LoadLibrary() the second time, the program crashed.

    I don't konw why and how to solve the problem,
    Thanks for any direction!
    So you are saying that this fails?
    Code:
    #include <windows.h>
    
    int main()
    {
       HMODULE hMod;
       for ( int i = 0; i < 2; ++i )
       {
            hMod = LoadLibrary("hello.dll");
            if ( hMod )
                 FreeLibrary( hMod );
       }
    }
    Does this code work (it calls LoadLibrary/FreeLibrary 2 times)? If it does, the problem is *not* just calling "FreeLibrary" a second time. So you should confirm with a very simple program first to make sure the problem is what you think it is. If it does work, then your program is causing some other bug that is affecting the call to FreeLibrary. In other words, FreeLibrary crashing is a symptom, and not the cause of the problem.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 16th, 2004 at 05:39 AM.

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center