Click to See Complete Forum and Search --> : Iwebbrowser & IHTMLDocument2


Soul_Eater
October 21st, 2004, 04:15 AM
goin a little nuts.

The following headers are included:

#pragma check_stack(off)
#pragma comment(linker,"/OPT:NOWIN98")
#pragma comment(lib, "comctl32.lib")
#include <windows.h>
#include <winuser.h>
#include <stdio.h>
#include <commctrl.h>
#include <windef.h>
#include <mshtml.h>
#include <atlbase.h>
#include <oleacc.h>
#import <shdocvw.dll>

Ok, in my code, I can't get the IHTMLDocument2 to navigate(if im doing something wrong tell me), so I'm trying to do it via Iwebbrowser2, and I'm getting compile errors. Someone please tell me whats wrong, thanks:

HWND web = FindWindowEx(shelldoc,NULL,"Internet Explorer_Server",NULL);

HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );

CComPtr<IHTMLDocument2> spDoc;
CComPtr<IWebBrowser2> pwb2;
LRESULT lRes;

UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( web, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );

LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
if ( pfObjectFromLresult != NULL )
{
HRESULT hr;
hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc );
if ( SUCCEEDED(hr) )
{
CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;
spWin->get_document( &spDoc.p );
// Change background color to red
// spDoc->put_bgColor( CComVariant("red") );

spWin->navigate((BSTR)"http://www.codeguru.com");
ObjectFromLresult(lRes, IID_IHTMLDocument, 0, (void**)&pwb2);
pwb2->Navigate((BSTR)"http://www.codeguru.com",NULL,NULL,NULL,NULL);
}
}
::FreeLibrary( hInst );
CoUninitialize();

These are my errors:

error C2039: 'Navigate' : is not a member of '_NoAddRefReleaseOnCComPtr<struct IWebBrowser2>'
error C2504: 'IWebBrowser2' : base class undefined see reference to class template instantiation 'ATL::_NoAddRefReleaseOnCComPtr<struct IWebBrowser2>' being compiled

kirants
October 21st, 2004, 03:04 PM
Are you including exdisp.h ?

Soul_Eater
October 21st, 2004, 03:24 PM
No I wasnt, just did though :)

and i got this error while Linking..

error LNK2001: unresolved external symbol _ObjectFromLresult@16

kirants
October 21st, 2004, 03:29 PM
Here is a general rule. And bury it deep in your grey cells :)

Whenever you call an API/ function.. 2 things to check out:
1. What header file to include to get the function signature.
2. What lib file if any to include to get to know which dll to link to.

Whenever you type in the function name, interface name etc in MSDN index, it will take you to the page explaining about it. At the very bottom you will find a Requirements section which will have all that info. How else do you think I gave you that info on exdisp.h ?? :p

Soul_Eater
October 21st, 2004, 03:38 PM
stuck a
#pragma comment(lib, "Oleacc.lib")

in there, no more compiling errors at all.

However, its not navigating at all either....

kirants
October 21st, 2004, 08:10 PM
Difficult to say.. did you check the return value of Navigate ?

Soul_Eater
October 21st, 2004, 09:49 PM
under another recommendation, i was using BSTR wrong...so now everything I have looks like this, and its not navigating:

HWND web = FindWindowEx(shelldoc,NULL,"Internet Explorer_Server",NULL);

HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );

CComPtr<IHTMLDocument2> spDoc;
CComPtr<IWebBrowser2> pwb2;
CComBSTR bstrURL((LPTSTR)"http://www.codeguru.com");
CComVariant vtEmpty;

LRESULT lRes;

UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( web, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );

LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
if ( pfObjectFromLresult != NULL )
{
HRESULT hr;
hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument2, 0, (void**)&spDoc );
if ( SUCCEEDED(hr) )
{
CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;
spWin->get_document( &spDoc.p );


ObjectFromLresult(lRes, IID_IWebBrowser2, 0, (void**)&pwb2);
pwb2->Navigate(bstrURL,&vtEmpty,&vtEmpty,&vtEmpty,&vtEmpty);
}
}
::FreeLibrary( hInst );
CoUninitialize();

hankdane
October 22nd, 2004, 08:29 PM
You do this -- correctly:


CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;
spWin->get_document( &spDoc.p );



Then you ignore this spWin in your navigate:


ObjectFromLresult(lRes, IID_IWebBrowser2, 0, (void**)&pwb2);
pwb2->Navigate(bstrURL,&vtEmpty,&vtEmpty,&vtEmpty,&vtEmpty);

With your code, I don't think you can know what IE Window the pwb2 object refers to. Looking for any "Internet Explorer Server" window can return anything, but even if you find the window you want, the actual interface object does not neccessarily represent that window.

A few things I would try: What happens if you call Navigate() on spWin instead? What happens if you call Open() on spWin? What happens if you pass in a frame name? On the latter, even if don't have the right window, you should get a new window with your URL.

If you haven't already, check out this:
http://www.codeguru.com/Cpp/I-N/ieprogram/article.php/c4383/
It's an article I wrote about attaching to a browser control in another app. It doesn't use Navigate though, only the DOM. You probably know most of it by now though.

Soul_Eater
October 23rd, 2004, 01:25 PM
I know I have the right window. I was reading the MSDN article on getting an IHTMLDocument2 from an HWND, and doing what it said worked, because I turned the background of the ie server window red.

I just want it to navigate to my specified URL, be it by IWebBrowser2 or IHTMLDocument2, and neither way is working.

Soul_Eater
October 23rd, 2004, 04:21 PM
figured out my problem. Got everything work :D thanks for everyones help!

jfloviou
October 6th, 2005, 10:20 AM
Hey Soul_Eater,

Would you mind explaining how you resolved the problem? I'm stuck in such matters...

My goal is to get the IWebBrowser from the window handle of "Internet Explorer_server"

I get well the IHTMLDocument2 but i do not manage to find a way to obtain the iwebbrowser then...

(I should add that i cannot use the parentWindow of the window: it's inside mshta use, so i do not have a "Shell DocObject View" window.

Can someone help?

Thx

Jeff