首先,参看这篇文章:

http://msdn.microsoft.com/en-us/library/windows/desktop/dn633971%28v=vs.85%29.aspx


loadlibrary时,会先加锁,然后调用dllmain,然后解锁返回。

所以dllmain中,要做的初始化或清理越简单越好。该做的复杂的初始化,应该延迟,在loadlibrary之后。


不能做的事:

LoadLibrary 

CreateThread

CreateProcess

GetModuleFileName

就是少用Windows API。


Use the memory management function from the dynamic C Run-Time (CRT) ? 这个是指什么


如果一定要在其中初始化,可以这样异步:

One of the approaches I’ve used has been to make use of Win32 asynchronous procedure calls. Specifically you can call QueueUserAPC to add a function to the queue, and this can contain the initialisation you would’ve otherwise done in DllMain.

However there is significant gotcha regarding use of APCs: your function will not be called until the thread is in an “alertable wait state”. This means you (or some other code on the thread) need to call an alertable wait function such as SleepEx, WaitForSingleObjectEx and specify TRUE for the alertable parameter.