求教一个关于通过ATL COM技术及IHTMLDocument2接口等取网页中“Input”文本框中的文本

信阳毛尖 信阳毛尖 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
部分代码如下:
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”呢? 或者,还有其他的途径取出文本框中的文本方法嘛?
...全文

相关推荐

【VC++ Web开发】通过COM接口获取Web页面中Input Text元素值的方法实现了文档的主要内容

内容概要:本文档详细介绍了使用VC++读取Web页面中Input Text的方法。首先定义了必要的数据类型HHTMLWND和HHTMLSEL。接着重点描述了两个关键函数:GetActiveHtmlTextCtrlHandle和GetActiveHtmlText。前者用于获取CHtmlView中具有焦点的Input Text控件句柄,主要步骤包括加载OLEACC.DLL库、发送WM_HTML_GETOBJECT消息、获取IHTMLDocument2接口、遍历元素查找具有焦点的文本编辑元素(如Input或TextArea)。后者则利用获取到的句柄来提取具体的文本内容,通过QueryInterface方法判断是文本框还是文本区域,并根据不同情况调用相应的get_value方法获取值,最后根据编译环境(Unicode或多字节字符集)将BSTR类型转换为合适的字符串格式返回。; 适合人群:对VC++有一定了解,熟悉COM接口、WebBrowser控件以及Windows API编程的开发人员。; 使用场景及目标:①在开发基于MFC的浏览器应用时需要与网页交互,特别是读取用户输入的内容;②学习如何通过COM接口操作HTML DOM树,理解浏览器与本地应用程序之间的通信机制。; 阅读建议:由于涉及到较多的底层API调用和COM编程知识,在阅读时应先熟悉相关基础知识,可以结合MSDN文档加深理解,同时建议边看代码边进行实际测试以更好地掌握其中的技术细节。

goudantaba

WebBrowser组件和MSHTML 在Delphi中的使用.doc

WebBrowser组件和MSHTML 在Delphi中的使用.doc

悠闲饭团

.net操作IE窗口

操作IE窗口 SHDocVw.InternetExplorer ies = new SHDocVw.InternetExplorer(); //DataSet1TableAdapters.userTableAdapter ud = new userTableAdapter(); SHDocVw.WebBrowser wb = new SHDocVw.WebBrowser(); SHDocVw.IShellWindows swc = new SHDocVw.ShellWindowsClass(); mshtml.HTMLInputElementClass input; foreach (var item in swc) { SHDocVw.InternetExplorer objIE = (SHDocVw.InternetExplorer)item; System.Console.Write(objIE.FullName); System.Console.Write(objIE.LocationName); //System.Console.Write(objIE.Document); System.Console.Write(objIE.Name); if (objIE.LocationName == "国航飞行安全管理信息系统") { // System.Console.Write("----"); mshtml.IHTMLDocument2 ieDoc = (mshtml.IHTMLDocument2)objIE.Document; foreach (mshtml.IHTMLElement ieElement in ieDoc.all) { //if its tag is input and name is q(question) if (ieElement.tagName.ToUpper().Equals("INPUT")) { input = ((mshtml.HTMLInputElementClass)ieElement); if (input.name == "username") { //Response.Write(input.value); input.value = ""+name+""; } if (input.name == "password") { // Response.Write(input.value); input.value = "GHGF20096152"; } } } }

duanbaogui

易语言源码获取外部IE浏览器网页源码和密码框的密码例程

易语言源码获取外部IE浏览器网页源码和密码框的密码例程

codedadi

易语言源码易语言网页元素探索高亮显示

易语言源码易语言网页元素探索高亮显示

codemami
评论 6
收藏 已收藏
分享
返回顶部
信阳毛尖
VC/MFC 16,550 成员 421,606 内容