Solved

Spying on Internet Explorer

Posted on 2001-08-08
19
1,185 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
  • 7
  • 7
  • 2
  • +2
19 Comments
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:andla
Comment Utility
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
Comment Utility
>>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
Comment Utility
Thanks Axter!

/Andla
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
AssafLavie,
What does OP stand for?
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
Comment Utility
#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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
pOptionButtonElement == NULL
0
 
LVL 4

Expert Comment

by:AssafLavie
Comment Utility
Axter:
OP  = Original Poster

newsgroup lingo.
0
 
LVL 1

Author Comment

by:andla
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

  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 …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now