MikeRand
MikeRand

Reputation: 4838

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?

Upvotes: 5

Views: 6023

Answers (3)

David Heffernan
David Heffernan

Reputation: 613332

The canonical example of this is InitCommonControlsEx(). You have to make the users of your DLL call your initialization and finalization routines in just the same way.

Upvotes: 2

Mark Wilkins
Mark Wilkins

Reputation: 41252

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.

Upvotes: 3

bmargulies
bmargulies

Reputation: 100153

Here's an example:

http://zone.ni.com/devzone/cda/tut/p/id/4877

Upvotes: 0

Related Questions