进程间发送消息接收简单数据,消息接收到了,但数据好像是空的,高手请帮看一下代码
Server端:有一个static控件,文本是:test message
LONG OnBtnSendClick(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam){
TCHAR buf[32];
HWND handle = FindWindow(NULL,TEXT("接收消息"));
if (NULL==handle)
{
MessageBox(hDlg,TEXT("找不到接收消息的窗体"),TEXT("错误"),MB_OK);
}else{
COPYDATASTRUCT cs;
cs.dwData = 0;
Static_GetText(GetDlgItem(hDlg,IDC_MSG),buf,32);
cs.lpData = buf;
cs.cbData = wcslen(buf)+1;
SendMessage(handle,WM_COPYDATA,(WPARAM)(HWND)hDlg,(LPARAM)&cs);
}
return 0;
}
Client端:也有一个static控件,文本是: 测试
LONG OnReceive(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam){
//MessageBox(hDlg,TEXT("not fond dsfsdafdsafsa"),TEXT("error"),MB_OK);
COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
TCHAR *buf;
HWND hs = GetDlgItem(hDlg,IDC_RECEIVE);
int reLen = data->cbData;
int sLen = Static_GetTextLength(hs);
buf = new TCHAR[sLen+reLen+1];
Static_GetText(hs,buf,sLen);
wcscpy(buf+sLen,TEXT("\n"));
wcscpy(buf+sLen+1,(TCHAR*)data->lpData);
Static_SetText(hs,buf);
delete buf;
return 0;
}
本人想在收到消息后,读取文本,将客户端文本变成:测试test message
客户端消息是响应了,但是好像,数据没传过来,而且调试的时候,sLen = 2 reLen=12 长度是对的,但是最好显示出来只有一个:“测”,试字读不出来,tessmessage也没有,data->lpData好像是空的,怎么回事呀
[解决办法]
cs.dwData = 1;
cs.cbData = wcslen(buf) * sizeof(TCHAR)+1;
问题的原因肯定不 LSs 所说的变量是局部的。
[解决办法]
大哥,你断点好么,看内存啊。看有没有换行啊,换行是0x0a啊