使用C ++ Builder延迟绑定COM对象 [英] Late Binding COM objects with C++Builder
问题描述
我们正在与来自C ++ Builder 2010应用程序的一些第三方COM对象连接。
目前我们导入类型库并生成组件包装器,然后能够以相当自然的方式调用方法和访问属性。
object-> myProperty = 42;
object-> doSomething(666);但是,我们已经被修改的COM对象的接口(这仍然是被扩展和被咬了,开发)导致我们自己的应用程序失败,因为一些方法GUID似乎无效 - 即使接口的唯一的改变是添加一个新的方法)。
已经建议延迟绑定作为解决这个问题的一种方式。我认为这需要我们的代码更改为:
object.OlePropertySet(myProperty,42);
object.OlePrcedure(doSomething,666);
显然这对于读写是很痛苦的,所以我们必须编写包装类。 / p>
当我们导入类型库时,是否有任何方法获得自动生成的后期绑定包装?
解决方案 如果是,那么, div> 当为支持后期绑定的COM对象(当它实现 IDispatch
接口)导入TypeLibrary时,导入程序可以生成单独的包装类组件)用于静态绑定和后期绑定。
向现有接口添加新方法不应使代码失效。方法没有GUID。但是,对于 IDispatch
的接口,它的方法有 DISPID
与它们相关联的值,那些 DISPID
值可以从一个版本更改为另一个。尽管任何可信的COM开发人员都不应该在界面定义被锁定之后这样做。
We're interfacing to some 3rd party COM objects from a C++Builder 2010 application.
Currently we import the type library and generate component wrappers, and then are able to make method calls and access properties in a fairly natural way.
object->myProperty = 42;
object->doSomething(666);
However, we've been bitten by changes to the COM object's interface (which is still being extended and developed) causing our own app to fail because some method GUIDs seem to get invalidated - even if the only change to the interface has been the addition of a new method).
Late Binding has been suggested as a way of addressing this. I think this requires our code to be changed rather like this:
object.OlePropertySet("myProperty", 42);
object.OlePrcedure("doSomething", 666);
Obviously this is painful to read and write, so we'd have to write wrapper classes instead.
Is there any way of getting late binding wrappers generated automatically when we import the type library? And, if so, are they smart enough to only do the textual binding once when the object is created, rather than on every single method call?
解决方案 When you import a TypeLibrary for a COM object that supports late-binding (when it implements the IDispatch
interface), the importer can generate separate wrapper classes (not components) for both static-binding and late-binding.
Adding a new method to an existing interface should not invalidate your code. Methods do not have GUIDs. However, for an IDispatch
-based interface, its methods do have DISPID
values associated with them, and those DISPID
values can be changed from one release to another. Though any respectable COM developer should never do that once an interface definition has been locked in.
这篇关于使用C ++ Builder延迟绑定COM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文