user9567282
user9567282

Reputation:

Knowing what interfaces to query on a COM object

I was looking at a code sample by Raymond Chen, available here. From what I understand, he obtains a shell item using the IShellWindows interface. Then, using that item's IDispatch interface and a call to QueryInterface, he hops over to the item's IWebBrowserApp interface. And then a few lines later, it appears that he hops over to the item's IServiceProvider interface. My question is, before using QueryInterface, how would you even know that an IShellWindows item might support the IWebBrowserApp and IServiceProvider interfaces? For example, I don't see any documentation listing all the interfaces that an IShellWindows item supports.

Upvotes: 2

Views: 694

Answers (1)

Anders
Anders

Reputation: 101746

MSDN does not usually tell you which interfaces a object implements but if you look around you will often find some documentation and related interfaces you can QI. And just to make it clear, a interface is just a contract and multiple objects can implement a certain interface so you can't really blame Microsoft for not having a definitive list.

Let's try to pick apart your specific example.

The object that implements IShellWindows (CLSID_ShellWindows) does not really have any other interesting interfaces, you just care about its list of windows.

IShellWindows -> (IDispatch ->) IWebBrowserApp:

IShellWindows has a collection of open Internet Explorer and Explorer windows. For whatever reason it just gives you a IDispatch for each window instead of letting you ask for a specific interface. Possibly just because IShellWindows is also scriptable by Windows Scripting Host/Visual Basic and IDispatch plays a big role there.

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.

..and the connection between IShellWindows and 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.

The fact that the web browser and shell are connected like this should be no surprise for people that were interested in Windows around the Windows 98/IE 4 time frame. Internet Explorer and File Explorer were basically the same thing; Explorer could display web pages and IE could display the "file list" (IShellView).

IShellBrowser -> IShellView:

just a simple call to QueryActiveShellView.

There is a key point here; IShellFolder/IShellView can be implemented by a 3rd-party shell extension. Explorer implements IShellBrowser and it is IShellBrowser that hosts IShellView, and 3rd-party ISVs can also create file browsers that implement IShellBrowser. In theory you could have a file explorer app created by one company hosting a shell view created by a different company with no Microsoft code involved. IShellBrowser and IShellView is how they see each other.

IShellView -> IFolderView:

There is no direct connection here but if you look around you can connect the dots.

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.]

In other cases where you can't find specific documentation you just have to try to query for interfaces you are interested in. The shell also has a ton of undocumented interfaces and a debugger is your only choice if you want to experiment with those.

Upvotes: 3

Related Questions