Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
883 views
in Technique[技术] by (71.8m points)

casting - Convert OleVariant to Object in Delphi

I'm working on this project where we don't have the source code for large chunks of the project, but we have the .DLL files with some information. There is a bug in the DLL files. I am able to create a subclass of the class with the bug in it and I would like to downcast the object which already exists at a point I have access to it. The issue is that at any point I have access to the object, it's cast as a Variant. I've tried the following (edited to remove context):

tempSubclass := Subclass(ParentClass(Integer(oleVariantCast)));

but I get the following error:

Could not convert variant of type (Dispatch) into type (Integer)

Is there any other way to get the pointer to the object out of the OleVariant and/or do the typecasting involved?

Thank you.

EDIT: Yes, the Parentclass implements IDispatch. CORRECTION: The parentclass implements an interface which inherits from IDispatch.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A Dispatch Variant is a pretty generic interface, not a class (which is why it can't be typecast to a Delphi object - it isn't one, and doesn't have the VMT for the class you're trying to cast it to become).

If the DLL contains a type library, you can import that into Delphi and then use the interfaces it contains directly without trying to cast them to anything else first.

If you have documentation about the actual interface implementation in the DLL, you can write a Delphi class that uses that interface. You can convert it by defining a type to represent the interface, and then get access to it using as:

type
  TYourInterface=interface(IDispatch)
    // the interface definition here
  end;

var
  Intf: TYourInterface;
begin
  Intf := YuorOleVariant as TYourInterface;
  // work with interface from DLL using Intf.
  Intf := nil;
end;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...