参考这2个函数
function TCommClient.SendString(aStr: String; const aDestination: String; aTTL: Integer): Integer;
var
V : Variant;
P : Pointer;
L : Integer;
begin
Result := -1;
try
L := Length(aStr);
if L > 0 then
begin
V := VarArrayCreate([0, L-1], varByte);
P := VarArrayLock(V);
try
Move(aStr[1], P^, L);
finally
VarArrayUnlock(V);
end;
Assert(FServer <> nil);
if Active then Result := FServer.Send(V, aDestination, aTTL);
end;
except
on E: Exception do
begin
TLogFile.WriteLn('发送数据出错, 错误信息="%s"', [E.Message]);
end;
end;
end;
procedure TCommClient.DoOnData(aData: OleVariant; const aSource: WideString; out aAction: Integer);
var
S : TMemoryStream;
P : Pointer;
L : Integer;
begin
if Assigned(FOnData) then
try
S := TMemoryStream.Create;
try
//将传过来的安全数组转换为TStream类型,触发事件
L := VarArrayHighBound(aData, 1) - VarArrayLowBound(aData, 1) + 1;
P := VarArrayLock(aData);
try
S.Seek(0, soFromBeginning);
S.WriteBuffer(P^, L);
finally
VarArrayUnlock(aData);
end;
FOnData (Self, S, aSource, aAction);
finally
S.Free
end;
except
on E: Exception do
begin
TLogFile.WriteLn('数据接收处理出错, 错误信息="%s"', [E.Message]);
end;
end;
end;