枚举所有IE窗口,实现自动登录
//定义IE程序处理函数指针
typedef void (*ProcessWebBrowser)( const IWebBrowser2Ptr pBrowser );
/*
* 枚举当前打开的IE窗口,并输出网页的标题
*/
BOOL EnumInternetExplorer( ProcessWebBrowser pHander )
{
Sleep(500);
//IE属于shell的一个应用程序,再次需要枚举ShellWindows集合
IShellWindowsPtr spSHWinds;
if( FAILED( spSHWinds.CreateInstance( __uuidof( ShellWindows ) ) ) )
return FALSE;
//获取ShellWindows集合的数目
long nCount=0;
spSHWinds->get_Count(&nCount);
for ( long lIndex = 0; lIndex < nCount; lIndex++ )
{
//枚举每个项
IDispatchPtr spDisp;
_variant_t var( lIndex, VT_I4 );
spSHWinds->Item( var, &spDisp );
if ( spDisp != NULL )
{
//Query网页接口
IWebBrowser2Ptr spBrowser( spDisp );
if( ( spBrowser != NULL ) && ( pHander != NULL ) )
{
//调用处理函数
pHander( spBrowser );
}
}
}
return TRUE;
}
// 这里需要事先设置好 LPCTSTR lpszUserNameID, LPCTSTR lpszUser, LPCTSTR lpszPwdID, LPCTSTR lpszPwd, LPCTSTR lpszSubmitID
LPCTSTR lpszUserNameID="loginname";
LPCTSTR lpszUser="87426628123456@sina.com";
LPCTSTR lpszPwdID="password";
LPCTSTR lpszPwd="123456";
LPCTSTR lpszSubmitID="login_submit_btn";
void AutoLogin(const IWebBrowser2Ptr pBrowser)
{
MSHTML::IHTMLDocument3Ptr ptrDoc3;
IDispatch* pDocDisp;
HRESULT hResult = pBrowser->get_Document((IDispatch **)&pDocDisp);
if (FAILED(hResult))
{
_TRACE("获取IE里面的文档失败\n");
return;
}
hResult = pDocDisp->QueryInterface(__uuidof(IHTMLDocument3), (void**)&ptrDoc3);
if (FAILED(hResult))
{
_TRACE("获取IHTMLDocument3 失败\n");
return;
}
MSHTML::IHTMLDocument2Ptr ptrDoc2 = ptrDoc3;
MSHTML::IHTMLInputElementPtr ptrUserName = NULL;
MSHTML::IHTMLInputElementPtr ptrPwd = NULL;
MSHTML::IHTMLElementPtr pstrSubmit = NULL;
try
{
// 为什么这里老崩溃呢
//#pragma implementation_key(9490)
// inline VARIANT_BOOL MSHTML::DispHTMLSelectElement::GetisTextEdit ( ) {
// VARIANT_BOOL _result = 0;o
// _com_dispatch_method(this, 0x8001040a, DISPATCH_PROPERTYGET, VT_BOOL, (void*)&_result, NULL);
// return _result;
// }
MSHTML::IHTMLElementPtr elePtr;
elePtr = ptrDoc3->getElementById(lpszUserNameID);
if (!elePtr.GetInterfacePtr())
{
return;
}
ptrUserName = (MSHTML::IHTMLInputElementPtr)(elePtr);
}
catch(...)
{
}
if (ptrUserName==NULL)
{
return;
}
ptrUserName->value = lpszUser;
ptrUserName->put_value(ptrUserName->value);
ptrPwd = ptrDoc3->getElementById(lpszPwdID);
if (ptrPwd==NULL)
{
return;
}
ptrPwd->value = lpszPwd;
ptrPwd->put_value(ptrPwd->value);
CComBSTR bstrMember("toggleLogin");
DISPID dispid = NULL;
IDispatchPtr ptrDisp = ptrDoc2->Script;
HRESULT hr=ptrDisp->GetIDsOfNames(IID_NULL, &bstrMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
if (SUCCEEDED(hr))
{
DISPPARAMS paraDisp = {0,0,0,0};
ptrDisp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, ¶Disp, NULL, NULL, NULL);
}
pstrSubmit = ptrDoc3->getElementById(lpszSubmitID);
if (pstrSubmit==NULL)
{
return;
}
pstrSubmit->click();
}