Friday, August 28, 2009

Windows Message Handling (message pump) with Console
Couldn't get away from using the Win32 App (that uses WinMain()) to get this working.
  • Inside WinMain - CreateWindow with default style and options but don't ShowWindow. Register your WNDCLASS and WndProc using RegisterClass()
  • AllocConsole() - this is the key. This opens a console window and attaches it to the app
  • GetStdHandle() - Get the console handle for use in WriteConsole() and ReadConsole()
  • Run the usual GetMessage - TranslateMessage - DispatchMessage loop in WinMain (probably the last lines of code)
  • Messages (such as WM_CREATE, WM_CLOSE, WM_DEVICECHANGE, etc.) are handled in the WndProc CALLBACK.
  • Console events (CTRL+C, Close, etc.) needs to be handled in ConsoleHandler() that is registered via SetConsoleCtrlHandler() call.
  • Since you don't see the "window", the close/exit event will be received in the console and it is in the ConsoleHandler() that one should generate (using PostMessage)the WM_CLOSE message for the window (which is then handled in the WndProc()).
  • Just like malloc, the AllocConsole() has a corresponding FreeConsole(). It's logical place is in the WM_CLOSE message handler.
See also:

No comments:

Post a Comment