Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WM_USER vs WM_APP

Tags:

c

winapi

I want the worker threads to send a user-defined message to the UI thread message queue, but I am not sure if I should use WM_USER or WM_APP. The documentation for WM_APP says:

WM_APP through 0xBFFF

Messages available for use by applications.

So should I use WM_APP?

like image 751
Tom Avatar asked Jun 15 '15 11:06

Tom


2 Answers

Microsoft is really conservative on its APIs, so you can be confident when you see that messages WM_APP through 0xBFFF do not conflict with system messages. It would need a major change in Windows API for that rule to be broken, and many other applications would not survive.

The only relevant question is : do you need to use messages in the WM_USER range or in the WM_APP range?

MSDN says:

Message numbers in the second range (WM_USER through 0x7FFF) can be defined and used by an application to send messages within a private window class. These values cannot be used to define messages that are meaningful throughout an application because some predefined window classes already define values in this range. For example, predefined control classes such as BUTTON, EDIT, LISTBOX, and COMBOBOX may use these values. Messages in this range should not be sent to other applications unless the applications have been designed to exchange messages and to attach the same meaning to the message numbers.

Message numbers in the third range (0x8000 through 0xBFFF) are available for applications to use as private messages. Messages in this range do not conflict with system messages.

(emphasis mine)

If you explicitly post those messages to a window that is designed to handle them in a specific way and is not a subclass of a Windows control, you can use WM_USER range. If they are to be handled directly by the message loop (like WM_QUIT for example), or if in doubt, use the WP_APP range.

Said differently, as you do not need a lot of such messages and as you want to post them to the UI thread message queue, simply use one in the WM_APP range that is not already used by your application, and be sure to document it for later maintenance.

like image 159
Serge Ballesta Avatar answered Nov 05 '22 23:11

Serge Ballesta


If you completely control the window class of the destination window (i.e. you defined it, you're not subclassing/superclassing another class, and you don't use IsDialogMessage on your window) then you can use WM_USER+xxx (with x >= 0).

Otherwise, you should use at least WM_APP+xxx, provided you control the application that contains the window.

Failing that, the only option left would be RegisterWindowMessage().

like image 30
Medinoc Avatar answered Nov 06 '22 01:11

Medinoc