Hi,

     I am trying to use web browser object, access its DOM, add, remove elements, use IDocHostUIHandler, etc. I have realized that BCL sometimes doesn't give interface pointer, neither supports those interfaces. Please see my example below:


template<typename T, REFIID riid>
T* NativeQIOnObject(System::Object^ o)
{
 IntPtr punkMngd = System::Runtime::InteropServices::Marshal::GetIUnknownForObject(o);
 IUnknown* punk = static_cast<IUnknown*>(punkMngd.ToPointer());
 T* pT = NULL;
 HRESULT hr = punk->QueryInterface(riid, (void**)&pT);
 return pT;
}


 


This template simply QIs for object for a given interface. It doesn't work for following objects that I need:

IHTMLImgElement
IHTMLDocument2

And I could not find HtmlImgElement in BCL.

The function:


void foo(System::Windows::Forms::WebBrowser^ hBrowser)
{
HtmlDocument^ hdoc = hBrowser->Document;

IHTMLDocument2 *pdoc = NativeQIOnObject<IHTMLDocument2, IID_IHTMLDocument2>(hdoc);
// (can't) use pdoc
}

 


returns E_NOINTERFACE. The System::Windows::Forms::HtmlDocument::NativeHtmlDocument2 I see in IntelliSense has no accessible get method.

I can, however, access IHTMLDocument2 through IWebBrowser2 interface. I mean, if I do something like following:



IHTMLDocument2* pdoc = NULL;
IWebBrowser2* pwb = NULL;

pwb = NativeQIOnObject<IWebBrowser2, IID_IWebBrowser2>(managedBrowserHandle);
pwb->get_Document((IDispatch**)&pdoc);
// (can) use pdoc

 


My code tries to create IMG element, inserts into document, and deals with its events. This works perfect in native code and I am trying to port it to .NET.

Is there another way to obtain those interfaces or do we have to write COM code after we have a handle to web browser?

Ismail