蒋晟大侠:基于对话框的程序中放置了一个WebBrowser控件,怎样在另外一个程序中获得该WebBrowser的IHTMLDocument?

布学无数 2006-02-09 07:41:15
同题!
...全文
177 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
布学无数 2006-02-12
  • 打赏
  • 举报
回复
强悍啊!!!
这种办法都想的到,真是佩服那些高人们哪!!!
Kudeet 2006-02-10
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/msdnmag/issues/01/06/c/default.aspx

Q
I use the Active Accessibility SDK to obtain an IHTMLDoc-ument2 pointer from a window handle (HWND). Is there any way to obtain an IWebBrowser2 pointer from the IHTMLDocument2 pointer? I've tried QueryInterface on both the document pointer as well as the IHTMLWindow2 pointer without results. I also tried the IOmNavigator * from get_navigator on the HTMLWindow2 pointer. Any ideas?

A
This is a common cause for COM consternation. You have the window, document, or browser and you know you should be able to get the others, but everywhere you turn, QueryInterface delivers a big fat NULL. The answer lies in the mysterious IServiceProvider whose job it is, well, to provide services. IServiceProvider is a great interface: it has only one method, QueryService. If you're using ATL smart pointers, it looks like this. First, you have to get the IServiceProvider.

CComQIPtr<IServiceProvider> isp = pIHTMLDocument2;

This does a QueryInterface on the document for IServiceProvider. Once you have it, you can get the browser like so.

CComQIPtr<IWebBrowser2> iwb2;
isp->QueryService(IID_IWebBrowserApp,
IID_IWebBrowser2, (void**)&iwb2);

If all this seems confusing, there's a good reason for it. A cardinal rule of COM is that QueryInterface must always return an interface to the object queried. But the document doesn't implement IWebBrowser2; it only knows how to get the object that does. The document, browser, and window are all separate objects. In general, IServiceProvider is used whenever a bunch of separate but related COM objects together implement some kind of service. QueryInterface asks an object, do you implement this interface? QueryService tells a service provider, "get me whatever object implements this interface, please." With QueryService, the interface pointer returned may or may not be the same object as the one queried. Figure 6 illustrates the system. All the objects implement their various interfaces and store internal pointers to one another; IServiceProvider is your way to get whichever object implements a particular interface. IServiceProvider::QueryService chases the internal pointers to retrieve the object that implements the interface you want.


Figure 6 Many Objects, One IServiceProvider

IServiceProvider is essential for navigating the DHTML object hierarchy. Say you're writing an ActiveX® control and you want to navigate the object model. How do you do it? First, query your IOleClientSite for IServiceProvider like so:

CComQIPtr<IServiceProvider> isp = pSite;

then, once you have IServiceProvider, you can QueryService it for the application object.

CComQIPtr<IWebBrowserApp> iwba;
isp->QueryService(IID_IWebBrowserApp,
IID_IWebBrowserApp, (void **)&iwba);

Now you can navigate the object hierarchy (the app object is at the top). If you want the Web browser, it's the same as before.

CComQIPtr<IWebBrowser2> iwb2;
isp->QueryService(IID_IWebBrowserApp,
IID_IWebBrowser2, (void **)&iwb2));

In all these examples, SID_SWebBrowserApp identifies the service, but you'll often see code that uses IID_IWebBrowserApp as the service ID. Either will work, since <shlguid.h> #defines SID_SWebBrowserApp to IID_IWebBrowserApp, but for programming pedants SID_SWebBrowserApp is technically more correct, and more clear to anyone reading your code.
By the way, if you're brave enough to implement some far-reaching colossal object system such as the DHTML object model (Lord help you!), then you should implement IServiceProvider too.
布学无数 2006-02-10
  • 打赏
  • 举报
回复
谢谢,这个问题已经解决了!!!

如果我要得到IWebBrowser接口呢?又该如何做啊?
蒋晟 2006-02-10
  • 打赏
  • 举报
回复
Write a COM server (create the application with automation support, and add a property exporting the IDispatch interface of the WebBrowser control.

Another way is to register a systemwide message with RegisterWindowMessage and return the HWND of the WebBrowser control. The other application can send this message to the dialog application and use the HWND of the WebBrowser control to retrieve the IHTMLDocument interface of the HTMLDocument obejct(see MSDN
KB Q249232 HOWTO: Get IHTMLDocument2 from a HWND http://support.microsoft.com/support/kb/articles/Q249/2/32.asp).

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧