After quite some effort, I managed to capture Windows Messages from a third party MFC dll (I asked about that here). To make it short, I had to create a Message-only window with a message loop which captured the third party dll’s messages.
Said Message-only window must remain hidden. And it does, initially, since I pass HWND_MESSAGE to CreateWindowEx and call ShowWindow with SW_HIDE.
However, my C++ dll has some callbacks into managed code. And I noticed that when I perform the user actions that trigger the first of them, a console window appears. And it doesn’t go away until I close my app.
Since the console window has my app’s executable path as its title, I thought that the window was somehow being associated to my app. So I passed NULL to CreateWindowEx’s hInstance parameter, but it didn’t work.
Here’s my Message-only window code:
DWORD WINAPI CDRTech::MessageLoopThread( void * pParams ){
HWND hwnd;
MSG mensaje;
WNDCLASSEX wincl;
const string windowClass = "DR_TECH_MESSAGE_HANDLER";
// Window class
wincl.hInstance = ::GetModuleHandle(NULL);
wincl.lpszClassName = windowClass.c_str();
wincl.lpfnWndProc = ::DefWindowProc;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = ::LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = ::LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = ::LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = ::GetSysColorBrush(COLOR_BACKGROUND);
if(!::RegisterClassEx(&wincl)){
::GetErrorLoggerInstance()->Log( LOG_TYPE_ERROR, "CDRTech", "MessageLoopThread", "Could not register Message Handling Window" );
return 0;
}
//Create Window (hidden)
hwnd = ::CreateWindowEx(
0, //Default ExStyle
windowClass.c_str(), //Window class
"DRTech", //Window Title
WS_OVERLAPPEDWINDOW, //Default Style
CW_USEDEFAULT, //Let Windows decide position
CW_USEDEFAULT,
10, //Width
10, //Height
HWND_MESSAGE, //Message-only window
NULL, //No Menu
NULL, //Handle to application
NULL //Window creation data
);
::ShowWindow( hwnd, SW_HIDE );
CDRTech* pThis = reinterpret_cast<CDRTech*>( pParams );
pThis->InitDRTechLibrary();
//Start message loop
while(TRUE == GetMessage(&mensaje, NULL, 0, 0)){
TranslateMessage(&mensaje);
DispatchMessage(&mensaje);
}
return mensaje.wParam;
}
The window you create is not related to the console window you see. Something that you call creates a console window (or you program is marked as a console application in which case the console is created when your application is launched).
Put a breakpoint at
AllocConsole()to find who is creating the console.