本文介绍了在Delphi中将对象作为OleVariant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在OleVariant中传递一个包装和展开一个TObject子孙?我试图传递一个TObject跨自动化对象。我知道这不是一个好主意,但我没有一个很好的选择。
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.
对象是在同一个自动化dll的对象之间传递,如果这使任何差异。
The object is to be passed between objects from the same automation dll, if that makes any difference.
这样的:
function GetMyObjAsVariant;
var
MyObj: TMyObj;
begin
MyObj := TMyObj.Create;
result := OleVariant(MyObj);
end;
客户端将使用
var
MyObj: TMyObj;
begin
MyObj := GetMyObjAsVariant as TMyObj;
end;
无法编译,返回
E2015 Operator not applicable to this operand type.
推荐答案
如果你绝对,真的想,为了确保两个对象在同一个进程中,您可以将任何TObject转换为整数,然后返回到TObject:
If you absolutely, really want to, and you know for sure both objects are in the same process, you can cast any TObject to an Integer and then back to an TObject:
function GetMyObjAsVariant;
var
MyObj: TMyObj;
begin
MyObj := TMyObj.Create;
result := OleVariant(Integer(MyObj));
end;
,然后:
var
anInt: Integer;
MyObj: TMyObj;
begin
anInt := GetMyObjAsVariant;
MyObj := TMyObj(anInt);
end;
这篇关于在Delphi中将对象作为OleVariant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!