INF: Some Windows Functions Cannot Be Called from LibMain

ID Number: Q74288

3.00 3.10

WINDOWS

Summary:

In the Microsoft Windows graphical environment, when a dynamic-link

library (DLL) is implicitly linked to an application, the LibMain

function in the DLL should not call any dialog box, messaging, or

window management functions. Making such a call causes a FatalExit

error under the debugging version of Windows because the application

has not created a message queue when the LibMain function for an

implicitly loaded DLL executes.

More Information:

Every Windows DLL must include an automatic initialization function,

which is typically called LibMain. Windows calls this function when

the DLL is initially loaded, giving the DLL an opportunity to perform

one-time startup processing.

This startup processing typically includes registering window classes,

initializing the DLL's local heap, and setting initial values for the

DLL's global variables. However, a DLL's LibMain function cannot

safely make many Windows calls.

When Windows loads an application that is implicitly linked to a DLL,

it also loads the DLL on behalf of the application. Windows loads the

DLL and calls the DLL's LibMain function before calling the

application's WinMain function and before creating the application's

message queue.

While the DLL is executing, it uses certain system resources that are

owned by the application. One of these resources is the application's

message queue. If the application's message queue has not been

created, the DLL cannot do any messaging or tasking (because these

operations require a message queue). In other words, the DLL cannot

call any Windows functions that would send a message or yield control

to another application. Attempting to do so causes a FatalExit error.

Because the DLL's LibMain function may be called before the

application's message queue is created, the LibMain function cannot do

any messaging or tasking. Such calls include the dialog box, messaging

and window management functions as well as other functions that could

result in a message being sent.

Additional reference words: 3.00 3.10 linking