Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1257
  • Last Modified:

Spying on Internet Explorer

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
andla
Asked:
andla
  • 7
  • 7
  • 2
  • +2
1 Solution
 
AxterCommented:
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
 
AssafLavieCommented:
Axter, this code won't work on anything older than IE5. The OP asked for support for IE3.x.
0
 
andlaAuthor Commented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
andlaAuthor Commented:
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
 
AxterCommented:
>>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
 
andlaAuthor Commented:
Thanks Axter!

/Andla
0
 
AxterCommented:
AssafLavie,
What does OP stand for?
0
 
AxterCommented:
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
 
AxterCommented:
#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
 
AxterCommented:
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
 
andlaAuthor Commented:
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
 
andlaAuthor Commented:
pOptionButtonElement == NULL
0
 
AssafLavieCommented:
Axter:
OP  = Original Poster

newsgroup lingo.
0
 
andlaAuthor Commented:
Ok it works in the answer mode :-)
Thanks Axter! You have helped me alot and i'm very grateful.

Cheers
Andla
0
 
AxterCommented:
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
 
andlaAuthor Commented:
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
 
deefstesCommented:
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
 
david_johnsCommented:
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
 
david_johnsCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 7
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now