在 Delphi 中将 OleVariant 转换为对象

发布于 2024-10-22 01:36:14 字数 472 浏览 8 评论 0原文

我正在开发这个项目,我们没有项目大部分的源代码,但我们有包含一些信息的 .DLL 文件。 DLL 文件中存在错误。我能够创建包含错误的类的子类,并且我想向下转换在我可以访问它的点上已经存在的对象。问题是,在任何时候我可以访问该对象,它都会被转换为变体。我已尝试以下操作(编辑以删除上下文):

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

但出现以下错误:

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

是否有其他方法可以从 OleVariant 中获取指向对象的指针和/或进行类型转换?

谢谢。

编辑:是的,父类实现了 IDispatch。 更正:父类实现一个继承自 IDispatch 的接口。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静被遗忘 2024-10-29 01:36:14

Dispatch Variant 是一个非常通用的接口,而不是一个类(这就是为什么它不能类型转换为 Delphi 对象 - 它不是一个,并且没有您尝试转换的类的 VMT成为)。

如果 DLL 包含类型库,您可以将其导入 Delphi,然后直接使用它包含的接口,而无需先尝试将它们转换为其他任何内容。

如果您有有关 DLL 中实际接口实现的文档,您可以编写一个使用该接口的 Delphi 类。您可以通过定义一个类型来表示接口来转换它,然后使用 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;

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;
偏爱你一生 2024-10-29 01:36:14

首先尝试将类型转换为 IUnknown。

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

Try type cast to IUnknown first.

tempSubclass := Subclass(ParentClass(Integer(IUnknown(oleVariantCast))));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文