INTERNAL: Problems with initilizing callable IDL under Windows
Topic:
When initilizing IDL from DllMain in a Windows DLL, sometimes the call to IDL_Win32Init() causes a crash or it hangs indefinitely under Windows.Discussion:
This unpredictable behavior is caused by the Windows API. The reason is that the windows DllMain function does not allow calling LoadLibrary() or FreeLibrary(). Since IDL_Win32Init() will call LoadLibrary() to load idl32.dll this function can not be safely called from DllMain().
The correct solution is to make a separate function in the DLL that initializes IDL by calling IDL_Win32Init().
== Excerpts from documentation on DllMain. ==
== See MSDN documentation for full details. ==
The DllMain function is an optional method of entry into a dynamic-link library (DLL). If the function is used, it is called by the system when processes and threads are initialized and terminated, or upon calls to the LoadLibrary and FreeLibrary functions.
...
Warning On attach, the body of your DLL entry-point function should perform only simple initialization tasks, such as setting up thread local storage (TLS), creating synchronization objects, and opening files. You must not call
LoadLibrary in the entry-point function, because you may create dependency loops in the DLL load order. This can result in a DLL being used before the system has executed its initialization code. Similarly, you must not call the
FreeLibrary function in the entry-point function on detach, because this can result in a DLL being used after the system has executed its termination code.
Calling Win32 functions other than TLS, synchronization, and file functions may result in problems that are difficult to diagnose. For example, calling User, Shell, COM, RPC, and Windows Sockets functions (or any functions that call these functions) can cause access violation errors, because their DLLs call LoadLibrary to load other system components.
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. Otherwise, you can have the initialization routine create a named mutex, and have each routine in the DLL call the initialization routine if the mutex does not exist.
Solution:
[Edit this field in the IDL-based Tech Tip Editor, v3]