Question

If I load a DLL with LoadLibrary, is that DLL guaranteed to stay loaded? If not, how can I prevent re-loading.

Actual situation: I have a dispatcher which, depending on some messages, needs to load one of several dll's and execute some function from them. I can't link against them at compile time, so I use LoadLibrary. Because there can potentially be a lot of calls, I'd prefer not to have to call LoadLibrary every time, since it turns out it's a bottleneck. So I was thinking to only call it once per DLL, calling GetProcAddress to get the function also only once per dll, and cache it somewhere. But is it safe? Am I guaranteed that calling that function will be ok on any subsequent call? If not, how can I have this guarantee?

Était-ce utile?

La solution

LoadLibrary increases the reference count of the executable, and FreeLibrary decreases it.

When the reference count reach 0, the executable is unloaded. So you normally don't have to worry about it. As long as no one calls FreeLibrary in your process, the Dll will stay there.

Autres conseils

If you read the MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/ms684175%28v=vs.85%29.aspx it states what the behaviour will be if the dll is not already loaded and what happens if the dll is already loaded so you should not worry about this overhead.

If the specified module is a DLL that is not already loaded for the calling process, the system calls the DLL's DllMain function with the DLL_PROCESS_ATTACH value. If DllMain returns TRUE, LoadLibrary returns a handle to the module.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top