Hi, i've been looking around for help with this problem but google seems to just send a load of crap my way so its finally got to the point where i'm asking specifically for help. Here is the code;

void CWebRobotDlg::OnBnClickedDologin()
{
    void* pElem= ElemFromID(L"username", IID_IHTMLInputElement) ;
    IHTMLInputElement* pTextBoxUser= (IHTMLInputElement*)pElem;

    bstr_t sUser(L"UsErNaMe");  // <<<-- your login name here

    pTextBoxUser->put_value(sUser); // populate the text box!
    //--------------------------------------------------------
    pElem= ElemFromID(L"password", IID_IHTMLInputElement) ;
    IHTMLInputElement* pTextBoxPswd= (IHTMLInputElement*)pElem;

    bstr_t sPswd(L"PaSsWoRd");   // <<<-- your login password here

    HRESULT hr= pTextBoxPswd->put_value(sPswd);  // populate the text box!
    //--------------------------------------------From here is where the problem starts.
    pElem= ElemFromID(L"checkbox", IID_IHTMLInputElement) ;
    IHTMLInputElement* pCheckBoxChkbx= (IHTMLInputElement*)pElem;

    bstr_t sChkbx(L"VALUE");  // <<<-- ??

    pCheckBoxChkbx->put_value(sChkbx); // Not working

    //-------------------------------------------End Of problem area.
    pElem= ElemFromID(L"submit", IID_IHTMLElement);
    IHTMLElement* pSubmit= (IHTMLElement*)pElem;

    hr= pSubmit->click(); // GO! 
}

As you can see this code is filling in a form on a website (completely anonymised atm). It works perfectly but there is an optional checkbox that I would like to tick before the form is submitted. I have harvested the correct id's/names from the website source and can put a value into text box fields, but im using borrowed code and I cant work out how to interact with the input type of check boxes.

IHTMLInputElement* pTextBoxPswd= (IHTMLInputElement*)pElem;

I Assumed the "TextBox" part of this line was defined in mshtml.h, I also assumed it was specifically designed to interact with text box type web elements and not check boxes, so I tried "Checkbox". This was my best guess. I'm also unser what value to pass to the box once i've got the element name correct.

Thanks in advance, Alochai.

Recommended Answers

All 3 Replies

So, I found the answer, after a dissatisfying amount of time. C++ programs using IE activeX objects to interface with the internet does not seem well documented at all.

Answer below \/

pElem= ElemFromID(L"checkbox", IID_IHTMLInputElement) ;
IHTMLInputElement* pCheckBoxChkbx= (IHTMLInputElement*)pElem;

bstr_t sChkbx(L"true");//Yea, attempting to pass a string to a checkbox was dumb.

pCheckBoxChkbx->put_checked(true); // Golden, this is how you do it.

I assume put_checked works for radio buttons too, if not I hope it's something semantic like put_toggled(true).

You're welcome Alochai ;)

p.s. Yes I was talking to myself.

You forgot to mark the thread as solved, too busy talking to yourself ;)

Yea, but I actually wanted someone to post in the thread that wasnt me.

Mission accomplished btw.

Thread complete!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.