求教一个关于通过ATL COM技术及IHTMLDocument2接口等取网页中“Input”文本框中的文本
信阳毛尖 2010-08-24 02:47:52 部分代码如下:
BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{
TCHAR buf[100];
::GetClassName( hwnd, (LPTSTR)&buf, 100 );
if ( _tcscmp( buf, _T("Internet Explorer_Server") ) == 0 )
{
*(HWND*)lParam = hwnd;
return FALSE;
}
else
return TRUE;
}
void CAboutIHTMLDocument2Dlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
HWND qqMailWin = NULL;
USES_CONVERSION;
qqMailWin=::FindWindowEx(NULL,NULL,"IEFrame","QQ邮箱 - 写信 - Microsoft Internet Explorer");
//qqMailWin=::FindWindowEx( NULL, NULL, "IEFrame", "2010年河南省高校门户网站评选 - Microsoft Internet Explorer"); //
if(qqMailWin==NULL)
{
DWORD err=::GetLastError();
::AfxMessageBox("找不到写信窗口");
}
CoInitialize( NULL );
HINSTANCE hInst = ::LoadLibrary(_T("OLEACC.DLL"));
if ( hInst != NULL )
{
if( qqMailWin != NULL )
{
HWND hWndChild = NULL;
::EnumChildWindows(qqMailWin,EnumChildProc, (LPARAM)&hWndChild );
if ( hWndChild )
{
CComPtr<IHTMLDocument2> spDoc;
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, (LPCSTR)"ObjectFromLresult" );
if( pfObjectFromLresult !=NULL )
{
HRESULT hr;
hr = pfObjectFromLresult( lRes,IID_IHTMLDocument2,0,(void**)&spDoc );
if ( SUCCEEDED(hr) )
{
// 获取各个控件
CComQIPtr < IHTMLElementCollection > spElementCollection;
hr = spDoc->get_forms(&spElementCollection);
long nFormCount=0; //取得表单数目
hr = spElementCollection->get_length( &nFormCount );
if (nFormCount==0)
::AfxMessageBox("表单数目为0");
for( long i=0;i<nFormCount;i++ )
{
IDispatch *pDisp = NULL; //取得第 i 项表单
hr = spElementCollection->item( CComVariant( i ), CComVariant(), &pDisp );
if ( FAILED( hr ) ) continue;
CComQIPtr < IHTMLFormElement > spFormElement = pDisp;
pDisp->Release();
long nElemCount=0; //取得表单中 域 的数目
hr = spFormElement->get_length( &nElemCount );
if ( FAILED( hr ) ) continue;
for(long j=0; j <nElemCount; j++)
{
CComDispatchDriver spInputElement; //取得第 j 项表单域
hr = spFormElement->item( CComVariant( j ), CComVariant(), &spInputElement );
if ( FAILED( hr ) ) continue;
CComVariant vName,vVal,vType; //取得表单域的 名,值,类型
hr = spInputElement.GetPropertyByName( L"name", &vName );
if( FAILED( hr ) ) continue;
hr = spInputElement.GetPropertyByName( L"value", &vVal );
if( FAILED( hr ) ) continue;
hr = spInputElement.GetPropertyByName( L"type", &vType );
if( FAILED( hr ) ) continue;
LPCTSTR strTextType= _T("hidden");
LPCTSTR strElementType = vType.bstrVal ? OLE2CT( vType.bstrVal ) : _T("NULL");
LPCTSTR strElementName = vName.bstrVal ? OLE2CT( vName.bstrVal ) : _T("NULL");
LPCTSTR strValue = vVal.bstrVal ? OLE2CT(vVal.bstrVal) : _T("NULL");
if( StrCmp( strTextType,strElementType) == 0)
{
if( StrCmp( strElementName, _T("fun")))
{
::AfxMessageBox("找到输入框!!");
CComBSTR str = NULL;
CComQIPtr < IHTMLInputTextElement > spGetText(spInputElement); hr = spGetText->get_value(&str);
if( SUCCEEDED(hr) )
{
if( str.Length() != 0 )
{
CComBSTR msg=_T("输入框类容是:");
msg += str;
CString s( str );
::AfxMessageBox( s );
}
}
}
else
::AfxMessageBox("没有找到输入框!!");
}
}//for j
}//for i
}
}
}
}
::BringWindowToTop(qqMailWin);
::FreeLibrary( hInst );
}
CoUninitialize();
OnOK();
}
编译无误,单步执行的时候执行到 上文中红色字体的地方:hr = spGetText->get_value(&str);出错,就是弹出那个“Microsoft Visual C++ Debug Library”(有“终止,重试,忽略”)的框框,说是Debug Assertion Failed!我觉得是上文中蓝色字体地方CComQIPtr < IHTMLInputTextElement > spGetText(spInputElement);不准确吧,不知道这种赋值方式对不对!如果不对,那么该如何把CComDispatchDriver类型的“spInputElement”转换成(还是赋值?) IHTMLInputTextElement接口类型的“spGetText”呢? 或者,还有其他的途径取出文本框中的文本方法嘛?