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

Hybrid View

  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
    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()

  4. #4
    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!"

  5. #5
    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

  6. #6
    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/

  7. #7
    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/

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

    Arrow Re: LoadLibrary failed at 2nd time

    Quote Originally Posted by bigBA
    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)


    When I clicked the button, I want to call the functions in the DLL. If I put load/free into constructor/destructor, the variable in DLL will remain the value at the last time be called, which result to call the functions in the DLL in 2nd time, 3rd time ... failed. So I need unmap everything in the memory before I call it, could you tell me how to? Thanks

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

    Re: LoadLibrary failed at 2nd time

    hm.. maybe i don't really get your point...
    just for making clear what you want to achieve:
    - you have a dll
    - the dll exports a function
    - which returns a value of a global variable in the dll (????)

    what kind of variable or value does the function return? (can you post the code?)

    but if you load the dll everytime the handler is called(and in case that you only have one of this button in your app, the dll gets mapped and unmapped every time the handler is called... hm... no... it does get mapped/unmaped every time, also if you have more of these buttons (because your app isn't multithreaded, is it?)........

    back to the variable and the value... every time your handler gets called, the dll gets mapped, and so the variable with its initial value. i really don't know what you do in your function... but if you just return this variable, it will have the same value (it also will, if you doesn't modify its contents...) every time you call the function.
    also if you modify the variable, right after the modify your dll (and so the variable) gets unmapped from adress space. and the next time your handler gets called the dll gets mapped again (with the variable, with its initial value.......)

    hm... no idea.

    please post the code of this function and the mentioned variables.
    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/

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: LoadLibrary failed at 2nd time

    [ Merged threads ]

  11. #11
    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

  12. #12
    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()

  13. #13
    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

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

    Re: LoadLibrary failed at 2nd time

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

  15. #15
    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

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
  •  





Click Here to Expand Forum to Full Width

Featured