Navigating through the HTML pages in VC++ using IHTMLElements and IHTMLDocuments

int intCntr=0;
do
{
    handleToWindow = FindWindow("WindowsForms", "Name of your window");
    Sleep(2000);
    intCntr++;
} while (handleToWindow == NULL && intCntr > 200);
// you need to add error checking on your own
HINSTANCE hInstance = ::LoadLibrary( _T("OLEACC.DLL") );
if(hInstance)
    return error;
if(handleToWindow != NULL && hInstance != NULL)
{
    // find the handle to the internet explorer window
    FindWindow(handleToWindow, L"Internet Explorer_Server", captin, handleToInterentExplorerWindow)!= ORC_OK)
    // iterate the gui windows of internet explorer server
    iterator iter;
    for( iter = handleToInterentExplorerWindow.begin(); iter != handleToInterentExplorerWindow.end(); ++iter )
    {
        handleToWindow handleToWindowIEHandle = (*iter).handleToWindow;
        if ( handleToWindowIEHandle )
        {
            CComPtr interfaceHTMLDocument2;
            LRESULT lRes;
            UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
            ::SendMessageTimeout( handleToWindowIEHandle, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
            LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInstance, "ObjectFromLresult" );
            if ( pfObjectFromLresult != NULL )
            {
                // retrieve the html document
                HRESULT hr;
                hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument2, 0, (void**)&interfaceHTMLDocument2 );
                if ( SUCCEEDED(hr) )
                {
                    // get all the frames
                    IHTMLFramesCollection2 *p;
                    interfaceHTMLDocument2->get_frames(&p);
                    long lLen;
                    p->get_length(&lLen);
                    for (long i = 0; i item(&name, &frameOut);
                    if(frameOut.lVal)
                    {
                        // you cannot get the contents inside frames directly
                        // you need to convert them into webbrowser and then
                        // back again, else you will get a access denied error
                        // if you do so
                        IHTMLWindow2 *interfaceHTMLWindow2;
                        hr = frameOut.pdispVal->QueryInterface(IID_IHTMLWindow2, (void**)&interfaceHTMLWindow2);
                        if(SUCCEEDED(hr))
                        {
                            CComQIPtr spServiceProvider = interfaceHTMLWindow2;
                            if (spServiceProvider == NULL)
                            {
                                return error;
                            }
                            CComQIPtr spWebBrws;
                            HRESULT hRes = spServiceProvider->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, (void**)&spWebBrws);
                            if (hRes != S_OK)
                            {
                                return error;
                            }
                            CComQIPtr spDisp;
                            hRes = spWebBrws->get_Document(&spDisp);
                            if (hRes != S_OK)
                            {
                                return error;
                            }
                            CComQIPtr spDocument = spDisp;
                            // iterate through the frames
                            // click the links - encryption
                            IHTMLElementCollection *spCollectEmbed;
                            spDocument->get_links(&spCollectEmbed);
                            if(spCollectEmbed)
                            {
                                // get all the links
                                long lLen;
                                spCollectEmbed->get_length(&lLen);
                                for (long i = 0; i item(index, index, &ppvdispOption);
                                if(SUCCEEDED(hResult))
                                {
                                    // query for the element
                                    hResult = ppvdispOption->QueryInterface( IID_IHTMLElement,
                                    (void **) &interfaceHTMLElement);
                                    if(SUCCEEDED(hResult))
                                    {
                                        BSTR innerhtml;
                                        interfaceHTMLElement->get_innerHTML(&innerhtml);
                                        // click the links
                                        interfaceHTMLElement->click();
                                        Sleep(2000);
                                    }
                                    interfaceHTMLElement->Release();
                                }
                            }
                        }
                    }
            // extract elements from the frame
            IHTMLElement *element;
            // get the whole body and then read them
            spDocument->get_body(&element);
            BSTR innerHtml, innerText;
            if(element != NULL)
            {
                // read the inner html and text
                element->get_innerHTML(&innerHtml);
                element->get_innerText(&innerText);

Leave a Reply

Your email address will not be published. Required fields are marked *