知道查询 COM 对象的接口

Knowing what interfaces to query on a COM object

我在看 Raymond Chen 的代码示例,可用 here。据我了解,他使用 IShellWindows 接口获得了一个 shell 项目。然后,使用该项目的 IDispatch 界面和对 QueryInterface 的调用,他跳转到该项目的 IWebBrowserApp 界面。几行之后,他似乎跳到了项目的 IServiceProvider 界面。我的问题是,在使用 QueryInterface 之前,您怎么知道 IShellWindows 项目可能支持 IWebBrowserAppIServiceProvider 接口?例如,我没有看到任何文档列出 IShellWindows 项支持的所有接口。

MSDN 通常不会告诉您一个对象实现了哪些接口,但如果您环顾四周,您通常会发现一些文档和相关接口,您可以对其进行 QI。需要说明的是,一个接口只是一个契约,多个对象可以实现一个特定的接口,所以你不能真的责怪微软没有一个明确的列表。

让我们试着把你的具体例子分开。

实现了IShellWindows(CLSID_ShellWindows)的对象其实并没有其他有趣的接口,你只关心它的windows.

列表

IShellWindows -> (IDispatch ->) IWebBrowserApp:

IShellWindows 有打开的 Internet Explorer 和 Explorer windows 的集合。无论出于何种原因,它只是为每个 window 提供一个 IDispatch,而不是让您请求特定的接口。可能只是因为 IShellWindows 也可以通过 Windows Scripting Host/Visual Basic 和 IDispatch 在那里发挥重要作用。

The Shell windows collection includes file explorer windows and web browser windows Internet Explorer and 3rd-party web browsers). Normally each Shell window implements IDispatch; IShellWindows::Item and IShellWindows::FindWindowSW provide ways to access a Shell window's IDispatch interface.

..以及IShellWindows和IWebBrowserApp/IWebBrowser2之间的连接:

exdisp.h contains the following programming interfaces

  • IShellWindows
  • IWebBrowser2

IWebBrowserApp -> IShellBrowser:

Objects that have access to the site chain of the browser can get a reference to the browser on IShellBrowser using IServiceProvider::QueryService, with Service IDs such as SID_STopLevelBrowser and SID_SCommDlgBrowser. See the Knowledge Base article Retrieve the Top-Level IWebBrowser2 Interface from an ActiveX Control for more information on using service IDs.

对于在 Windows 98/IE 4 时间范围内对 Windows 感兴趣的人来说,Web 浏览器和 shell 是这样连接的事实应该不足为奇。 Internet Explorer 和 File Explorer 基本上是一回事; Explorer可以显示网页,IE可以显示“文件列表”(IShellView)。

IShellBrowser -> IShellView:

只需调用 QueryActiveShellView

这里有个重点; IShellFolder/IShellView 可以通过第 3 方 shell 扩展来实现。 Explorer 实现了 IShellBrowser,它是托管 IShellView 的 IShellBrowser,第 3 方 ISV 也可以创建实现 IShellBrowser 的文件浏览器。理论上,您可以拥有一个由一家公司创建的文件资源管理器应用程序托管由另一家公司创建的 shell 视图,而不涉及 Microsoft 代码。 IShellBrowser 和 IShellView 是他们如何看待彼此的。

IShellView -> IFolderView:

这里没有直接的联系,但如果你环顾四周,你可以把这些点联系起来。

IShellFolderView is supported by the IShellView object that is returned from SHCreateShellFolderViewEx

[IShellFolderView is no longer available for use as of Windows 7. Instead, use IFolderView2 and IFolderView.]

在其他找不到特定文档的情况下,您只需要尝试查询您感兴趣的接口。shell 也有大量未记录的接口,调试器是您唯一的选择如果你想尝试这些。