Use of LoadLibrary call

This is a discussion on Use of LoadLibrary call within the C Programming forums, part of the General Programming Boards category; I am using the following statement to load a dll in my code HINSTANCE hDLL = LoadLibrary(LibName); The library is ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    15

    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?

    Thanks

    pk68

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,675
    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
    Posts
    435
    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, 10:18 AM
  2. loadlibrary from dll
    By DampfHans in forum Windows Programming
    Replies: 13
    Last Post: 04-07-2010, 12:41 AM
  3. How to loadlibrary from a new thread
    By allynm in forum Windows Programming
    Replies: 4
    Last Post: 04-07-2010, 12:36 AM
  4. LoadLibrary return..
    By raphael in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2008, 07:40 AM
  5. From ini to LoadLibrary
    By NoFearXD in forum Windows Programming
    Replies: 6
    Last Post: 05-30-2007, 08:08 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21