We help IT Professionals succeed at work.

Looking for a simple IWebBrowser2 sample

The Master
The Master asked
on
17,110 Views
Last Modified: 2013-11-25
I'm looking for a simple example showing how to create a WebBrowser control - NOT using MFC.

Ideally I would like a sample EXE that creates a main window which in turn contains the WebBrowser control. Every example I can find uses MFC. I'm in the process of learning ATL, so an ATL example would be fine, but even a straight bare-bones COM example would do.

Anyone know where I can find what I'm looking for? Thanks in advance!
Comment
Watch Question

Senior Software Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
AxterSenior Software Engineer
CERTIFIED EXPERT

Commented:
Here's another example:

#include <windows.h>
#include <mshtml.h>
#include <exdisp.h>

#pragma warning(disable:4786)
#include <vector>
#include <string>



int function1 (void)
{
     CLSID clsid;
     LPUNKNOWN punk=NULL;
     IWebBrowser2 *pWB = NULL;
     IHTMLDocument2 *pHTML = NULL;
     IHTMLElementCollection *pElement = NULL;
     IDispatch *pdisp = NULL;
     DISPID  dispidNamed = DISPID_PROPERTYPUT;
     VARIANT_BOOL bBusy;
     VARIANT varName, varIndex, var;
     BSTR bstr;

     OleInitialize (NULL);
     CLSIDFromProgID (OLESTR("InternetExplorer.Application"), &clsid);
     CoCreateInstance (clsid, NULL, CLSCTX_SERVER, IID_IUnknown, (LPVOID *) &punk);
     punk->QueryInterface (IID_IWebBrowser2, (LPVOID *) &pWB);
     punk->Release();
     pWB->put_Visible (TRUE);
     BSTR bstrVal = SysAllocString (L"https://www.experts-exchange.com/jsp/qShow.jsp?qid=20165536");
     var.vt = VT_I4;
     var.lVal = 0;
     pWB->Navigate (bstrVal, &var, &var, &var, &var);
     SysFreeString (bstrVal);
     do
     {
      Sleep (500);
      pWB->get_Busy (&bBusy);
     }
     while (bBusy);
     pWB->get_Document (&pdisp);
     if (pdisp == NULL)
     {//Report error here
          return 0;
     }
     pdisp->QueryInterface (IID_IHTMLDocument2, (LPVOID *) &pHTML);
     pdisp->Release();
     pHTML->get_all (&pElement);
     long NumItems = 0;
     pElement->get_length(&NumItems);
     if (!NumItems)
     {//Report error here
          return 0;
     }

     CStdioFile TagFile("tagfile.txt",CFile::modeCreate|CFile::modeWrite);
     std::vector<std::string> ListOfTagNames;
     std::vector<std::string> ListOfInnerHTML;
     std::vector<std::string> ListOfOuterHTML;
     int InputElementQty = 0;
     for (int item_number_index = 0;item_number_index < NumItems;item_number_index++)
     {
          varName.vt = VT_I4;
          varName.lVal = item_number_index;//Get item via index number (zero based number)
          varIndex.vt = VT_I4;
          varIndex.lVal = 1;
          IDispatch *pdisp2 = NULL;
          if (pElement->item (varName, varIndex, &pdisp2) != S_OK)
          {//Report error here
                return 0;
          }
          if (pdisp2 == NULL)
          {//Report error here
                return 0;
          }
          IHTMLElement *pItem = NULL;
          pdisp2->QueryInterface (IID_IHTMLElement, (LPVOID *) &pItem);
          pdisp2->Release();
          if (pItem == NULL)
          {//Report error here
                return 0;
          }
          CString TempVariable;


          pItem->get_tagName(&bstr);
          TempVariable = bstr;
          std::string szTagName = (LPCTSTR)TempVariable;
          ListOfTagNames.push_back(szTagName);
          TagFile.WriteString(TempVariable);
          TagFile.WriteString("\n");

          if (TempVariable == "TEXTAREA")
          {
               IHTMLTextAreaElement  *pTextAreaElement = NULL;
               pdisp2->QueryInterface (IID_IHTMLTextAreaElement, (LPVOID *) &pTextAreaElement);
               if (pTextAreaElement == NULL)
               {//Report error here
                    AfxMessageBox("Error:  pTextAreaElement == NULL");
                    return 0;
               }
               BSTR bstrHelloWorld = SysAllocString (L"Hello World.  This really works!");
               pTextAreaElement->put_value(bstrHelloWorld);
               SysFreeString (bstrHelloWorld);
          }
          else if (TempVariable == "INPUT")
          {
               InputElementQty++;
               if (InputElementQty == 2)
               {
                    IHTMLOptionButtonElement *pOptionButtonElement = NULL;
                    pdisp2->QueryInterface (IID_IHTMLOptionButtonElement , (LPVOID *) &pOptionButtonElement);
                    if (pOptionButtonElement == NULL)
                    {//Report error here
                         AfxMessageBox("Error:  pOptionButtonElement == NULL");
                         return 0;
                    }
                    VARIANT_BOOL bIsChecked = TRUE;
                    pOptionButtonElement->put_checked(bIsChecked);
               }
          }
               
          pItem->get_innerHTML(&bstr);
          TempVariable = bstr;
          std::string szInnerHTML = (LPCTSTR)TempVariable;
          ListOfInnerHTML.push_back(szInnerHTML);

          pItem->get_outerHTML(&bstr);
          TempVariable = bstr;
          std::string szOuterHTML = (LPCTSTR)TempVariable;
          ListOfOuterHTML.push_back(szOuterHTML);

          pItem->Release();
     }
     AfxMessageBox("Move the web page to the bottom, and you'll notice that ANSWER is selected, and HELLO
WORLD in the textarea.\n\nDon't click OK until after you view changes.");
     TagFile.Close();
     pWB->Quit();
     pWB->Release();
     OleUninitialize ();
     return 1;
}


AxterSenior Software Engineer
CERTIFIED EXPERT

Commented:
The last example code shows you how to use the Control Element Objects to make modifications
to the Internet Explorer.

The code opens this EE question.
Then it changes the COMMENT/ANSWER selection so that ANSWER is selected.
Then it puts some text into the Comment-Textarea.
AxterSenior Software Engineer
CERTIFIED EXPERT

Commented:
If you need more information send me an email at "david@axter.com", and I'll post answer here.

The Email notification is not working, so if you just post comments here, I may not see it.

Author

Commented:
Thanks for both of your comments.  However, it's not quite what I'm looking for.  What I need is an example of how to create a simple main window, in which I can create a MS Web Browser control.  I'm not real clear on what the minimal requirements are for the main window (in order to act as a container for the web browser control), nor am I very clear on how to attach the web browser control to the main window, once the main window is created.

I don't need anything else - no menu, no toolbars, no address bar, etc.  Just a simple window that would have the web browser control as a child.  I eventually want to have as much control over this window and it's message loop as possible, so I'd prefer that it actually be a window, and not a dialog box.

Thanks in advance for any further assistance!
AxterSenior Software Engineer
CERTIFIED EXPERT

Commented:
Here's some example code that will put IE into your application.
All you have to do is pass the handle to your application window.


#include <mshtml.h>
#include <exdisp.h>
CLSID clsid;
LPUNKNOWN punk=NULL;
IWebBrowser2 *pWB = NULL;
IHTMLDocument2 *pHTML = NULL;
IHTMLElementCollection *pElement = NULL;
IDispatch *pdisp = NULL;
DISPID  dispidNamed = DISPID_PROPERTYPUT;
VARIANT_BOOL bBusy;
VARIANT var;

void PutIE_InWindow(HWND hWnd)
{
     OleInitialize (NULL);
     CLSIDFromProgID (OLESTR("InternetExplorer.Application"), &clsid);
     CoCreateInstance (clsid, NULL, CLSCTX_SERVER, IID_IUnknown, (LPVOID *) &punk);
     punk->QueryInterface (IID_IWebBrowser2, (LPVOID *) &pWB);
     pWB->put_Visible (TRUE);
     BSTR bstrVal = SysAllocString (L"https://www.experts-exchange.com"); //Set web page on  IE
     var.vt = VT_I4;
     var.lVal = 0;
     pWB->Navigate (bstrVal, &var, &var, &var, &var);
     SysFreeString (bstrVal);
     do
     {
          Sleep (500);
          pWB->get_Busy (&bBusy);
     }
     while (bBusy);
     
     
     long IE_HWND = 0;
     if (pWB->get_HWND(&IE_HWND) != S_OK)
     {
        AfxMessageBox("Failed: pWB->get_HWND");
        return;
     }

     HWND IEhWnd = (HWND)IE_HWND;

     //Code to put IE into a window
     //This will put IE into your app window
     ::SetParent(IEhWnd ,hWnd);

}

Commented:
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "Axter" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: https://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
Per recommendation, force-accepted by
Netminder
CS Moderator
Subrat (C++ windows/Linux)Software Engineer
CERTIFIED EXPERT

Commented:
Hello
The answer accepted is uses MFC.( CString , etc...). But the user has strictly denying to use MFC.

Please convert the code without using MFC. Whatever it may be but really Good!

Thanks
Subrat.
Subrat (C++ windows/Linux)Software Engineer
CERTIFIED EXPERT

Commented:
If u need the complete code, plz let me know!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.