开发者

How can I know when dllmain is completed?

开发者 https://www.devze.com 2022-12-29 02:23 出处:网络
I have work I need to complete in a DLL as soon as it is loaded.The work involves synchronization and so开发者_如何学C can\'t be done inside dllmain.Is there a way to trigger code to execute as soon a

I have work I need to complete in a DLL as soon as it is loaded. The work involves synchronization and so开发者_如何学C can't be done inside dllmain. Is there a way to trigger code to execute as soon as dllmain (or all dllmains) is complete?


According to this MSDN post:

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

If this holds true, then you should be able to do your work in the Thread in question, which will not start until the DLLMain has completed. Of course this leaves some possible synchronization issues open, If you use a Mutex maybe you can resolve this.

NOTE: I have not tried this, it just looks like in theory it might work.

P.S. If you try it pls leave a comment on whether it worked or not.


This has long been a thorny issue from my point of view. If the DLL is being used by 3rd party applications over which you have no control, then it is difficult be able to force the other applications to call some initialization function. Ultimately, it can be a requirement, but it is certainly nice to not have to do that to use a DLL (e.g., initializing winsock).

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


The easiest way is to probably put all the code into another function which you call after the library is loaded.

You could also create a thread that does the work but I'm not sure exactly what you're trying to do.

0

精彩评论

暂无评论...
验证码 换一张
取 消