Solved

Spying on Internet Explorer

Posted on 2001-08-08
19
1,220 Views
Last Modified: 2012-05-04
Please can you save me alot of time telling me what interfaces that i need if i wish to get or set data in controls that belong to Internet Explorer 3.x and 4->5.x

I would be gratefull if you could help me on this.

Regards
Andla
0
Comment
Question by:andla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 2
  • +2
19 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6366643
Example code:

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

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 var;

     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"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);
     pWB->Refresh();     //Refresh the web page on IE

     //Change web page to the EE C++ topic area
     bstrVal = SysAllocString (L"www.experts-exchange.com/jsp/qList.jsp?ta=cplusprog");
     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);
     
     AfxMessageBox("Press enter when you want be to click BACK button on IE");
     pWB->GoBack();

     AfxMessageBox("Press enter when you want be to click HOME button on IE");
     pWB->GoHome();


     OleUninitialize ();
     return 1;
}
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6366843
Axter, this code won't work on anything older than IE5. The OP asked for support for IE3.x.
0
 
LVL 1

Author Comment

by:andla
ID: 6367020
IID_IWebBrowser2 should be supported on 4.0 but not on 3.x
IID_IWebBrowser is supported on 3.x

The qestion is will it work with IID_IWebBrowser. I will try that laiter. Thanks for the code. I will try it now.

/Andreas
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:andla
ID: 6367045
Thanks! I have tested this now. Very cool :-)

I need to know about setting and getting data. I mean for editbox, combobox , menues and other stuff. I know this could perhaps be done by reading the htmlcode, but i need to emulate user input.
Can this be done ?
I would be very happy if you could help me.

/Andla
0
 
LVL 30

Expert Comment

by:Axter
ID: 6367103
>>I need to know about setting and getting data. I mean
>>for editbox, combobox , menues and other stuff.

Did you look at the code I posted on your other two questions?
In one of your other two questions, I posted information on how to get help on IDD_?? objects.

If you look in the VC++ help document index under "IHTML??????", you'll find a list of HTML objects that you can use with IE to control it.

I'll take a look at it later on today, and give you some feedback on it.
0
 
LVL 1

Author Comment

by:andla
ID: 6367188
Thanks Axter!

/Andla
0
 
LVL 30

Expert Comment

by:Axter
ID: 6369780
AssafLavie,
What does OP stand for?
0
 
LVL 30

Expert Comment

by:Axter
ID: 6371611
From looking at the help document you would use the following classes to get or set controls:

IHTMLButtonElement
IHTMLTextAreaElement
IHTMLTextElement
IHTMLSelectElement
IHTMLOptionElement
IHTMLOptionButtonElement
IHTMLInputButtonElement
IHTMLInputElement

Some other miscellaneous Element objects:

IHTMLControlElement
IHTMLLinkElement
IHTMLOListElement
IHTMLMetaElement
IHTMLTitleElement
IHTMLImgElement
IHTMLParaElement
IHTMLParamElement
IHTMLObjectElement
0
 
LVL 30

Expert Comment

by:Axter
ID: 6371830
#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"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;
}


0
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 6371835
The above code shows you an example on 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.
0
 
LVL 1

Author Comment

by:andla
ID: 6377047
Thanks Axter!

It works with TEXTAREA but not with the INPUT. I did some changes because i don't use MFC. Not sure why this don't work.


          else if (strcmp(TempVariable,"INPUT")==0)
          {
               InputElementQty++;
               if (InputElementQty == 1)
               {
                    IHTMLOptionButtonElement *pOptionButtonElement = NULL;
                    pdisp2->QueryInterface (IID_IHTMLOptionButtonElement , (LPVOID *) &pOptionButtonElement);
                    if (pOptionButtonElement == NULL)
                    {//Report error here
                         MessageBox(0,"Error:  pOptionButtonElement == NULL","",0);
                         return 0;
                    }
                    VARIANT_BOOL bIsChecked = TRUE;
                    pOptionButtonElement->put_checked(bIsChecked);
               }
          }

Cheers
Andla





0
 
LVL 1

Author Comment

by:andla
ID: 6377054
pOptionButtonElement == NULL
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6377057
Axter:
OP  = Original Poster

newsgroup lingo.
0
 
LVL 1

Author Comment

by:andla
ID: 6377102
Ok it works in the answer mode :-)
Thanks Axter! You have helped me alot and i'm very grateful.

Cheers
Andla
0
 
LVL 30

Expert Comment

by:Axter
ID: 6380089
AssafLavie,
>>OP  = Original Poster
Thanks for the info.

andla,
>>Ok it works in the answer mode
Sorry for the late reply.  My home computer is temporarily down.
Do you have everything working now?

To tell you the truth, before you asked this question, I never realized that IE has some many built-in COM controls.
0
 
LVL 1

Author Comment

by:andla
ID: 6380537
A whole bunch of them ;-)

Things are working fine now. The hard thing is to now what interface is used for a specific tag.  I also noticed that it's kind of slow to list all the tags. Am i forced to use IDispatch (automation)? I have lot of candy to learn about and it's intresting what new stuff it can reveal.

Cheers
Andla
0
 

Expert Comment

by:deefstes
ID: 8494506
Imagine my elation when I stumbled across this discussion, it directly addresses a problem I'm currently sitting on. I still have some problems getting it to work though so I'd like to ask the question here if you can bear with me.

I'm trying to populate an INPUT field of type TEXT for which IHTMLTextAreaElement wouldn't apply as it is only for multiline text input control. So I was thinking that IHTMLTextElement would be appropriate but that doesn't have a put_value member so the following code will not work.

IHTMLTextElement  *pTextElement = NULL;
pdisp2->QueryInterface (IID_IHTMLTextElement, (LPVOID *) &pTextElement);
BSTR bstrHelloWorld = SysAllocString (L"Hello World.  This really works!");
pTextElement->put_value(bstrHelloWorld);
SysFreeString (bstrHelloWorld);

Would you mind helping me out here please?

All the best
Déwald
0
 

Expert Comment

by:david_johns
ID: 8628204
This was a great starting course for me working with COM on IE.  Does anyone know how to click a button.  I can get to the IHTMLInputButtonElement that I need to click, but there is no click() member function or anything convienient like in VB.  Does anyone know how to accomplish this seemingly meanial task?

Thanks,
David Johns
0
 

Expert Comment

by:david_johns
ID: 8637820
Nevermind - I was able to answer my own question.  You have to first get the form and then submit the form.  For instance if you know the value of the button you can cycle over the button values until finding the right one and then

pMyInputButton->get_form(&pMyButtonsForm);
pMyButtonsForm->submit();

Thanks.
David Johns
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question