tags:

views:

140

answers:

3

CoInitialize(NULL) creates an STA by creating a hidden window. How to get an HWND handle for this window?

Function EnumThreadWindows does not work, in an example I tried:

...

CoInitialize(NULL);

EnumThreadWindows(GetCurrentThreadId(), EnumThreadWndProc, 0);

...

BOOL CALLBACK EnumThreadWndProc(HWND hwnd, LPARAM lParam)
{
   m_hwnd = hwnd;

   return FALSE;
}

Nothing ever enters the EnumThreadWndProc.

Any ideas?

+2  A: 

This hidden window is Message-Only Window, It is not visible, has no z-order, cannot be enumerated, and does not receive broadcast messages. The window simply dispatches messages.

To find message-only windows, specify HWND_MESSAGE in the hwndParent parameter of the FindWindowEx function. In addition, FindWindowEx searches message-only windows as well as top-level windows if both the hwndParent and hwndChildAfter parameters are NULL.

Source: MSDN

Bahaa Zaid
A: 

Thank you, BZ!

You are welcome :)
Bahaa Zaid
+1  A: 

Btw, I would be VERY careful here - you really shouldn't be sending window messages to windows you don't own. Your code is highly likely to break in a future version of Windows.

Larry Osterman