【发布时间】:2014-11-15 22:15:48
【问题描述】:
我有一个 Visual Studio 插件,其中包含一个用 C++ 实现的脚本引擎。
加载项只能使用 IDispatch 接口与 Visual Studio 进行通信。
我正在将其从 VS 2005 升级到 VS 2010。
外接程序进行一系列IDispatch::Invoke() 调用,与此 Visual Basic 等效:
control = commandBar.Controls.Add(MsoControlType.msoControlButton)
control.Caption = "My button"
control.FaceId = 59
在 VS 2005 中,这曾经可以工作。但在 VS 2010 中却不行。 GetIDsOfNames() 为“FaceId”返回 DISP_E_UNKNOWNNAME。
请注意,“Caption”(成功)是 CommandBarControl 的属性,而“FaceId”(失败)是 CommandBarButton 子类的属性。按钮IDispatch* 的类名是CommandBarControl。所以我想我需要以某种方式将CommandBarControl IDispatch* 降级为CommandBarButton IDispatch*。
在 Visual Basic 中我可以编写:
button = DirectCast(control, CommandBarButton)
button.FaceId = 59
但我不知道DirectCast() 在内部做什么。如果我这样做了,我可能会接近解决这个问题。
谢谢
【问题讨论】:
-
多个调度接口的乐趣
-
感谢 Matt... 多个 IDispatch 接口,真的吗?这不是意味着 MS 违反了他们自己的规则,即一个对象只能有一个 IDispatch 接口吗?我的理解是:“只允许一个 IDispatch(使用 QueryInterface 和 IDispatch 应该总是返回相同的接口)”(codeguru.com/cpp/com-tech/atl/atl/article.php/c47/…)
-
允许多个调度接口,但某些语言无法访问它们。这就是为什么有时建议不要使用多个双接口的原因。例如,在 C++ 中,您在接口请求中指定 IID,因此您可以拥有
IDispatch *,但您可以请求IID_IFoo。如果对象将IFoo作为双接口,则此方法有效。 -
(不知道和你的问题有没有关系)
-
我希望我可以 QI ICommandBarButton 的 IDispatch 并提供编译的钩子让脚本直接调用它,但是我需要为脚本可能想要使用的每个其他类执行此操作. IDispatch 不是旨在确保脚本引擎不需要这些编译知识吗?
标签: c++ visual-studio-2010 com ole