如何在应用程序之间发送包含字符串的记录

How to send records containing strings between applications

所以,我有一个 class 使用 WM_COPYDATA 允许应用程序进行通信。

type
  TMyRec = record
    Name: string[255]; // I want just string
    Age: integer;
    Birthday: TDateTime;
  end;

function TAppCommunication.SendRecord(const ARecordType: ShortString; const ARecordToSend: Pointer; ARecordSize: Integer): Boolean;
var
  _Stream: TMemoryStream;
begin
  _Stream := TMemoryStream.Create;
  try
    _Stream.WriteBuffer(ARecordType, 1 + Length(ARecordType));
    _Stream.WriteBuffer(ARecordToSend^, ARecordSize);
    _Stream.Position := 0;
    Result := SendStreamData(_Stream, TCopyDataType.cdtRecord);
  finally
    FreeAndNil(_Stream);
  end;
end;

function TAppCommunication.SendStreamData(const AStream: TMemoryStream;
  const ADataType: TCopyDataType): Boolean;
var
  _CopyDataStruct: TCopyDataStruct;
begin
  Result := False;

  if AStream.Size = 0 then
    Exit;

  _CopyDataStruct.dwData := integer(ADataType);
  _CopyDataStruct.cbData := AStream.Size;
  _CopyDataStruct.lpData := AStream.Memory;

  Result := SendData(_CopyDataStruct);
end;

function TAppCommunication.SendData(const ADataToSend: TCopyDataStruct)
  : Boolean;
var
  _SendResponse: integer;
  _ReceiverHandle: THandle;
begin
  Result := False;

  _ReceiverHandle := GetRemoteReceiverHandle;
  if (_ReceiverHandle = 0) then
    Exit;

  _SendResponse := SendMessage(_ReceiverHandle, WM_COPYDATA,
    WPARAM(FLocalReceiverForm.Handle), LPARAM(@ADataToSend));

  Result := _SendResponse <> 0;
end;

发件人申请:

procedure TSenderMainForm.BitBtn1Click(Sender: TObject);
var
  _AppCommunication: TAppCommunication;
  _ms: TMemoryStream;
  _Rec: TMyRec;
  _Record: TAttrData;
begin
  _AppCommunication := TAppCommunication.Create('LocalReceiverName', OnAppMessageReceived);
  _ms := TMemoryStream.Create;
  try
    _AppCommunication.SetRemoteReceiverName('LocalReceiverNameServer');
    _AppCommunication.SendString('ąčęėįšųūž123');
    _AppCommunication.SendInteger(998);
    _AppCommunication.SendDouble(0.95);

    _Rec.Name := 'Edijs';
    _Rec.Age := 29;
    _Rec.Birthday := EncodeDate(1988, 10, 06);
    _Record.Len := 1988;
    _AppCommunication.SendRecord(TTypeInfo(System.TypeInfo(TMyRec)^).Name, @_Rec, SizeOf(_Rec));
  finally
    FreeAndNil(_ms);
    FreeAndNil(_AppCommunication);
  end;
end;

接收器应用程序:

procedure TReceiverMainForm.OnAppMessageReceived(const ASender
  : TPair<HWND, string>; const AReceivedData: TCopyDataStruct;
  var AResult: integer);
var
  _MyRec: TMyRec;
  _RecType: ShortString;
  _RecData: Pointer;
begin
  ...
  else
  begin
    if (AReceivedData.dwData) = Ord(TCopyDataType.cdtRecord) then
    begin
    _RecType := PShortString(AReceivedData.lpData)^;
      _RecData := PByte(AReceivedData.lpData)+1+Length(_RecType);
      if (_RecType = TTypeInfo(System.TypeInfo(TMyRec)^).Name) then
      begin
        _MyRec := TMyRec(_RecData^);
        ShowMessage(_MyRec.Name + ', Age: ' + IntToStr(_MyRec.Age) + ', birthday: ' +
          DateToStr(_MyRec.Birthday));
      end;
    end;
    AResult := -1;
  end;
end;

问题是当我在 TMyRec 中将 Name: string[255]; 更改为 Name: string; 时发生崩溃。我该如何克服这个问题?我不想编辑我所有的记录以将字符串更改为其他内容,我希望有一个函数来发送所有类型的记录(据我的想法 none 其中将包含对象)。

已编辑: 使用 Remy 提供的答案并进行了一些调整,这样我就可以仅使用一个 SendRecord 函数发送任何类型的记录:

function TAppCommunication.SendRecord(const ARecordToSend, ARecordTypInfo: Pointer): Boolean;
var
  _Stream: TMemoryStream;
  _RType: TRTTIType;
  _RFields: TArray<TRttiField>;
  i: Integer;
begin
  _Stream := TMemoryStream.Create;
  try
    _RType := TRTTIContext.Create.GetType(ARecordTypInfo);

    _Stream.WriteString(_RType.ToString);
    _RFields := _RType.GetFields;
    for i := 0 to High(_RFields) do
    begin
      if _RFields[i].FieldType.TypeKind = TTypeKind.tkUString then
        _Stream.WriteString(_RFields[i].GetValue(ARecordToSend).ToString)
      else if _RFields[i].FieldType.TypeKind = TTypeKind.tkInteger then
        _Stream.WriteInteger(_RFields[i].GetValue(ARecordToSend).AsType<integer>)
      else if _RFields[i].FieldType.TypeKind = TTypeKind.tkFloat then
        _Stream.WriteDouble(_RFields[i].GetValue(ARecordToSend).AsType<Double>)
    end;
    _Stream.Position := 0;
    Result := SendStreamData(_Stream, TCopyDataType.cdtRecord);
  finally
    FreeAndNil(_Stream);
  end;
end;

发件人:

_AppCommunication.SendRecord(@_Rec, System.TypeInfo(TMyRec));

A ShortString 的固定大小最大为 256 字节(1 字节长度 + 最多 255 AnsiChars),因此很容易嵌入记录并按原样发送。

另一方面,

A String 是指向 Char 数组动态分配内存的指针。因此,来回序列化需要更多的工作。

要按照您的要求进行操作,您不能简单地将 ShortString 替换为 String,而不更改两者之间的所有其他内容以解决该差异。

您已经有了发送可变长度字符串的基本框架(在发送数据之前先发送长度),因此您可以对其进行扩展以处理 string 值,例如:

type
  TMyRec = record
    Name: string;
    Age: integer;
    Birthday: TDateTime;
  end;

  TStreamHelper = class helper for TStream
  public
    function ReadInteger: Integer;
    function ReadDouble: Double;
    function ReadString: String;
    ...
    procedure WriteInteger(Value: Integer);
    procedure WriteDouble(Strm: Value: Double);
    procedure WriteString(const Value: String);
  end;

function TStreamHelper.ReadInteger: Integer;
begin
  Self.ReadBuffer(Result, SizeOf(Integer));
end;

function TStreamHelper.ReadDouble: Double;
begin
  Self.ReadBuffer(Result, SizeOf(Double));
end;

function TStreamHelper.ReadString: String;
var
  _Bytes: TBytes;
  _Len: Integer;
begin
  _Len := ReadInteger;
  SetLength(_Bytes, _Len);
  Self.ReadBuffer(PByte(_Bytes)^, _Len);
  Result := TEncoding.UTF8.GetString(_Bytes);
end;

...

procedure TStreamHelper.WriteInteger(Value: Integer);
begin
  Self.WriteBuffer(Value, SizeOf(Value));
end;

procedure TStreamHelper.WriteDouble(Value: Double);
begin
  Self.WriteBuffer(Value, SizeOf(Value));
end;

procedure TStreamHelper.WriteString(const Value: String);
var
  _Bytes: TBytes;
  _Len: Integer;
begin
  _Bytes := TEncoding.UTF8.GetBytes(Value);
  _Len := Length(_Bytes);
  WriteInteger(_Len);
  Self.WriteBuffer(PByte(_Bytes)^, _Len);
end;

function TAppCommunication.SendRecord(const ARecord: TMyRec): Boolean;
var
  _Stream: TMemoryStream;
begin
  _Stream := TMemoryStream.Create;
  try
    _Stream.WriteString('TMyRec');
    _Stream.WriteString(ARecord.Name);
    _Stream.WriteInteger(ARecord.Age);
    _Stream.WriteDouble(ARecord.Birthday);
    _Stream.Position := 0;
    Result := SendStreamData(_Stream, TCopyDataType.cdtRecord);
  finally
    FreeAndNil(_Stream);
  end;
end;

// more overloads of SendRecord()
// for other kinds of records as needed... 

procedure TSenderMainForm.BitBtn1Click(Sender: TObject);
var
  ...
  _Rec: TMyRec;
begin
  ...
  _Rec.Name := 'Edijs';
  _Rec.Age := 29;
  _Rec.Birthday := EncodeDate(1988, 10, 06);
  _AppCommunication.SendRecord(_Rec);
  ...
end;

type
  TReadOnlyMemoryStream = class(TCustomMemoryStream)
  public
    constructor Create(APtr: Pointer; ASize: NativeInt);
    function Write(const Buffer; Count: Longint): Longint; override;
  end;

constructor TReadOnlyMemoryStream.Create(APtr: Pointer; ASize: NativeInt);
begin
  inherited Create;
  SetPointer(APtr, ASize);
end;

function TReadOnlyMemoryStream.Write(const Buffer; Count: Longint): Longint;
begin
  Result := 0;
end;

procedure TReceiverMainForm.OnAppMessageReceived(const ASender : TPair<HWND, string>; const AReceivedData: TCopyDataStruct; var AResult: integer);
var
  ... 
  _Stream: TReadOnlyMemoryStream;
  _MyRec: TMyRec;
  _RecType: String;
begin
  ...
  else
  begin
    if (AReceivedData.dwData = Ord(TCopyDataType.cdtRecord)) then
    begin
      _Stream := TReadOnlyMemoryStream(AReceivedData.lpData, AReceivedData.cbData);
      try
        _RecType := _Stream.ReadString;
        if (_RecType = 'TMyRec') then
        begin
          _MyRec.Name := _Stream.ReadString;
          _MyRec.Age := _Stream.ReadInteger;
          _MyRec.Birthday := _Stream.ReadDouble;
          ShowMessage(_MyRec.Name + ', Age: ' + IntToStr(_MyRec.Age) + ', birthday: ' + DateToStr(_MyRec.Birthday));
        end;
      finally
        _Stream.Free;
      end;
    end;
    AResult := -1;
  end;
end;