How could I create a custom windows message?

时间:2023-12-10 16:27:32

[问题]

Our project is running on Windows CE 6.0 and is written in C++ . We have some problems with the code , and we are unable to debug . We also found out that if in our application we create threads and try to printf from them , the output won't appear . The only output that will appear is the one from the main thread . I would like to do the following :

  • create a custom windows message
  • use as it's WPARAM the address of a char* I want to show on the screen
  • use as it's LPARAM the length of the char* I want to show on the screen
  • send the message
  • process it when it comes , so that it prints the char*

How could I create the custom windows message ? What are the types of WPARAM and LPARAM ? Is it possible to do what I just wrote ?

Thanks

【答案】

It's certainly possible to do what you describe. You don't need to actually do anything to create a custom message for communication within your application: just make sure that the code that sends the message and the code that receives the message agree on what the message number actually is, and use a message number that doesn't overlap with any of the numbers Windows uses. There is a RegisterWindowMessage() function, but that's only needed to get a message number that's unique across the entire operating system, so used for inter-process communication.

The simplest way to achieve this is to just have a header file somewhere containing your custom message numbers, starting with WM_USER and numbering upwards, like so:

#define WM_FIRST_CUSTOM_MSG (WM_USER+0)

#define WM_SECOND_CUSTOM_MSG (WM_USER+1)

The WPARAM and LPARAM types are defined when you include "windows.h", so can have different types on different systems. For 32-bit operating systems, they are both usually 32-bit integers. If you're just using the message for testing purposes, that's usually good enough, and you can stick whatever you want in there. For production code, though, you should be more careful: WPARAM is really for "integer-like" data, and LPARAM for "pointer-like" data. In Win64, for example, LPARAM is long enough to hold a 64-bit pointer, but WPARAM only holds a 32-bit integer. For passing more data than just an integer and a pointer, I'd use lParam to pass a pointer to some sort of structure containing all my arguments.

Having said all that, it sounds like a complicated way of getting debugging output. Have you tried using the OutputDebugString() API call? Or debugging the thread's printf() call?

 

From: https://*.com/questions/293723/how-could-i-create-a-custom-windows-message