Can I use my own message loop in windows?

I am creating a program in C ++ on Windows using Visual Studio. It is based on the base COM API, which sends a Windows message for notification.

To process these messages, I see two possibilities:

  • Create a Windows form and call doModal on it, which should handle messages, but since I donโ€™t want to use any user interface, this is not what I want to do
  • create your own message processing loop

I donโ€™t know which is better, or if there is another way to handle messages (maybe there is a windows function that can run a loop)

while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0) { if (bRet == -1) { // handle the error and possibly exit } else { TranslateMessage(&msg); DispatchMessage(&msg); } } 
+4
source share
2 answers

This is not only for your own benefit, COM requires that you create a message loop. COM requires it to handle threaded COM servers, an expensive word for "components that do not support multithreading." The vast majority of them do not.

Itโ€™s best to create a window; it should not be visible. This gives you an HWND that you can use in your SendMessage () calls. The window procedure you are writing can process messages. From there it is easy to create a minimal user interface, for example, using Shell_NotifyIcon. It is always nice when you can display a notification when something goes wrong. So much better than an event in a magazine that no one ever looks at.

+4
source

Yes, you can. Each thread can have one message loop, and you do not 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.

+2
source

Source: https://habr.com/ru/post/1342146/


All Articles