找了个代码,不过是C的,把里面两个关键的函数抽出来,谁有兴趣翻译以下,
//***********************************************
/*函数名:GetDocInterface
参数:hWnd,WebBrowser控件的窗口句柄
功能:通过WM_HTML_GETOBJECT取得控件的IHTMLDocument2接口
*/
IHTMLDocument2* GetDocInterface(HWND hWnd)
{
// 我们需要显示地装载OLEACC.DLL,这样我们才知道有没有安装MSAA
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL"
);
IHTMLDocument2* pDoc2=NULL;
if ( hInst != NULL ){
if ( hWnd != NULL ){
CComPtr<IHTMLDocument> spDoc=NULL;
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT"
);
::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult"
);
if ( pfObjectFromLresult != NULL ){
HRESULT hr;
hr=pfObjectFromLresult(lRes,IID_IHTMLDocument,0,(void**)&spDoc);
if ( SUCCEEDED(hr) ){
CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;
spWin->get_document( &pDoc2 );
}
}
}
::FreeLibrary(hInst);
}
else{//如果没有安装MSAA
AfxMessageBox(_T("请您安装Microsoft Active Accessibility"
);
}
return pDoc2;
}
/*函数名:GetDocInterfaceByMSAA
参数:hwnd,WebBrowser控件的窗口句柄
功能:取得hwnd对应的Webbrowser控件的IHTMLDocument2*接口.
*/
IHTMLDocument2* GetDocInterfaceByMSAA(HWND hwnd)
{
HRESULT hr;
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL"
);
IHTMLDocument2* pDoc2=NULL;
if ( hInst != NULL ){
if ( hwnd != NULL ){
//取得AccessibleObjectFromWindow函数
LPFNACCESSIBLEOBJECTFROMWINDOW pfAccessibleObjectFromWindow =
(LPFNACCESSIBLEOBJECTFROMWINDOW)::GetProcAddress(hInst,_T("AccessibleObjectFromWindow"
);
if(pfAccessibleObjectFromWindow != NULL){
CComPtr<IAccessible> spAccess;
hr=pfAccessibleObjectFromWindow(hwnd,0,
IID_IAccessible,(void**) &spAccess);//取得Webbrowser控件的IAccessible接口
if ( SUCCEEDED(hr) ){
CComPtr<IServiceProvider> spServiceProv;
hr=spAccess->QueryInterface(IID_IServiceProvider,(void**)&spServiceProv);
if(hr==S_OK){
CComPtr<IHTMLWindow2> spWin;
hr=spServiceProv->QueryService(IID_IHTMLWindow2,IID_IHTMLWindow2,
(void**)&spWin);
/*
注意:并不是每次都能取得IHTMLWindow2接口,如果调用失败,可以尝试取得IHTMLElement接口:
CComPtr<IHTMLElement> spElement;
hr=spServiceProv->QueryService(IID_IHTMLElement,IID_IHTMLElement,(void**)&spElement);
*/
if(hr==S_OK)
spWin->get_document(&pDoc2);
}
}
}
}
::FreeLibrary(hInst);
}
else{
AfxMessageBox(_T("请您安装Microsoft Active Accessibility"
);
}
return pDoc2;
}
CString GetPassword(IHTMLDocument2 *pDoc2,POINT pt)
{
if(pDoc2==NULL)return "";
CComPtr<IHTMLElement> pElement;
HRESULT hr=pDoc2->elementFromPoint(pt.x,pt.y,&pElement);
if(SUCCEEDED(hr)){
CComPtr<IHTMLInputTextElement> pPwdElement;
hr=pElement->QueryInterface(IID_IHTMLInputTextElement,
(void**)&pPwdElement);
if(SUCCEEDED(hr)){
CComBSTR type;
hr=pPwdElement->get_type(&type);
if(SUCCEEDED(hr)){
if(type==_T("password"
){
CComBSTR pwd;
hr=pPwdElement->get_value(&pwd);
if(SUCCEEDED(hr)){
if(pwd.Length()!=0){
CComBSTR msg;
msg=pwd;
CString str(msg);
return str;
}
else{
return "";
}
}
}
}
}
}
pDoc2->Release();
return "";
}
另外我把地址完整的说明贴上来http://www.ccw.com.cn/htm/app/aprog/01_12_20_2.asp
网页中的星星是一颗特别的星,铁石心肠,然而它却是豆腐作的。你很难获得它的窗口句柄,通过Visual Studio提供的SPY++也很难获得它的窗口句柄和它收到的窗口消息。获得网页中的密码必须通过必须通过HTML中的MSHTML文档对象模型。你首先必须通过各种方法获得Internet Explorer浏览器(Mr Navigate,Sorry)窗口句柄,判断它的类名是否是"Internet Explorer_Server",然后通过下述代码获得其文档对象模型指针,即IHTMLDocument2接口。
HWND hw;
MSHTML::IHTMLDocument2Ptr doc;
MSHTML::IHTMLElementCollectionPtr col;
MSHTML::IHTMLElementPtr EL;
DWORD lRes;
HRESULT hres;
UINT MSG =
RegisterWindowMessage("WM_HTML_GETOBJECT"
;
SendMessageTimeout(hw, MSG, 0, 0,
SMTO_ABORTIFHUNG, 1000, &lRes);
hres=ObjectFromLresult(lRes,
__uuidof(MSHTML::IHTMLDocument2),
0,
(void**)&doc);
然后你必须获得鼠标屏幕坐标,把它转换成相对于客户区的坐标,然后根据坐标位置获得网页对对应的元素,查询IID_IHTMLInputTextElement接口指针,判断元素类型是否是一个密码编辑框,并获得对应密码。