Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WM_COPYDATA: Can the receiver modify the COPYDATASTRUCT contents?

I am trying to communicate between two Windows applications in Delphi. Sender sends commands via SendMessage using WM_COPYDATA. That part is working fine. Is it possible for the receiver to reply back some result strings in the same call? It is failing for me and following is what is happening now.

  1. Sender uses WM_COPYDATA to send a command to the Receiver using the blocking call SendMessge.

  2. Receiver processes the command and modify the COPYDATASTRUCT with some result strings that must be sent back to the sender and exit out of the event handler

  3. Receiver's "SendMessage" function returns but the contents of the COPYDATASTRUCT are still unchanged.

Apparently Windows' messaging mechanism is not sharing the COPYDATASTRUCT memory between two applications. Instead it is making a copy.

like image 398
ssh Avatar asked Dec 22 '22 00:12

ssh


2 Answers

WM_COPYDATA does just what it says: It copies the data from the source process to the target process. It does not copy the data back from the target process to the source process. If you want bidirectional communication, send another message the other direction.

like image 105
Dark Falcon Avatar answered Dec 23 '22 15:12

Dark Falcon


Please consider reading the documentation. Remark section imposes the following rules:

The receiving application should consider the data read-only. The lParam parameter is valid only during the processing of the message. The receiving application should not free the memory referenced by lParam. If the receiving application must access the data after SendMessage returns, it must copy the data into a local buffer.

like image 25
OnTheFly Avatar answered Dec 23 '22 15:12

OnTheFly