About the use of several HTML document interfaces

Source: Internet
Author: User
Tags object model

Problem:

In the process of using the Active Accessibility SDK, I get the IHTMLDocument2 pointer from a handle to a window. Is there any way to get the IWebBrowser2 pointer from the IHTMLDocument2 pointer? I tried it on two interfaces (IHTMLDocument2 and IWebBrowser2) with QueryInterface, but it didn't work. I also tried to get IOmNavigator * from Get_navigator using Netscape's HTMLWindow2 pointers. Also ended in failure. Please advise the master.

Answer:

This problem is usually a common problem in COM programming. You have windows, documents, or browsers, and you know you can get other information through these known data, but often in a real-world environment, QueryInterface always sends you back a fat null. The answer to this question is actually hidden in the mysterious IServiceProvider interface, as the name suggests, the role of IServiceProvider is to provide services. IServiceProvider is a great interface: it has only one method--queryservice. If you would use an ATL smart pointer, just like the following. First you must obtain the IServiceProvider interface,

ccomqiptr ISP = PIHTMLDocument2;

This line of code actually performs a QueryInterface of the document to inquire about the IServiceProvider interface. Once you have an ISP, you can get the browser as follows.

CComQIPtr iwb2;

Isp->queryservice (Iid_iwebbrowserapp,iid_iwebbrowser2, (void**) &iwb2);

If you still don't understand what is said above, it doesn't matter, it's normal. A fundamental rule of COM programming is that QueryInterface must always return the interface of the object being queried. But the document does not implement the IWebBrowser2 interface, it only knows how to get the object that is working. Documents, browsers, and windows are separate objects. Typically, IServiceProvider is used in a number of separate and related COM object groups to implement a service of some kind. QueryInterface asks an object: "Do you implement this interface?" and QueryService tells a service provider, "Whatever object implements this interface, tell me." "The interface pointer returned using QueryService may or may not be the same as the object being queried." As shown in figure one. All objects implement their own different interfaces and store pointers internally that point to another object. You must use the IServiceProvider interface to obtain an object of a particular interface, regardless of which object it is implemented. IServiceProvider::QueryService to follow these internal pointers to get the interface object that you want to implement.

Figure a multiple object, a IServiceProvider

Essentially, IServiceProvider is used to navigate the DHTML object hierarchy. Let's say you're writing an ActiveX control to navigate the object model. How do you do it? First of all, like the following query IOleClientSite to obtain IServiceProvider:

ccomqiptr ISP = Psite;

Then, once you have the IServiceProvider, you can use QueryService to query the Application object from.

CComQIPtr IWBA;

Isp->queryservice (Iid_iwebbrowserapp, Iid_iwebbrowserapp, (void * *) &IWBA);

You can then navigate the object hierarchy (the Application object is at the top level). If you want to get a Web browser, it's similar to the previous one.

CComQIPtr iwb2;

Isp->queryservice (Iid_iwebbrowserapp, Iid_iwebbrowser2, (void * *) &iwb2));

In all of these examples, Sid_swebbrowserapp are service flags, but you can often see code that takes Iid_iwebbrowserapp as a service ID. Both usages can work because there is a macro definition in the file:

#defines Sid_swebbrowserapp Iid_iwebbrowserapp

But from a programmatic point of view, Sid_swebbrowserapp is technically more correct and clearer to the person who reads the code.

Also, if you have the courage to implement a huge object system like the DHTML object model, you need to use the IServiceProvider interface ...

The answer to this question I can only briefly explain to this point, and then go deeper, I also a cha cha. More in-depth discussion please see the MSDN Library.

As a result of the personal level limit, to the solution existence error and the unknown place, invites the brothers Haihan.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.