在 Delphi 中将 OleVariant 转换为对象
我正在开发这个项目,我们没有项目大部分的源代码,但我们有包含一些信息的 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Dispatch Variant 是一个非常通用的接口,而不是一个类(这就是为什么它不能类型转换为 Delphi 对象 - 它不是一个,并且没有您尝试转换的类的 VMT成为)。
如果 DLL 包含类型库,您可以将其导入 Delphi,然后直接使用它包含的接口,而无需先尝试将它们转换为其他任何内容。
如果您有有关 DLL 中实际接口实现的文档,您可以编写一个使用该接口的 Delphi 类。您可以通过定义一个类型来表示接口来转换它,然后使用
as
访问它: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
:首先尝试将类型转换为 IUnknown。
Try type cast to IUnknown first.