How do I know when dllmain is finished?

I have a job that I need to populate in a DLL as soon as it is loaded. The job involves syncing and therefore cannot be done inside dllmain. Is there a way to get the code to run as soon as the dllmain (or all dllmains) have finished?

+2


a source to share


3 answers


According to this MSDN post :

During process startup and DLL initialization procedures, new threads can be created, but they do not start until DLL initialization is done for the process.

If this is true, then you should be able to do your work on the topic in question, which does not start until DLLMain finishes. Of course, this leaves some of the timing issues open. If you are using Mutex, maybe you can fix this problem.



NOTE. I haven't tried this, it just looks like it could theoretically work.

PS If you try, please ask for a comment on whether it works or not.

+3


a source


From my point of view, this has long been a problem. If the DLL is being used by third party applications that you have no control over, then it is difficult to get other applications to call some initialization function. Ultimately this may be a requirement, but it is certainly a good idea not to do so in order to use a DLL (like winsock initialization).



If an initialization call is not possible, it is likely that you need to rely on lazy initialization, which happens on demand. I came across some pretty decent paper on DLL Best Practices that might be worth reading. It has a nice list of specific things you can and cannot do inside DLLMain. I know from experience that they need to be adhered to (the list is "not necessary").

+2


a source


The easiest way is to put all the code in another function that you call after loading the library.

You can also create a thread that does the job, but I'm not sure exactly what you are trying to do.

0


a source







All Articles