How to create a DLL initialization procedure?

In the DllMain Entry Point documentation, the author makes the following comment:

To provide more complex initialization, create an initialization procedure for the DLL. You can require applications to invoke the initialization procedure before calling any other routines in the DLL.

In C / C ++, how do I create another procedure and require the application to call it before any other?

+4
source share
3 answers

The initialization procedure can be any exported function. The trick is that "other applications need to call it." To enforce it, you will need to check to see if it was called in almost any other exported function. If each exported function has some common prefix code, this would be a good place to check if the initialization function was called.

If, however, you need to check whether it was called at each entry point, it may be easier for DLL consumers if you actually call this function automatically if it was not called. This requires some extra work to make it thread safe. You will need a critical section (or mutex, semaphore, etc.) to ensure that it is called only once.

+2
source

The canonical example of this is InitCommonControlsEx() . You must have your DLL users invoke your initialization and termination routines.

+2
source

Source: https://habr.com/ru/post/1345797/


All Articles