求助:我在进程a 中发送一个消息给进程b ,为什么b收到消息后,在读取信息时出错了
这是进程a ,发送消息的代码
//------------------------结构体
typedef struct changeInfo
{
int operate; //0表示删除,1 表示添加, 2 表示修改
AnsiString name;
}CHANGEINFO;
///-----------------------
COPYDATASTRUCT cds;
cds.dwData = 0;
CHANGEINFO *info = new CHANGEINFO;
info-> operate = operate;
info-> name = name;
cds.lpData = (PVOID)info;
cds.cbData = sizeof(CHANGEINFO);
SendMessage(msgHwnd, WM_COPYDATA, wparam, (LPARAM)&cds);
delete info;
进程b中在读取
info-> operate时,是正确的,info-> name时就错了
[解决办法]
我试了一下,没问题啊
typedef struct tagChangeInfo
{
int operate;
AnsiString name;
} TChangeInfo;
void __fastcall TForm1::btnSendClick(TObject *Sender)
{
COPYDATASTRUCT cds;
cds.dwData = 0;
TChangeInfo* pInfo = new TChangeInfo;
pInfo-> name = "abc ";
pInfo-> operate = 100;
cds.lpData = (PVOID)pInfo;
cds.cbData = sizeof(TChangeInfo);
SendMessage(Handle, WM_COPYDATA, (WPARAM)Handle, (LPARAM)&cds);
delete pInfo;
}
void __fastcall TForm1::OnCopyData(TMessage &msg)
{
if (msg.Msg = WM_COPYDATA) {
TChangeInfo* pInfo = (TChangeInfo*)(((COPYDATASTRUCT*)(msg.LParam))-> lpData);
int n = pInfo-> operate;
AnsiString name = pInfo-> name;
}
}