Actually I have 2 questions:

1. Is it normal to load a DLL that is already loaded?

EXE: ... HMODULE hm=LoadLibrary(L"DLL_1.dll"); typedef void(*func)(int); func Func = (func)GetProcAddress(hm, "Func"); LoadLibrary(L"DLL_2.dll"); ... DLL_1: ... extern "C" _declspec(dllexport) void Func(int); ... DLL_2: ... Мне нужно использовать функцию из DLL_1. Cнова вызывать LoadLibrary()? ... 

2. When should a DLL be unloaded?

Can I use functions from the library after I called FreeLibrary() ?

PS: The reason for asking the question is an exception in my program. It is not born immediately, after a large number of code calls, where the described situation occurs (in длл loaded without being supported, and also the loaded длл are called). Exception in msctf.dll , write error at 0x00000005. Googling, there are similar problems - everything. they write that the exception is born when they load the library. Here is a similar question - https://social.msdn.microsoft.com/Forums/vstudio/en-US/f74eec60-c215-42b8-b3ff-4591870d5a87/msctfdll-exception?forum=vsdebug

  • Advanced optimization? - PinkTux

2 answers 2

Is it normal to load a DLL that is already loaded?

Although it will not cause an error, but in terms of good code, it is not very normal. In your code there should be one point of initialization of the library and its functions, and one point of unloading it from memory. You must arrange for yourself to check whether you have downloaded this library or not.

Can I use functions from the library after I called FreeLibrary ()?

No way. Moreover, when you unload a library from memory, you must reset all function pointers from this library to be sure that no part of the code will try to use them later.

As an example, how to organize the library initialization, I can show the code, though in Delphi - libpng.pas . Some explanations:

  • external code can pull InitLibPng number of times, the library will be loaded only once, on the very first call of this function;
  • The library is unloaded (in FinLibPng ) in the same way once the application is closed (and the unloading is done automatically, because this function is not available for outside calls, but is called in the finalization section, but these are Delphi features). In C, you can count the number of calls to the load function (increment the global counter) and the release functions (reduce the counter) and unload only when the counter reaches zero.

On the other hand, in the world of OOP (Object Oriented Programming), the same library can be loaded / unloaded any number of times, the main thing is that this should occur symmetrically . Those. in the constructor of some object we load the library and initialize the functions, and in the destructor, unload. I have an example of this use (again Delphi, yes) - a wrapper class over the C-shnoy library . By the way, in the pseudo OOP style, you can write in C. At least, these are the wrappers on libraries - quite.

    Loading the library again via LoadLibrary is perfectly normal. Here is what is written in MSDN :

    Calling LoadLibrary increments the reference count. Calling the FreeLibrary or FreeLibraryAndExitThread function decrements When the process terminates (regardless of the reference count).

    But with FreeLibrary is a good question. Really unloading will not be made until the counter reaches zero. But counting how many times the module has loaded is boring. Therefore, I would not build a system like this.