Rahul Bajaj
Rahul Bajaj

Reputation: 135

send string via SendMessage from inside dll

I have two applications that use one common DLL. From application "A' I have sent a message with string parameter as shown below

txt := 'Test String'
SendMessage(Handle, MyMessage, 0, lParam(PChar(txt)));

and in the same DLL I have another function to read that message, But I have received an empty message. I don't know where I am doing mistake.

procedure MyClass.WndMethod(var Msg: TMessage);
var
  Str: string;
begin
  case Msg.Msg of
    MyMessage:
    begin
      Str := string(PChar(Msg.LParam));
      ShowMessage(Str);  // Empty message
    end;
end;

Upvotes: 0

Views: 2348

Answers (1)

David Heffernan
David Heffernan

Reputation: 613572

You are sending a private message between processes. The system does not marshal the data from one process to the other. That's needed because processes have private isolated virtual address spaces.

You are sending a pointer, an address to memory in the sending process. That address is received, but it is of no use because the receiving process cannot access the sending process memory. Hence the need to marshal the data between processes.

If you wish to marshal data between processes using messages, you should use the WM_COPYDATA message.

Upvotes: 1

Related Questions