Note that there are some explanatory texts on larger screens.

plurals
  1. POOleVariant stream to TStringList optimization
    text
    copied!<p>My server returns a string stream as OleVariant via a COM/DCOM call. This code works well for the client, but can probably be improved. Hints are welcome.</p> <p>And as a follow up, what if the server returns the OleVariant stream as UTF-8, what is the best way for the client to convert it to unicode ?</p> <pre><code>procedure TClientToServer.GetText( mode : integer; const infoText : widestring; txt : TStrings); var size : integer; dataP : pointer; data : OleVariant; strStream : TStringStream; begin txt.Clear; data:= fDComServer.GetData( mode,infoText); if VarIsNull(data) then Exit; size:= VarArrayHighBound(data,1) + 1; dataP:= VarArrayLock( data); try strStream:= TStringStream.Create; try strStream.Write( dataP^,size); strStream.Seek(0,0); txt.LoadFromStream( strStream); finally strStream.Free; end; finally VarArrayUnlock( data); end; end; </code></pre> <p>Edit : Going for UTF8 means that less bandwidth is used on the network, and I can reuse the COM/DCOM routines more or less intact when later implementing a http protocol interface based on UTF8 strings.</p> <p>Thanks David for pointing me in the right direction to trim the code a little bit.</p> <p>Here is the updated procedure :</p> <pre><code>procedure TClientToServer.GetText( mode : integer; const infoText : widestring; txt : TStrings); var size : integer; dataP : Pointer; data : OleVariant; utf8Str : Utf8String; begin txt.Clear; data:= fDComServer.GetData( mode,infoText); if VarIsNull(data) then Exit; size:= VarArrayHighBound(data,1) + 1; dataP:= VarArrayLock( data); try SetString( utf8Str, PAnsiChar(dataP), size); txt.Text:= utf8Str; finally VarArrayUnlock( data); end; end; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload