1

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.

mnuzzo
  • 3,529
  • 4
  • 26
  • 29

2 Answers2

2

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;
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • I tried what you seemed to be suggesting which is: "tempSubclass := oleVariantCast as Subclass" and it gives an error saying "Operator not applicable to this operand type". – mnuzzo Mar 14 '11 at 18:54
  • I misunderstood what you were saying, but I get the same error when I try it with an interface. – mnuzzo Mar 14 '11 at 19:02
1

Try type cast to IUnknown first.

tempSubclass := Subclass(ParentClass(Integer(IUnknown(oleVariantCast))));
Mikael Eriksson
  • 136,425
  • 22
  • 210
  • 281
  • This just screams "it compiles, but does it really work" at me. – Warren P Mar 15 '11 at 12:42
  • @Warren P I tried to describe here how to go from OleVariant to Interface through IUnknown http://stackoverflow.com/questions/4938601/getting-an-istream-from-an-olevariant/4938818#4938818 . If it works in this case or not I have no idea. – Mikael Eriksson Mar 15 '11 at 13:56