Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a DLL initialization routine?

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

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

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

like image 677
MikeRand Avatar asked Mar 29 '11 22:03

MikeRand


1 Answers

The initialization routine can be any exported function. The trick is the "requiring other applications to call it". To enforce it, you would need to check if it had been called in pretty much every other exported function. If each exported function has some common prefix code, that would be a good place to check if the initialization function had been called.

If, though, you have to check if it has been called in every single entry point, it might be easier on the consumers of the DLL if you actually call that function automatically if it has not been called. That does require some additional work in making it thread safe most likely. You would need a critical section (or mutex, semaphore, etc.) to ensure that it was only called one time.

like image 139
Mark Wilkins Avatar answered Sep 24 '22 17:09

Mark Wilkins