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