4

I am building a C++ program, on windows, using Visual Studio. It relies on a COM base API, that sends windows message for notification.

To process those messages, I see two possibilities:

  • Create a windows form and call doModal on it which should process the messages, but since I don't want to use any UI, it's not what I want to do
  • make my own loop for processing messages

I don't know what is best, or if there is another way to process the messages (there is probably a windows function that can launch the loop)

  while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
  { 
    if (bRet == -1)
    {
      // handle the error and possibly exit
    }
    else
    {
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
    }
  } 
0x26res
  • 11,925
  • 11
  • 54
  • 108
  • 1
    This is exactly what natured intended you to do. – David Heffernan Mar 03 '11 at 15:50
  • Yes, your code is a model message loop. – Jon Mar 03 '11 at 15:51
  • Ok, but isn't there another way to do it ? Also I can I notify the queue to stop ? – 0x26res Mar 03 '11 at 16:16
  • 1
    PostQuitMessage (http://msdn.microsoft.com/en-us/library/ms644945(VS.85).aspx) should be called to exit that message loop. When GetMessage process WM_QUIT it returns 0. – Ismael Mar 03 '11 at 16:30
  • Ok, and then let's say I start this queue in a thread, and I want to send a message to this queue from another thread. What thread id should I provide in my `PostThreadMessage` – 0x26res Mar 04 '11 at 11:40

2 Answers2

4

Yes, you can. Every thread can have one message loop and you don't need any windows to receive messages or send them (see PostThreadMessage).

There is nothing wrong with using this method if your application is event-driven.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
4

It is not just for your own benefit, COM requires you to create a message loop. COM needs it to handle apartment threaded COM servers, an expensive word for "components that don't support multi-threading". The vast majority of them don't.

It is best to create a window, it doesn't have to be visible. That gives you a HWND that you can use in your SendMessage() calls. The window procedure you write can process the messages. From there, it gets to be easy to create a minimal user interface, with Shell_NotifyIcon for example. Always nice when you can display a notification when something goes wrong. So much better then an event in a log that nobody ever looks at.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Is it possible to do that without having to create a whole MFC application project ? I mean just by creating a concole application – 0x26res Mar 04 '11 at 16:25
  • Yes, easily. You really ought to pick up a copy of Petzold's "Programming Windows" if you want to do Windows programming. Second best is to create a project with the Win32 Project template. It auto-generates the boilerplate code for a very simple but functional GUI app. All you have to do is comment out the ShowWindow() and UpdateWindow() calls since you don't want to make the window visible. – Hans Passant Mar 04 '11 at 16:38