Home
Tutorials
Code Snippets
Code Samples
Downloads
Links

The Blog
Our Projects
About
Contact

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

 
Code Snippet - WM_USER & WM_COPYDATA
Author:Arkon
Category:Win32API
File Size:~ 1.26 KB
Uploaded at:27-Nov-02 03:12:09 pm
Description:
  An example which shows how to use WM_USER and WM_COPYDATA efficiently(?).
  
In order to message a window you need its handle and a tiny protocol to protect it.
Say someone send you a WM_USER and you just process the info in WPARAM and LPARAM
without seeing if the message is a "real" one, this can get us into alot of troubles.
For most of you this code is useless, but for some of us it still helpful and it might come handy
from my experience with DLL's and other stuff.

Note: that you can use both WM_USER and WM_COPYDATA as you want this is how I use them.


To overcome this problem,
we can use WPARAM for the structure pointer and LPARAM for the message code or vice versa.
IE:
Client:
SendMessage(hWnd, WM_USER + XXX, (WPARAM)&strct, MYCODE);

Server:
LPMYSTRUCT strct = (LPMYSTRUCT)wParam; // This is the unsafe way.
You better do:
if (lParam != MYCODE) return(0); // Don't process the message for safety purposes.
Use sent data..
return(0);

For the same idea you can use WM_COPYDATA

Client:
COPYDATASTRUCT cds;
cds.cwData = MYCODE;
cds.cbData = sizeof(MYSTRUCT);
memcpy(cds.lpData, &strct, sizeof(MYSTRUCT));
SendMessage(hWnd, WM_COPYDATA, hClientWnd, (LPARAM)(COPYDATASTRUCT*)&cds);

Server:
case WM_COPYDATA:
LPMYSTRUCT strct = (LPMYSTRUCT)lParam;
if (strct.cwData != MYCODE) return(0);
Use sent data..
return(0);
NOTE:
Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
Name:
email:
Comment:
::Top 5 Tutorials::
Embedded Python[105776]
2D Rotated Rectangles Collision Detection[79024]
Keyboard Hook[68594]
UDP[52667]
HTTP Proxy[35944]

::Top 5 Samples::
2D ColDet Rotated Rectangles[10589]
PS2 Mouse Driver[6083]
Wave Format Player[4968]
Reading FAT12[4855]
CodeGuru[4543]


All rights reserved to RageStorm © 2009