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 08: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[116858]
2D Rotated Rectangles Collision Detection[88948]
Keyboard Hook[77246]
UDP[65800]
HTTP Proxy[41189]

::Top 5 Samples::
2D ColDet Rotated Rectangles[11557]
PS2 Mouse Driver[6953]
Wave Format Player[5788]
Reading FAT12[5615]
CodeGuru[5355]


All rights reserved to RageStorm © 2009