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); |