I am writing an application in C++ which will send a message to an application written in Delphi.
This is my receiver app:
When the button is clicked, Edit1.Text will be sent via ShellExecute() as a command line parameter to the sender app (C++).
The sender app will send back the parameter as a WM_COPYDATA message to the receiver app, which will show it in the Edit2 text box.
This is the Delphi app's code (Delphi 10.3 Rio):
procedure TForm1.Button1Click(Sender: TObject);
begin
ShellExecute(0, 'open', 'deneme.exe', PWideChar(Edit1.Text), nil, SW_HIDE);
end;
procedure TForm1.MesajAl(var Mesaj: TMessage);
var
Veri: PCopyDataStruct;
begin
Veri := Pointer(Mesaj.LParam);
Edit2.Text := PChar(Veri^.lpData);
end;
This is my C++ app's code (Code::Blocks IDE):
#include <iostream>
#include <windows.h>
#include <tchar.h>
using namespace std;
int main(int argc, char* argv[])
{
if (argc < 2)
{
return 0;
}
else
{
HWND hwnd = FindWindow(NULL, "Form1");
string alinanMesaj;
LPCTSTR gonderilecekMesaj = alinanMesaj.c_str();
COPYDATASTRUCT cds;
cds.cbData = sizeof(TCHAR)*(_tcslen(gonderilecekMesaj) + 1);
cds.dwData = 1;
cds.lpData = (PVOID)gonderilecekMesaj;
SendMessage(hwnd, WM_COPYDATA, (WPARAM)hwnd, (LPARAM)(LPVOID)&cds);
return 0;
}
}
The problem is the Edit2 text box shows nothing.
By the way, I have made a research in this website about WM_COPYDATA. But despite the fact that this situation, I could not fix my issue myself.
So, what should I do in order to fix my issue?

I see three issues with this code:
The sender is sending blank data, because
alinanMesajis not assigned any value.There is an ANSI/UNICODE mismatch between the two apps. The Delphi code is working with Unicode strings, while the C++ code is working with ANSI strings instead.
WM_COPYDATAoperates on bytes, not characters. You have to pick a byte encoding for your string data and be consistent with it on both sides.The VCL uses
WM_COPYDATAinternally, so the sender needs to set thecds.dwDatafield to a unique value, such as fromRegisterWindowMessage(), that the receiver must verify before it interprets thecds.lpDatadata.With at said, try this instead: