我使用sendMessage
andreplyMessage
在同一台笔记本电脑中的两个应用程序之间进行通信。在接收方,当它收到来自发送方的消息时,它会回复一条消息。所以在发送者进程中,它会将这个MyStruct
转换为 LRESULT,将这个回复发送给发送者应用程序。我试图把它扔回接收器端,它也有效。
PCOPYDATASTRUCT result;
MyStruct* data;
LRESULT a;
MyStruct* t;
MyStruct* reply = new MyStruct;
switch (uMessageType)
{
case WM_COPYDATA:
result = (PCOPYDATASTRUCT)addtionalData;
data = (MyStruct*)result->lpData;
reply->msgId = 10;
strcpy_s(reply->msgInfo, 100, "test reply");
a = reinterpret_cast<LRESULT>(reply);
t = reinterpret_cast<MyStruct*>(a);//when cast the LRESULT data to MyStruct back here, it succeed
ReplyMessage(reinterpret_cast<LRESULT>(reply));
break;
但是,当我尝试将此 LRESULT 投射到MyStruct
发送方时,它失败了:
LRESULT result = SendMessage(test, WM_COPYDATA, (WPARAM)(HWND)hwndC, (LPARAM)(LPVOID)&data);
MyStruct* reply = (MyStruct*)result;//the value of reply is unreadable
如何将 LRESULT 转换为发送方的自定义结构?
我只是想顺便发送整数或浮点数。有用。但是,如果我使用自定义 struct MyStruct
,它将不起作用。我猜这是因为 LRESULT 的大小比MyStruct
.How 解决这个问题?LRESULT 的大小是 4,int 的大小也是 4。
typedef struct msg{
int msgId;
char msgInfo[100];
}MyStruct;