Board index » delphi » Converting Variant into Stream

Converting Variant into Stream


2003-07-04 10:33:21 PM
delphi274
All,
This is a newbie question.
I'm using a couple of OleAutomation objects in a Delphi app, one of which
contains a message object of type OLEVariant that itself contains a
GetStream function.
This GetStream function returns a variant.
I need to treat the variant like a stream object so that I can write to the
stream.
How do I do this?
Woeful at OLE
 
 

Re:Converting Variant into Stream

<<Wayne Williams:
This GetStream function returns a variant.
I need to treat the variant like a stream object so that I
can write to the stream.
Quote
>
function MemoryStreamToOleVariant(const Strm:
TMemoryStream): OleVariant;
var
Data: PByteArray;
begin
Result := VarArrayCreate ([0, Strm.Size - 1], varByte);
Data := VarArrayLock(Result);
try
Strm.Position := 0;
Strm.ReadBuffer(Data^, Strm.Size);
finally
VarArrayUnlock(Result);
end;
end;
function OleVariantToMemoryStream(const OV: OleVariant):
TMemoryStream;
var
Data: PByteArray;
Size: integer;
begin
Result := TMemoryStream.Create;
try
Size := VarArrayHighBound (OV, 1) - VarArrayLowBound
(OV, 1) + 1;
Data := VarArrayLock(OV);
try
Result.Position := 0;
Result.WriteBuffer(Data^, Size);
finally
VarArrayUnlock(OV);
end;
except
Result.Free;
Result := nil;
end;
end;
--
Deborah Pate (TeamB) delphi-jedi.org
TeamB don't see posts sent via Google or ISPs
Use the real Borland server: newsgroups.borland.com
www.borland.com/newsgroups/genl_faqs.html
 

Re:Converting Variant into Stream

Deborah,
Thanks for your help, but the variant being returned is not an array. How
can I figure out what is being returned so as to devise the proper handling?
Wayne
"Deborah Pate (TeamB)" <XXXX@XXXXX.COM>wrote in
message news:XXXX@XXXXX.COM...
Quote
<<Wayne Williams:
This GetStream function returns a variant.

I need to treat the variant like a stream object so that I
can write to the stream.
>>

function MemoryStreamToOleVariant(const Strm:
TMemoryStream): OleVariant;
var
Data: PByteArray;
begin
Result := VarArrayCreate ([0, Strm.Size - 1], varByte);
Data := VarArrayLock(Result);
try
Strm.Position := 0;
Strm.ReadBuffer(Data^, Strm.Size);
finally
VarArrayUnlock(Result);
end;
end;

function OleVariantToMemoryStream(const OV: OleVariant):
TMemoryStream;
var
Data: PByteArray;
Size: integer;
begin
Result := TMemoryStream.Create;
try
Size := VarArrayHighBound (OV, 1) - VarArrayLowBound
(OV, 1) + 1;
Data := VarArrayLock(OV);
try
Result.Position := 0;
Result.WriteBuffer(Data^, Size);
finally
VarArrayUnlock(OV);
end;
except
Result.Free;
Result := nil;
end;
end;

--
Deborah Pate (TeamB) delphi-jedi.org

TeamB don't see posts sent via Google or ISPs
Use the real Borland server: newsgroups.borland.com
www.borland.com/newsgroups/genl_faqs.html


 

Re:Converting Variant into Stream

<<Wayne Williams:
Thanks for your help, but the variant being returned is not
an array. How can I figure out what is being returned so
as to devise the proper handling?
Quote
>
Well, it is best if you can ask the creator of the server
what GetStream is supposed to return. But you could find
out what type the variant is, using the VarType function.
--
Deborah Pate (TeamB) delphi-jedi.org
TeamB don't see posts sent via Google or ISPs
Use the real Borland server: newsgroups.borland.com
www.borland.com/newsgroups/genl_faqs.html