Skip to main content
gamedev.net
🔒 Locked

WM_APP

Started by xlad 2 replies 1k views
Original Post
xlad
xlad

I want to create multiple threads which tell windows to create a message queue for them, and then executes different things, depending on the wParam and lParam of the WM_APP messages that they receive (which are sent between them).

If I use WM_APP, all the threads that communicate between them will receive the messages, but if other application send WM_APP to the threads, they will receive them?

I want to make sure that if a random application starts broadcasting random messages, my application won't have problems with them.
For example, a thread executes something if it receives the WM_APP message with wParam == 1 and lParam == (Some pointer which contain data). If another application sends the message WM_APP with wParam == 1 and lParam = 3 (an unwanted value), will my application receive it (and obviously crash)?

If so, how i can make my threads so that they do not have this kind of problems?

I've searched through Google and through the forum about this and found nothing.

void ThreadProc(void) // a thread's procedure{ PeekMessage(&msg,0,WM_APP,WM_APP,PM_NOREMOVE); //make sure windows creates the message queue while (1)  {   if (GetMessage(&msg,0,0,0))     {      if (msg.message == WM_APP)        {         if (msg.wParam == 0)           {            return 0; //we do not need the thread anymore           }          else           {            //Do something           }        }     }  }}
Endurion
Endurion
There's not too much you can do to prevent these messages.

You can however put something into the parameters (a magic number) and discard messages without them.

If you want to be completely sure use RegisterWindowMessage. It still won't stop other apps to be able to send you a message with the same value though.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
xlad
xlad
At the moment they are already implemented with a "magic number", because it seems the best solution I have found yet.

I was also thinking about making my own queue through linked lists, but it will make the program very slow if there would be lots of critical sections (without them, it wouldn't work).

I want these messages to be sent only through my application, and no other application should be able to send a message to my app. Maybe there is a way to find out which process sent the message? (for example, if i make a game, i do not want a cheater to send false data to the AI thread for example)

Is there is a better solution (even without using Get/Peek Message) ?
empirical2
empirical2
I cant see a way to determine the legitimacy of the sender.

If the messages are for private use only, perhaps your game could randomly define a 'base' at start up.

you would then do

const int WM_MYMESSAGE1 WM_USER
const int WM_MYMESSAGE2 WM_USER+1...
unsigned int MsgBase=0;
...
MsgBase=Rnd(Something)
...
SendThreadMessage(WM_MYMESSAGE+MsgBase....)

(Obviously subtract MsgBase when intercepting messages > WM_USER.

A cheater sending messages would end up with all sort of problems!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.