Tmemorystream, string, and olevariant

Source: Internet
Author: User

///////////////////////////////////// //////////////////////////////////////// ///
// function: the content of string is streamed to olevariant
// parameter:
///////////////////////////////////// //////////////////////////////////////// ///
function texttooledata (const atext: string): olevariant;
var
nsize: integer;
pdata: pointer;
begin
nsize: = length (atext );
If nsize = 0 then
result: = NULL
else begin
result: = vararraycreate ([0, nsize-1], varbyte );
pdata: = vararraylock (result);
try
move (pchar (atext) ^, pdata ^, nsize );
finally
vararrayunlock (result);
end;

//////////////////////////////////////// ////////////////////////////////////////
// Function: the string content is loaded by olevariant.
// Parameters:
//////////////////////////////////////// ////////////////////////////////////////
Function oledatatotext (const aData: olevariant): string;
VaR
Nsize: integer;
Pdata: pointer;
Begin
If aData = NULL then
Result: =''
Else begin
Nsize: = vararrayhighbound (aData, 1)-vararraylowbound (aData, 1) + 1;
Setlength (result, nsize );
Pdata: = vararraylock (aData );
Try
Move (pdata ^, pchar (result) ^, nsize );
Finally
Vararrayunlock (aData );
End;
End;
End;

 

// Convert olevariant and tmemorystream:
Function memorystreamtoolevariant (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 (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;

Function recordsettovariant (const recordset: _ recordset; var stream: olevariant): Boolean;
VaR
RS: olevariant;
Vdata: tmemorystream;
Begin
Result: = false;
If recordset = nil then exit;
Vdata: = tmemorystream. Create;
Try
RS: = createoleobject ('ADODB. recordset ');
RS: = recordset;
Rs. Save (tstreamadapter. Create (vdata) as iunknown, adpersistadtg );
Vdata. Position: = 0;
Result: = true;
Stream: = memorystreamtoolevariant (vdata );
Except
On E: exception do
Begin
RS: = E. message;
Stream: = RS;
Result: = false;
End;
End;
End;
Function recordsetfromvariant (vdata: olevariant): _ recordset;
VaR
RS: olevariant;
Stream: tmemorystream;
Begin
Result: = nil;
Stream: = tmemorystream. Create;
Stream: = olevarianttomemorystream (vdata );
If stream. Size <1 then exit;
Try
Stream. Position: = 0;
RS: = createoleobject ('ADODB. recordset ');
Rs. Open (tstreamadapter. Create (Stream) as iunknown );
Result: = iunknown (RS) AS _ recordset;
Finally;
End;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.