Microsoft KB Archive/118611

= INFO: SetMessageQueue Used in MFC WinMain =

Article ID: 118611

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q118611

<div class="notice_section">

<div class="summary_section">

SUMMARY
Functions that use the applications message queue do not work correctly if they are called in the constructor of a global or static object. SetMessageQueue is called in the initialization code at the beginning of the MFC WinMain function. SetMessageQueue replaces the current message queue with a new message queue. The constructors of global or static C++ objects are called before WinMain. If these constructors call functions that rely on the message queue, the objects do not work properly.

<div class="moreinformation_section">

MORE INFORMATION
MFC for Windows, version 2.5, calls SetMessageQueue in the AfxWinInit function, which is called by the MFC WinMain function. The constructors for global and static objects are called before WinMain. These constructors should not call functions that use the applications message queue or rely on the message queue remaining the same after the constructor is executed. These functions include:


 * Creating a window
 * Setting a timer (using SetTimer)
 * Setting a hook (using SetWindowsHook)
 * Posting a message

If any one of these things is done in the constructor and is not processed or removed from the application's message queue before the constructor exits, it will not work later in the application. To work around this, initialize each of these objects in the CWinApp::InitInstance function: Define an initialization member function for the object and call it in the CWinApp::InitInstance function.

Additional query words: kbinf 1.50 2.00 2.10 2.50 2.51 2.52 3.00 3.10

Keywords: kbarchitecture kbinfo KB118611

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.