Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast object as OleVariant in Delphi

Is there a way to pass a wrap and unwrap a TObject descendent in an OleVariant? I am trying to pass a TObject across automation objects. I know it's not a good idea but I don't have a good alternative.

The object is to be passed between objects from the same automation dll, if that makes any difference.

Something like this:

function GetMyObjAsVariant;
var
  MyObj: TMyObj;
begin
  MyObj := TMyObj.Create;
  result := OleVariant(MyObj);
end;

Which would be used by a client as

var
  MyObj: TMyObj;
begin
  MyObj := GetMyObjAsVariant as TMyObj;
end;

This fails to compile, returning

E2015 Operator not applicable to this operand type.
like image 435
Alan Clark Avatar asked Jan 24 '26 03:01

Alan Clark


1 Answers

You could write and register a custom Variant type; have a look at TSQLTimeStampVariantType for an example.

An alternative would be to write an automation wrapper for your class. The dual dispinterface automatically supports late binding through IDispatch which is supported by OleVariant.

like image 144
Ondrej Kelle Avatar answered Jan 25 '26 18:01

Ondrej Kelle