Click here to Skip to main content
16,012,316 members
Articles / Desktop Programming / MFC
Article

Connect the Gmail with IWebBrowser2

Rate me:
Please Sign up or sign in to vote.
1.38/5 (11 votes)
16 Jun 20041 min read 76.4K   26   12
Use the IWebBrowser2 to connect Gmail

Introduction

When I got a Gmail account, I must do many complex steps to login the Gmail system.  I think if there is an interface like POP#, people will feel more free to use the GMail system.

I do not know of a GMail Client API, so I use the IE COM interface to manage IE to do what we do manually.

There are many articles about IE programming, but I cannot find one code to do a page that includes frames or IFrame.  In this article, I resolved this problem.

First, We can use get_frames to get the frame collection. If the page does not have the <body> tag, the frame collection will include all the framesets. Otherwise, the frame collection includes all the iframes. In the Gmail system login page,there is a iframe tag, and we can find it and get the form.

BOOL CGMailClass::lunchBrowser()
{
    HRESULT hr1 ;
    hr1 = CoInitialize(NULL);
    if(!SUCCEEDED(hr1))
        return FALSE;

    //Create an Instance of web browser
    hr1 = CoCreateInstance (CLSID_InternetExplorer, NULL, 
        CLSCTX_LOCAL_SERVER, 
        IID_IWebBrowser2, (LPVOID *)&m_pBrowser); 
    if(hr1==S_OK)
    {
        VARIANT_BOOL pBool=true; //The browser is invisible
        m_pBrowser->put_Visible( pBool ) ; 
        //Gmail login page
        COleVariant vaURL("https://gmail.google.com/gmail") ; 
        COleVariant null; 

        //Open the mail login page
        m_pBrowser->Navigate2(vaURL,null,null,null,null) ; 
        WaitForEnd();
    }
    return TRUE;
}
m_pBrowser is the member variable of this class, below is the declaration:
protected:
    IWebBrowser2* m_pBrowser;
The function below is to get the document from the IFrame, so that we can find the form element.
BOOL CGMailClass::getDoc(IHTMLDocument2** pIFrameDoc)
{
    IDispatch *spDispatch;
    CComQIPtr<IHTMLDOCUMENT2, &IID_IHTMLDocument2> pDoc2;
    if (m_pBrowser){
        spDispatch=NULL;
        if (SUCCEEDED(m_pBrowser->get_Document( &spDispatch)))
            pDoc2 = spDispatch;
        if(pDoc2!=NULL)
        {
            //because the Gmail login page hide in a Iframe
            //so we must find where is the IFrame object
            CComPtr<IHTMLFRAMESCOLLECTION2> pIFrameCol;
            if (SUCCEEDED(pDoc2->get_frames(&pIFrameCol))) 
            {
                //retreive all of the Frames
                long p=0;
                pIFrameCol->get_length(&p);
                if(p!=0)
                { //yes we find more than one Frames
                    for(long i=0;i<=(p-1);i++)
                    { 
                        VARIANT frameRequested;
                        VARIANT frameOut;
                        frameRequested.vt = VT_I4 ;
                        frameRequested.bstrVal = (BSTR)i;
                        pIFrameCol->item(&frameRequested, &frameOut);
                        IHTMLWindow2* pIFrameWindow;
                        frameOut.pdispVal->QueryInterface(IID_IHTMLWindow2, 
                                                      (void**)&pIFrameWindow);

                    pIFrameWindow->get_document(pIFrameDoc);
                        return TRUE;
                        //ParseDoc(pIFrameDoc);
                    }
                }
            }
        }
    }
    return FALSE;
}
The getForm function can get the Form element and you can use the form element to set the input value,visible and other properties.
BOOL CGMailClass::getForm(IHTMLDocument2 *pIFrameDoc,
                          IHTMLFormElement **pFormElement)
{
    IHTMLElementCollection* pElementCol=NULL;
    long p=0;
    if (SUCCEEDED(pIFrameDoc->get_forms(&pElementCol)))
    {

    if(SUCCEEDED(pElementCol->get_length(&p)))
            if(p!=0)
            { 
                for(long i=0;i<=(p-1);i++)
                {
                    VARIANT id, index;
                    IDispatch* spDispatch=NULL;
                    V_VT(&id) = VT_I4;
                    V_I4(&id) = i;
                    V_VT(&index) = VT_I4;
                    V_I4(&index) = 0;
                    spDispatch=NULL;
                    if(SUCCEEDED(pElementCol->item(id,index, &spDispatch)))
                    {
                        if(SUCCEEDED(spDispatch->QueryInterface(
                                 IID_IHTMLFormElement,(void**)pFormElement)))
                            return TRUE;
                    }
                }
            }
    }
    return FALSE;
}

In this demo, I just implented the login function, because the Gmail system uses too many and too complicated javascript. I cannot find a way to get the inbox property. If you had a good method,please let me know. You can reach me at mailto:ChineseDofu@gmail.com

I must say thanks to pratheesh because of his Yahoo Mail.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionsource code? Pin
yaron shani11-Oct-07 4:06
yaron shani11-Oct-07 4:06 
GeneralSampleCode Required Pin
TusharKavali12-Oct-06 7:05
TusharKavali12-Oct-06 7:05 
GeneralSource Code Pin
Adeel6887-Aug-06 0:02
Adeel6887-Aug-06 0:02 
Generalerror happens Pin
pearlite10-Aug-05 22:47
pearlite10-Aug-05 22:47 
Generalunable to connect to server Pin
Anonymous26-Mar-05 15:23
Anonymous26-Mar-05 15:23 
GeneralSample code please... Pin
Member 987175326-Nov-04 18:00
Member 987175326-Nov-04 18:00 
Generalthanks Pin
Member 13634115-Oct-04 12:44
Member 13634115-Oct-04 12:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.