2017-08-08 74 views
0

我試圖填補從C++程序的Internet Explorer某種形式的輸入字段,但我面對的隨機錯誤,我希望是因爲我的代碼:的getElementsByTagName與IHTMLDocument3隨機返回任何

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); 

... 

在最後一行,並且在同一頁面上具有相同的HWND,我有時會得到好的數字或輸入字段,並且對於nelm通常會得到0。

你在我的代碼中看到錯誤,或者它是一個錯誤? 請注意,我驗證了HWND是正確的,並且return從不被調用。

感謝

+0

什麼的IHTMLDocument2 :: get_readyState的結果,當這種情況發生? –

+0

get_readyState返回「完整」,我可以沒有問題地獲得IHTMLDocument2 :: get_body。有時候,如果我得到nelm = 0並返回調試器將輸入更改爲INPUT並再次運行該行,它可以工作,但它也是隨機的。 – Entretoize

+0

與該代碼我可以得到它的工作(對於A標籤):'BSTR標記= L「A」; \t long nelm; \t int n = 0; \t do \t {\t \t n ++; \t \t if(tag == L「a」)\t \t \t tag = L「A」; \t \t else \t \t \t tag = L「a」; \t \t if(pElemCollections)\t \t \t pElemCollections-> Release(); if(FAILED(doc> getElementsByTagName(tag,&pElemCollections)))return; \t \t \t \t pElemCollections-> get_length(&nelm); \t}而(nelm == 0);' – Entretoize

回答

0

我必須這樣做沒有更多的問題:

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); 

...