从 dll 内部通过 SendMessage 发送字符串

send string via SendMessage from inside dll

我有两个应用程序使用同一个 DLL。从应用程序“A”我发送了一条带有字符串参数的消息,如下所示

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

在同一个 DLL 中,我有另一个函数来读取该消息,但我收到了一条空消息。我不知道我哪里做错了。

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;

您正在进程之间发送私人消息。系统不会将数据从一个进程编组到另一个进程。这是必需的,因为进程具有专用的隔离虚拟地址空间。

你在发送过程中发送了一个指针,一个指向内存的地址。该地址已收到,但没有用,因为接收进程无法访问发送进程的内存。因此需要在进程之间编组数据。

如果您希望使用消息在进程之间编组数据,您应该使用 WM_COPYDATA 消息。