Delphi中variant 和 Stream 的互換
DELPHI中使用COM,COM+时候经常需要用到STREAM和VARIANT类型的转换
1 procedure VarToStream(var AStm: TStream; var AOvar: Olevariant);
2 var
3 p: Pointer;
4 begin
5 AStm:= TMemoryStream.Create;
6 AStm.Position := 0;
7 p := VarArrayLock(AOvar);
8 AStm.Write(p^, VarArrayHighBound(AOvar, 1));
9 VarArrayUnlock(AOvar);
10 end;
11
12 procedure StreamToVar(var AStm: TStream; var AOvar: Olevariant);
13 var
14 p: Pointer;
15 begin
16 AOvar := VarArrayCreate([0, AStm.Size - 1], VarByte);
17 p := VarArrayLock(AOvar);
18 AStm.ReadBuffer(p^, AStm.Size);
19 VarArrayUnlock(AOvar);
20 end;
2 var
3 p: Pointer;
4 begin
5 AStm:= TMemoryStream.Create;
6 AStm.Position := 0;
7 p := VarArrayLock(AOvar);
8 AStm.Write(p^, VarArrayHighBound(AOvar, 1));
9 VarArrayUnlock(AOvar);
10 end;
11
12 procedure StreamToVar(var AStm: TStream; var AOvar: Olevariant);
13 var
14 p: Pointer;
15 begin
16 AOvar := VarArrayCreate([0, AStm.Size - 1], VarByte);
17 p := VarArrayLock(AOvar);
18 AStm.ReadBuffer(p^, AStm.Size);
19 VarArrayUnlock(AOvar);
20 end;