Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. setting form element values using ie TCppWebBrowser control

setting form element values using ie TCppWebBrowser control

Scheduled Pinned Locked Moved C / C++ / MFC
c++
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrew J Burke
    wrote on last edited by
    #1

    This is what I've come up with so far but need to find & add values to the form elements for user input and password input, then submit. I'm using C++Builder6. #include // TComInterfaceHTMLDoc; Browser->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&HTMLDoc); //get all the forms TComInterfaceHTMLforms; HTMLDoc->get_forms(&HTMLforms); //Get form 0 since login pages have only one form TComInterfaceHTMLform; //Get User,Password fields & set values TComInterfaceHTMLfield; //Then submit form HTMLform->submit(); Thanks!

    A 1 Reply Last reply
    0
    • A Andrew J Burke

      This is what I've come up with so far but need to find & add values to the form elements for user input and password input, then submit. I'm using C++Builder6. #include // TComInterfaceHTMLDoc; Browser->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&HTMLDoc); //get all the forms TComInterfaceHTMLforms; HTMLDoc->get_forms(&HTMLforms); //Get form 0 since login pages have only one form TComInterfaceHTMLform; //Get User,Password fields & set values TComInterfaceHTMLfield; //Then submit form HTMLform->submit(); Thanks!

      A Offline
      A Offline
      App_
      wrote on last edited by
      #2

      I hope this Boland C++ Builder 6 code might work..

      #include utilcls.h
      #include Mshtml.h

      template<class ParentIntf, class ItemIntf>
      HRESULT __fastcall GetHTMLItem(ParentIntf *CollectionOrElement, const
      WideString &name, ItemIntf** ppIntf)
      {
      TVariant vName = name;
      TVariant vIndex = 0;
      TComInterface disp;

      HRESULT hRes = CollectionOrElement->item(vName, vIndex, &disp);
      if( SUCCEEDED(hRes) )
      hRes = disp->QueryInterface(\_\_uuidof(ItemIntf),(LPVOID\*)ppIntf);
      return hRes;
      

      }

      void __fastcall TForm1::CppWebBrowser1DocumentComplete (TObject*
      Sender, LPDISPATCH pDisp, TVariant *URL)
      {
      if( CppWebBrowser->Document )
      {
      TComInterface HTMLDoc;
      if(SUCCEEDED(CppWebBrowser->Document->QueryInterface(
      IID_IHTMLDocument2,(LPVOID*)&HTMLDoc) ) )
      {
      TComInterface forms;
      if( SUCCEEDED(HTMLDoc->get_forms(&forms)) )
      {
      TComInterface form;
      if( SUCCEEDED(GetHTMLItem(forms, "loginform", &form)) )
      {
      TComInterface userid;
      TComInterface password;

      			GetHTMLItem(form, "userid", &userid);
      			GetHTMLItem(form, "password", &password);
      
      			if( userid )
      			userid->put\_value(WideString("username"));
      
      			if( password )
      			password->put\_value(WideString("password"));
      			}
      		}
      	}
      }
      

      }

      I hope this Boland C++ Builder 6 code might work too..

      bool TForm1::Login(char* inputname, const AnsiString& text)
      {
      bool done = false;

      IHTMLDocument2* HTMLDoc = NULL;

      if(SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&HTMLDoc)))
      {
      IHTMLElementCollection* pAll = NULL;
      if(SUCCEEDED(HTMLDoc->get_all(&pAll)))
      {
      //

      Variant name = inputname;
      Variant index = 0;

      IDispatch* pDisp = NULL;

      if(SUCCEEDED(pAll->item(name, index, &pDisp)))
      {
      if ( pDisp )
      {
      // IHTMLInputFileElement* pInput = NULL; // mshtml.h
      IHTMLInputElement* pInput = NULL; // mshtml.h
      // IHTMLFormElement* pForm = NULL; // mshtml.h

      pDisp->QueryInterface(IID\_IHTMLInputElement, (LPVOID\*)&pInput);
      pDisp->Release();
      
      if(pInput)
      {
       pInput->put\_value(WideString(text));
      
       done = true;
      
        /\*
        WideString mybuffer;
        pInput->get\_value ( &mybuffer );
        ShowMessage ( mybuffer );
        \*/
      
       pInput->Release();
       }
      }
      

      }
      pAll->Release();
      }

      HTMLDoc->Release();
      }
      return done;
      }

      A 1 Reply Last reply
      0
      • A App_

        I hope this Boland C++ Builder 6 code might work..

        #include utilcls.h
        #include Mshtml.h

        template<class ParentIntf, class ItemIntf>
        HRESULT __fastcall GetHTMLItem(ParentIntf *CollectionOrElement, const
        WideString &name, ItemIntf** ppIntf)
        {
        TVariant vName = name;
        TVariant vIndex = 0;
        TComInterface disp;

        HRESULT hRes = CollectionOrElement->item(vName, vIndex, &disp);
        if( SUCCEEDED(hRes) )
        hRes = disp->QueryInterface(\_\_uuidof(ItemIntf),(LPVOID\*)ppIntf);
        return hRes;
        

        }

        void __fastcall TForm1::CppWebBrowser1DocumentComplete (TObject*
        Sender, LPDISPATCH pDisp, TVariant *URL)
        {
        if( CppWebBrowser->Document )
        {
        TComInterface HTMLDoc;
        if(SUCCEEDED(CppWebBrowser->Document->QueryInterface(
        IID_IHTMLDocument2,(LPVOID*)&HTMLDoc) ) )
        {
        TComInterface forms;
        if( SUCCEEDED(HTMLDoc->get_forms(&forms)) )
        {
        TComInterface form;
        if( SUCCEEDED(GetHTMLItem(forms, "loginform", &form)) )
        {
        TComInterface userid;
        TComInterface password;

        			GetHTMLItem(form, "userid", &userid);
        			GetHTMLItem(form, "password", &password);
        
        			if( userid )
        			userid->put\_value(WideString("username"));
        
        			if( password )
        			password->put\_value(WideString("password"));
        			}
        		}
        	}
        }
        

        }

        I hope this Boland C++ Builder 6 code might work too..

        bool TForm1::Login(char* inputname, const AnsiString& text)
        {
        bool done = false;

        IHTMLDocument2* HTMLDoc = NULL;

        if(SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&HTMLDoc)))
        {
        IHTMLElementCollection* pAll = NULL;
        if(SUCCEEDED(HTMLDoc->get_all(&pAll)))
        {
        //

        Variant name = inputname;
        Variant index = 0;

        IDispatch* pDisp = NULL;

        if(SUCCEEDED(pAll->item(name, index, &pDisp)))
        {
        if ( pDisp )
        {
        // IHTMLInputFileElement* pInput = NULL; // mshtml.h
        IHTMLInputElement* pInput = NULL; // mshtml.h
        // IHTMLFormElement* pForm = NULL; // mshtml.h

        pDisp->QueryInterface(IID\_IHTMLInputElement, (LPVOID\*)&pInput);
        pDisp->Release();
        
        if(pInput)
        {
         pInput->put\_value(WideString(text));
        
         done = true;
        
          /\*
          WideString mybuffer;
          pInput->get\_value ( &mybuffer );
          ShowMessage ( mybuffer );
          \*/
        
         pInput->Release();
         }
        }
        

        }
        pAll->Release();
        }

        HTMLDoc->Release();
        }
        return done;
        }

        A Offline
        A Offline
        Andrew J Burke
        wrote on last edited by
        #3

        /* Well I got this far I get 3 errors in the BrowserDocumentComplete event: (C++ Error)BrowserFrm.cpp(53): E2285 Could not find match for 'TBrowserFrame::GetHTMLItemTComInterface,char *,IHTMLFormElement * *)' (C++ Error)BrowserFrm.cpp(58): E2285 Could not find match for 'TBrowserFrame::GetHTMLItemTComInterface,char *,IHTMLInputElement* *)' (C++ Error)BrowserFrm.cpp(59): E2285 Could not find match for 'TBrowserFrame::GetHTMLItemTComInterface,char *,IHTMLInputElement* *)' */ //I marked the lines with the errors below: //BrowserFrm.cpp #include #pragma hdrstop #include "BrowserFrm.h" #include #include //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "SHDocVw_OCX" #pragma resource "*.dfm" TBrowserFrame *BrowserFrame; //--------------------------------------------------------------------------- __fastcall TBrowserFrame::TBrowserFrame(TComponent* Owner) : TFrame(Owner) { } //--------------------------------------------------------------------------- void __fastcall TBrowserFrame::Go(AnsiString url) { Browser->Navigate(StringToOleStr(url),0,NULL,NULL,NULL); } //--------------------------------------------------------------------------- template HRESULT __fastcall TBrowserFrame::GetHTMLItem(ParentIntf *CollectionOrElement, const WideString &name, ItemIntf** ppIntf) { TVariant vName = name; TVariant vIndex = 0; TComInterface disp; HRESULT hRes = CollectionOrElement->item(vName, vIndex, &disp); if( SUCCEEDED(hRes) ) hRes = disp->QueryInterface(__uuidof(ItemIntf),(LPVOID*)ppIntf); return hRes; } //--------------------------------------------------------------------------- void __fastcall TBrowserFrame::BrowserDocumentComplete(TObject *Sender, LPDISPATCH pDisp, Variant *URL) { if( Browser->Document ) { TComInterfaceHTMLDoc; if(SUCCEEDED(Browser->Document->QueryInterface( IID_IHTMLDocument2,(LPVOID*)&HTMLDoc) ) ) { TComInterfaceforms; if( SUCCEEDED(HTMLDoc->get_forms(&forms)) ) { TComInterfaceform; /*Line 53 error*/ if( SUCCEEDED(GetHTMLItem(forms, "loginform", &form)) ) { TComInterfaceuserid; TComInterfacepassword;

        A 1 Reply Last reply
        0
        • A Andrew J Burke

          /* Well I got this far I get 3 errors in the BrowserDocumentComplete event: (C++ Error)BrowserFrm.cpp(53): E2285 Could not find match for 'TBrowserFrame::GetHTMLItemTComInterface,char *,IHTMLFormElement * *)' (C++ Error)BrowserFrm.cpp(58): E2285 Could not find match for 'TBrowserFrame::GetHTMLItemTComInterface,char *,IHTMLInputElement* *)' (C++ Error)BrowserFrm.cpp(59): E2285 Could not find match for 'TBrowserFrame::GetHTMLItemTComInterface,char *,IHTMLInputElement* *)' */ //I marked the lines with the errors below: //BrowserFrm.cpp #include #pragma hdrstop #include "BrowserFrm.h" #include #include //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "SHDocVw_OCX" #pragma resource "*.dfm" TBrowserFrame *BrowserFrame; //--------------------------------------------------------------------------- __fastcall TBrowserFrame::TBrowserFrame(TComponent* Owner) : TFrame(Owner) { } //--------------------------------------------------------------------------- void __fastcall TBrowserFrame::Go(AnsiString url) { Browser->Navigate(StringToOleStr(url),0,NULL,NULL,NULL); } //--------------------------------------------------------------------------- template HRESULT __fastcall TBrowserFrame::GetHTMLItem(ParentIntf *CollectionOrElement, const WideString &name, ItemIntf** ppIntf) { TVariant vName = name; TVariant vIndex = 0; TComInterface disp; HRESULT hRes = CollectionOrElement->item(vName, vIndex, &disp); if( SUCCEEDED(hRes) ) hRes = disp->QueryInterface(__uuidof(ItemIntf),(LPVOID*)ppIntf); return hRes; } //--------------------------------------------------------------------------- void __fastcall TBrowserFrame::BrowserDocumentComplete(TObject *Sender, LPDISPATCH pDisp, Variant *URL) { if( Browser->Document ) { TComInterfaceHTMLDoc; if(SUCCEEDED(Browser->Document->QueryInterface( IID_IHTMLDocument2,(LPVOID*)&HTMLDoc) ) ) { TComInterfaceforms; if( SUCCEEDED(HTMLDoc->get_forms(&forms)) ) { TComInterfaceform; /*Line 53 error*/ if( SUCCEEDED(GetHTMLItem(forms, "loginform", &form)) ) { TComInterfaceuserid; TComInterfacepassword;

          A Offline
          A Offline
          App_
          wrote on last edited by
          #4

          Change this line:

          HRESULT __fastcall GetHTMLItem(ParentIntf *CollectionOrElement, const WideString &name, ItemIntf** ppIntf)

          To this:

          HRESULT __fastcall GetHTMLItem(TComInterface<ParentIntf> &CollectionOrElement, const WideString &name, ItemIntf** ppIntf)

          A 1 Reply Last reply
          0
          • A App_

            Change this line:

            HRESULT __fastcall GetHTMLItem(ParentIntf *CollectionOrElement, const WideString &name, ItemIntf** ppIntf)

            To this:

            HRESULT __fastcall GetHTMLItem(TComInterface<ParentIntf> &CollectionOrElement, const WideString &name, ItemIntf** ppIntf)

            A Offline
            A Offline
            Andrew J Burke
            wrote on last edited by
            #5

            That Worked! It Fills the values but when I submit the form the web browser comes up with a dialog with a caption of: _ASSERTE: and Message of: intf = 0 @ C:\Program Files\Borland\CBuilder6\Include\Vcl\utilcls.h/2916 Press[Y]es to terminate, [N]o to continue and [C]ancel to Debug My code: template HRESULT __fastcall TBrowserFrame::GetHTMLItem(TComInterface &CollectionOrElement, const WideString &name, ItemIntf** ppIntf) { TVariant vName = name; TVariant vIndex = 0; TComInterfacedisp; HRESULT hRes = CollectionOrElement->item(vName, vIndex, &disp); if( SUCCEEDED(hRes) ) hRes = disp->QueryInterface(__uuidof(ItemIntf),(LPVOID*)ppIntf); return hRes; } //--------------------------------------------------------------------------- void __fastcall TBrowserFrame::BrowserDocumentComplete(TObject *Sender, LPDISPATCH pDisp, Variant *URL) { if( Browser->Document ) { TComInterfaceHTMLDoc;; if(SUCCEEDED(Browser->Document->QueryInterface( IID_IHTMLDocument2,(LPVOID*)&HTMLDoc) ) ) { TComInterfaceforms; if( SUCCEEDED(HTMLDoc->get_forms(&forms)) ) { TComInterfaceform;; if( SUCCEEDED(GetHTMLItem(forms, "idpform", &form)) ) { TComInterfaceuserid; TComInterfacepassword; GetHTMLItem(form, "txtUsername", &userid); GetHTMLItem(form, "txtPassword", &password); if( userid ) userid->put_value(WideString(username)); if( password ) password->put_value(WideString(pass)); //form->submit(); } } } } } //---------------------------------------------------------------------------

            A 1 Reply Last reply
            0
            • A Andrew J Burke

              That Worked! It Fills the values but when I submit the form the web browser comes up with a dialog with a caption of: _ASSERTE: and Message of: intf = 0 @ C:\Program Files\Borland\CBuilder6\Include\Vcl\utilcls.h/2916 Press[Y]es to terminate, [N]o to continue and [C]ancel to Debug My code: template HRESULT __fastcall TBrowserFrame::GetHTMLItem(TComInterface &CollectionOrElement, const WideString &name, ItemIntf** ppIntf) { TVariant vName = name; TVariant vIndex = 0; TComInterfacedisp; HRESULT hRes = CollectionOrElement->item(vName, vIndex, &disp); if( SUCCEEDED(hRes) ) hRes = disp->QueryInterface(__uuidof(ItemIntf),(LPVOID*)ppIntf); return hRes; } //--------------------------------------------------------------------------- void __fastcall TBrowserFrame::BrowserDocumentComplete(TObject *Sender, LPDISPATCH pDisp, Variant *URL) { if( Browser->Document ) { TComInterfaceHTMLDoc;; if(SUCCEEDED(Browser->Document->QueryInterface( IID_IHTMLDocument2,(LPVOID*)&HTMLDoc) ) ) { TComInterfaceforms; if( SUCCEEDED(HTMLDoc->get_forms(&forms)) ) { TComInterfaceform;; if( SUCCEEDED(GetHTMLItem(forms, "idpform", &form)) ) { TComInterfaceuserid; TComInterfacepassword; GetHTMLItem(form, "txtUsername", &userid); GetHTMLItem(form, "txtPassword", &password); if( userid ) userid->put_value(WideString(username)); if( password ) password->put_value(WideString(pass)); //form->submit(); } } } } } //---------------------------------------------------------------------------

              A Offline
              A Offline
              App_
              wrote on last edited by
              #6

              What you are likely seeing is a call to MessageBox() before the exception is thrown. To turn that off, you can go into the Project Options and add NO_PROMPT_ON_HRCHECK_FAILURE to the Conditionals list. The exception will then be thrown without any prompting. If you are still using the TComInterface wrapper, the '->' operator validates whether the interface pointer is available or not. If it is not, an ASSERT is thrown. To disable the prompt on that error, you can define NO_PROMPT_ON_ASSERTE_FAILURE in the Project Options. If you implement a try..catch block, it will catch errors normally without messagebox prompt interruptions. (sample try catch)

              try {
              buf = new char[512];
              if( buf == 0 ) throw "Memory allocation failure!";
              }
              catch( char * str ) {
              cout << "Exception raised: " << str << '\n';
              }

              A 1 Reply Last reply
              0
              • A App_

                What you are likely seeing is a call to MessageBox() before the exception is thrown. To turn that off, you can go into the Project Options and add NO_PROMPT_ON_HRCHECK_FAILURE to the Conditionals list. The exception will then be thrown without any prompting. If you are still using the TComInterface wrapper, the '->' operator validates whether the interface pointer is available or not. If it is not, an ASSERT is thrown. To disable the prompt on that error, you can define NO_PROMPT_ON_ASSERTE_FAILURE in the Project Options. If you implement a try..catch block, it will catch errors normally without messagebox prompt interruptions. (sample try catch)

                try {
                buf = new char[512];
                if( buf == 0 ) throw "Memory allocation failure!";
                }
                catch( char * str ) {
                cout << "Exception raised: " << str << '\n';
                }

                A Offline
                A Offline
                Andrew J Burke
                wrote on last edited by
                #7

                Thankyou very much...every thing worked I added NO_PROMPT_ON_HRCHECK_FAILURE to the Conditionals list. and inserted a try/catch block around my code in the document complete event. Now if every login page had the same names for their form & input names!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups