-1

I use sendMessage and replyMessage to communicate between two apps in the same laptop. On the receiver side, when it receives the message comes from sender, it will reply with a message. So in the sender process, it will cast the MyStruct to LRESULT, send reply this to the sender app. I tried to cast it back in the receiver side, it also works.

    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;

However, when I was trying to cast this LRESULT to MyStruct in the sender side, it fails:

LRESULT result = SendMessage(test, WM_COPYDATA, (WPARAM)(HWND)hwndC, (LPARAM)(LPVOID)&data);
MyStruct* reply = (MyStruct*)result;//the value of reply is unreadable

How could I convert the LRESULT to my custom struct in the sender side ?

I just tried to send interger or float by the way. It works. However, if I use custom struct MyStruct, it won't work. I guess it is because the size of LRESULT is shorter than MyStruct.How to solve this problem ? The size of LRESULT is 4, size of int is also 4.

typedef struct msg{
int msgId;
char msgInfo[100];
}MyStruct;
IInspectable
  • 46,945
  • 8
  • 85
  • 181
firstaccount
  • 155
  • 2
  • 13
  • 1
    SendMessage() returns TRUE or FALSE when you use WM_COPYDATA. Why you think you can treat the return value like a pointer is very hard to guess. – Hans Passant Oct 30 '16 at 20:58
  • https://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx , check this link, it says the return value is LRESULT – firstaccount Oct 30 '16 at 21:00
  • @molbdnilo I know I cannot pass address between app. However, even though I don't use new, use copy constructor, I still cannot cast it successfully. I tried both of them, I only post one of them in this question – firstaccount Oct 30 '16 at 21:06
  • Possible duplicate of [Use WM\_COPYDATA to send data between processes](http://stackoverflow.com/questions/2451103/use-wm-copydata-to-send-data-between-processes) – IInspectable Oct 31 '16 at 10:57
  • Things won't work for you, if you continue to fail at comprehending documentation. If you think that you don't need to know what you're doing, [using data copy](https://msdn.microsoft.com/en-us/library/windows/desktop/ms649009.aspx) comes with a fully working sample implementation. (In case you're wondering, the down vote is due to lack of research.) – IInspectable Oct 31 '16 at 10:59
  • @molbdnilo: While true in general, the OS performs marshaling of data for standard window messages. For example, the pointer passed as *lParam* for [WM_GETTEXT](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632627.aspx) is marshaled by the system to end up being meaningful in the target process. [WM_COPYDATA](https://msdn.microsoft.com/en-us/library/windows/desktop/ms649011.aspx) being another example, where pointers are marshaled by the system as needed. – IInspectable Oct 31 '16 at 11:20

1 Answers1

0

When you send WM_COPYDATA, the data itself is copied to the receiving process.
The receiver of WM_COPYDATA gets a pointer to this copy.
It's very unlikely that the addresses are the same on both ends, but each end has a valid pointer to its own copy of the data.

On the other hand, ReplyMessage does no such copying and only returns the (reinterpreted) address of the sender's data.
This is not a valid address on the receiving end.

If you want to pass data back and forth, you need to use SendMessage with WM_COPYDATA in both directions, possibly adding your own protocol on top.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82