Originally Posted by
Crazed
back so quick, I looked at the SendMessage and the first param HWND is pretty simple a handle which I got, then the UINT msg, now it is obvious that it takes a unsig. int as one of the predifined macros, now an onward question so I don't coplicate it all too much, how can I send some text to another program, cuz I can't quite visualize it with the basic SendMessage or are there any secrets in the WPARAM and LPARAM?
How about using InterProcess Communications (IPC)? An example follows. Start the server first.
Code:
//Common.h
#pragma comment( lib, "user32.lib" )
#include <stdio.h>
#include <windows.h>
#ifndef __MYIPC__
#define __MYIPC__
#define MAX_MESSAGE_SIZE 0x10000
#define HEADER_SIZE (2 * sizeof(DWORD) + sizeof(HWND))
#define SERVERWNDCLASSNAME "MYSERVERWND001"
#define WINSERVERNAME "MYSERVER001"
#define SETSTRINGLEN(szInputString,iIndex) ((szInputString)[(iIndex)]='\0')
#define WM_SEND_DATA WM_APP
#define WM_SHUTDOWN_SERVER WM_APP + 1
typedef struct _MYDATASTRUCTURE
{
DWORD cbSize;
DWORD iMessage;
HWND hClient;
CHAR Data[MAX_MESSAGE_SIZE - HEADER_SIZE];
}MYDATASTRUCTURE,*LP_MYDATASTRUCTURE;
#endif __MYIPC__
Code:
//Client.cpp
#include "common.h"
HWND hServer;
DWORD pID;
BOOL WINAPI TransferData(DWORD dwMessage, const char *pBuffer, DWORD dwBytes)
{
BOOL bSend;
COPYDATASTRUCT cpStructData;
LP_MYDATASTRUCTURE lpMsg;
cpStructData.cbData = dwBytes + HEADER_SIZE;
lpMsg = (LP_MYDATASTRUCTURE)LocalAlloc(LPTR,cpStructData.cbData);
lpMsg->hClient = NULL;
lpMsg->iMessage = dwMessage;
lpMsg->cbSize = dwBytes;
cpStructData.lpData = lpMsg;
if(pBuffer!=NULL)
{
SETSTRINGLEN(lpMsg->Data,dwBytes);
memcpy((void *) lpMsg->Data,(const void*)pBuffer,dwBytes);
}
bSend = SendMessage(hServer,WM_COPYDATA, (WPARAM)hServer,(LPARAM)&cpStructData);
return(bSend);
}
int main(void)
{
CHAR szWndName[256] = {0};
CHAR szData[256] = {0};
pID = GetCurrentProcessId();
sprintf(szWndName,"PROCESSID %.X",pID);
SetConsoleTitle(szWndName);
if((hServer = FindWindow((LPCTSTR)SERVERWNDCLASSNAME,NULL)) == NULL)
printf("Please start the server!\n");
else
{
strcpy(szData, "Testing, testing one, two three!!");
TransferData(WM_SEND_DATA ,szData, strlen(szData));
Sleep(5000);
strcpy(szData, "Let's shutdown the server.");
TransferData(WM_SHUTDOWN_SERVER ,szData,strlen(szData));
}
return 0;
}
Code:
//Server.cpp
#pragma warning( disable : 4715 )
#include "common.h"
LRESULT CALLBACK WMIPCWNDPROC(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LP_MYDATASTRUCTURE lpMessage;
if(uMsg == WM_COPYDATA)
{
lpMessage = (LP_MYDATASTRUCTURE)((COPYDATASTRUCT*)lParam)->lpData;
SETSTRINGLEN(lpMessage->Data,(lpMessage->cbSize));
switch(lpMessage->iMessage)
{
case WM_SEND_DATA:
printf("\nOK, we got a message from the other program, Let's print it\n");
printf(lpMessage->Data);
break;
case WM_SHUTDOWN_SERVER:
printf("\nWell, the other program wants us to shutdown. Let's print the message and shut down\n");
printf(lpMessage->Data);
PostQuitMessage(0);
break;
}
}
else
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int main(void)
{
MSG iMessage;
BOOL bVal;
WNDCLASS WMClass;
SetConsoleTitle(WINSERVERNAME);
WMClass.hInstance = GetModuleHandle(NULL);
WMClass.lpszClassName = SERVERWNDCLASSNAME;
WMClass.cbClsExtra = 0;
WMClass.cbWndExtra = 0;
WMClass.hbrBackground = NULL;
WMClass.hCursor = NULL;
WMClass.hIcon = NULL;
WMClass.lpszMenuName = NULL;
WMClass.style = CS_GLOBALCLASS;
WMClass.lpfnWndProc = WMIPCWNDPROC;
if(RegisterClass(&WMClass) == 0)
printf("Class registration failed!\n");
else
{
if(CreateWindow((LPCTSTR)SERVERWNDCLASSNAME, NULL, 0, 0, 0, 0, 0,
NULL, NULL, WMClass.hInstance, NULL) == NULL)
{
printf("Can't create server window\n");
}
else
{
printf("Server successfully started\n");
while((bVal = GetMessage(&iMessage, NULL, 0, 0)) && (bVal != -1))
DispatchMessage(&iMessage);
}
}
return 0;
}