Search code examples
c++ihtmldocument2ihtmldocument

getElementsByTagName with IHTMLDocument3 randomly returns nothing


I'm trying to fill some form input fields in internet explorer from a c++ program but I'm facing a random bug that I hope is because of my code:

UINT msg = RegisterWindowMessage("WM_HTML_GETOBJECT");
LRESULT result = 0;
SendMessageTimeout(hwnd, msg, NULL, NULL, SMTO_ABORTIFHUNG, 10000, (PDWORD_PTR)&result);
if (!result)
    return;


// get main document object
IHTMLDocument3 *doc = NULL;
ObjectFromLresult(result, IID_IHTMLDocument3, NULL, (void**)&doc);
if (!doc)
    return;

VARIANT varint, varstr;
varint.vt = VT_I4;
varstr.vt = VT_BSTR;


IHTMLElementCollection* pElemCollections=NULL;


if (FAILED(doc->getElementsByTagName(L"input", &pElemCollections)))
    return;

long nelm;
pElemCollections->get_length(&nelm);

...

At this last line and with the same HWND at the same page I sometimes get the good number or input fields and often get 0 for nelm.

Do you see something wrong in my code or is it a bug ? Note that I verified that the HWND is correct, and the return are never called.

Thanks


Solution

  • I have no more problem by doing that:

    UINT msg = RegisterWindowMessage("WM_HTML_GETOBJECT");
    LRESULT result = 0;
    SendMessageTimeout(hwnd, msg, NULL, NULL, SMTO_ABORTIFHUNG, 10000, (PDWORD_PTR)&result);
    if (!result)
        return;
    
    
    // get main document object
    IHTMLDocument3 *doc = NULL;
    ObjectFromLresult(result, IID_IHTMLDocument3, NULL, (void**)&doc);
    if (!doc)
        return;
    
    CComVariant varint;
    CComVariant varstr;
    
    
    IHTMLElementCollection* pElemCollections=NULL;
    
    CComBSTR name(L"input")
    if (FAILED(doc->getElementsByTagName(name, &pElemCollections)))
        return;
    
    long nelm;
    pElemCollections->get_length(&nelm);
    
    ...