Thread: Use of LoadLibrary call

  1. #1
    Registered User
    Join Date
    Nov 2010

    Use of LoadLibrary call

    I am using the following statement to load a dll in my code

    HINSTANCE hDLL = LoadLibrary(LibName);

    The library is being loaded and works as expected. My questions are

    1. What will happen if I try to load the same library multiple times?
    2. Is there a function call to check if the library is loaded to avoid multiple calls to LoadLibrary?



  2. #2
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    Microsoft has great documentation on their MSDN site. Googling for "msdn LoadLibrary" turns up the following: LoadLibrary function. That has a lot of links to related stuff. Clicking around for all of 30 seconds turned up this function: GetModuleHandle function, which looks promising.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    1) Why would you try to load the same library multiple times (surely you know what you're program is loading and what it isn't?)?
    2) Can't you just track it yourself anyway and make sure you don't? What are we talking about here? 5, maybe 10 lines of code even if you don't know about GetModuleHandle etc.?
    3) Why don't you try it?

    I think it just increments a "usage" counter on the DLL, rather than loads it in again. When the DLL needs to be unloaded, it decrements that counter and if it becomes 0, it knows that it can unload the DLL from memory. So loading multiple times is unlikely to do anything particularly nasty but it might be that a DLL stays loaded longer than necessary (I would imagine that quitting your program resets the count anyway). You can check this by loading it "twice", seeing what happens, and seeing if the function pointers returned from GetProcAddress are the same (i.e. if both "instances" of the library return the same function pointers for the same function, then it didn't load it twice, it just knew to use the same in-memory copy of the DLL).

    To be honest, though, this is the sort of thing you should be able to answer (or workaround) for yourself. A two-second google about DLL programming would have popped this information up for you.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading dll with LoadLibrary
    By g4j31a5 in forum Windows Programming
    Replies: 1
    Last Post: 07-14-2010, 09:18 AM
  2. loadlibrary from dll
    By DampfHans in forum Windows Programming
    Replies: 13
    Last Post: 04-06-2010, 11:41 PM
  3. How to loadlibrary from a new thread
    By allynm in forum Windows Programming
    Replies: 4
    Last Post: 04-06-2010, 11:36 PM
  4. LoadLibrary return..
    By raphael in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2008, 06:40 AM
  5. From ini to LoadLibrary
    By NoFearXD in forum Windows Programming
    Replies: 6
    Last Post: 05-30-2007, 07:08 PM

Tags for this Thread