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

时间:2017-10-17 04:09:48

标签: string delphi-xe2 record wm-copydata

所以,我有一个使用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;

Receiver app:

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;

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

EDITED : 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));

1 个答案:

答案 0 :(得分:1)

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

另一方面,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;