转自:http://hi.baidu.com/wuyan3158/blog/item/255eb57ec5c3c2350cd7dafa.html
# import <mshtml.tlb> // Internet Explorer 5
# import <shdocvw.dll>
#include <atlbase.h>
#include <mshtml.h>
_variant_t v;
HRESULT hr;
VARIANT id, index;
CComPtr <IDispatch> spDispatch;
CComQIPtr <IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
CComQIPtr <IHTMLElement, &IID_IHTMLElement> pElement;
CComQIPtr <IHTMLElementCollection,&IID_IHTMLElementCollection> pElementCol;
CComPtr <IHTMLInputTextElement> pInputTextElement;
if (m_spSHWinds == NULL) {
hr = m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows));
if (FAILED(hr)){
MessageBox( "Failed ");
CoUninitialize();
}
}
if (m_spSHWinds) {
long n=0;
m_spSHWinds-> get_Count(&n); //得到浏览器的个数
//遍历每一个浏览器
for (long i = 0; i < n; i++) {
CComPtr <IDispatch> spDisp;
v = (long)i;
spDisp =m_spSHWinds-> Item(&v);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp); //生成一个IE窗口的智能指针
if (spBrowser) {
//获取IHTMLDocument2接口
if (SUCCEEDED(spBrowser-> get_Document( &spDispatch)))
pDoc2 = spDispatch;
if(pDoc2!=NULL) {
//获取所有元素的集合
if(SUCCEEDED(pDoc2-> get_all(&pElementCol))){
long p=0;
if(SUCCEEDED(pElementCol-> get_length(&p))) {
if(p!=0){
//遍历所有元素
for(long i=0;i <p;i++){
V_VT(&id) = VT_I4;
V_I4(&id) = i;
V_VT(&index) = VT_I4;
V_I4(&index) = 0;
if(SUCCEEDED(pElementCol-> item(id,index, &spDispatch))) { //应该就这附近的几行错误吧
if(SUCCEEDED(spDispatch-> QueryInterface(IID_IHTMLElement,(void**)&pElement))) { //应该就这附近的几行错误吧
if(SUCCEEDED(pElement-> QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputTextElement))) {
CComBSTR strName( "csdn "); //用户名
CComBSTR strPwd( "123456 "); //密码
CComBSTR type; //输入框类型
pInputTextElement-> get_type(&type);//获取输入框类型
CString strtype(type);
strtype.MakeUpper();
if (lstrcmpi(strtype, _T( "TEXT ")) == 0) {
pInputTextElement-> put_value(strName);//设置文本框的值(用户名)
}
if (lstrcmpi(strtype, _T( "PASSWORD ")) == 0) {
pInputTextElement-> put_value(strPwd);//设置文本框的值(密码)
}
}
}
}
}
}
}
}
}
}
}
}
}
查找IE窗口里的submit按钮,模拟点击
the following code will click submit button of html page in IE, modify it to click other button.
///////////////////////////////////////////////////////////////////////////////////////
//click submit button of IE window
//If it works, it is written by masterz,otherwise I don't
//know who writes it^_^
///////////////////////////////////////////////////////////////////////////////////////
void CGetIESrcDlg::NavigateToUrl()
{
// Import the following files in your stdafx.h
// #import <mshtml.tlb> // Internet Explorer 5
// #import <shdocvw.dll>
// Refer to "Connect to Internet Explorer Instances, From your own Process. " in www.codeguru.com
SHDocVw::IShellWindowsPtr m_spSHWinds;
CoInitialize(NULL);
if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK)
{
IDispatchPtr spDisp;
long nCount = m_spSHWinds->GetCount();
for (long i = 0; i < nCount; i++)
{
_variant_t va(i, VT_I4);
spDisp = m_spSHWinds->Item(va);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
if (spBrowser != NULL)
{
IDispatchPtr spDisp;
if(spBrowser->get_Document(&spDisp) == S_OK && spDisp!= 0 )
{
MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
MSHTML::IHTMLElementPtr spHtmlElement;
if(spHtmlDocument==NULL)
continue;
spHtmlDocument->get_body(&spHtmlElement);
if(spHtmlDocument==NULL)
continue;
HRESULT hr;
MSHTML::IHTMLElementCollection* pColl=NULL;
hr=spHtmlDocument->get_all(&pColl);
if(pColl!=NULL&&SUCCEEDED(hr))
{
MSHTML::IHTMLElement* pElem=NULL;
_variant_t index;
index.vt=VT_I4;
index.intVal=0;
_variant_t name("Submit");
IDispatchPtr disp;
disp=pColl->item(name,index);
if(disp==NULL)
hr=E_FAIL;
else
{
hr=disp->QueryInterface(&pElem);
}
if (SUCCEEDED(hr)&& pElem != NULL)
{
//
BSTR bstrhtml;
pElem->get_outerHTML(&bstrhtml);
CString str(bstrhtml);
AfxMessageBox(str);
pElem->click();
pElem->Release();
}
pColl->Release();
}
}
}
}
}
else {
AfxMessageBox("Shell Windows interface is not avilable");
}
CoUninitialize();
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1876826
获取网页中的密码和文本输入框的内容
介绍
网页中的密码输入框和一般不同,他没有句柄之类的,但是可以通过IE的IHTMLInputTextElement接口获取!
本文是教你如何得到网页中输入框的内容。如何你想获取网页中的密码,则只要把if (lstrcmpi(strtype, _T("TEXT")) == 0) 改成if
(lstrcmpi(strtype, _T("PASSWORD")) == 0) 就可以了,如果你要是再加个实时监视的话,就成了密码窃取程序了咯!
主要思路:获取IE的IHTMLInputTextElement接口,这样就可以获取网页中的输入框(包括文本和密码输入框)的内容了。
VARIANT id, index;
CComPtr<IDispatch> spDispatch;
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
CComQIPtr<IHTMLElement, &IID_IHTMLElement> pElement;
CComPtr<IHTMLElementCollection> pElementCol;
CComPtr<IHTMLFormElement> pFormElement;
CComPtr<IHTMLInputTextElement> pInputElement;
//首先获取IWebBrowser2接口
CoInitialize(NULL); //必须要这句初始化
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
if (m_spSHWinds == NULL)
{
if (m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) != S_OK)
{
MessageBox("Failed");
CoUninitialize();
}
}
if (m_spSHWinds)
{
int n = m_spSHWinds->GetCount();
for (int i = 0; i < n; i++)
{
_variant_t v = (long)i;
IDispatchPtr spDisp = m_spSHWinds->Item(v);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp); //生成一个IE窗口的智能指针
if (spBrowser)
{
//获取IHTMLDocument2接口
if (SUCCEEDED(spBrowser->get_Document( &spDispatch)))
pDoc2 = spDispatch;
if(pDoc2!=NULL)
{
// AfxMessageBox("已经获取IHTMLDocument2");
if (SUCCEEDED(pDoc2->get_forms(&pElementCol)))
{
// AfxMessageBox("已经获取IHTMLElementCollection");
long p=0;
if(SUCCEEDED(pElementCol->get_length(&p)));
if(p!=0)
{
for(long i=0;i<=(p-1);i++)
{
V_VT(&id) = VT_I4;
V_I4(&id) = i;
V_VT(&index) = VT_I4;
V_I4(&index) = 0;
if(SUCCEEDED(pElementCol->item(id,index, &spDispatch)))
if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLFormElement,(void**)&pFormElement)))
{
// AfxMessageBox("已经获取IHTMLFormElement");
long q=0;
if(SUCCEEDED(pFormElement->get_length(&q)));
for(long j=0;j<=(q-1);j++)
{
V_VT(&id) = VT_I4;
V_I4(&id) = j;
V_VT(&index) = VT_I4;
V_I4(&index) = 0;
if(SUCCEEDED(pFormElement->item(id,index, &spDispatch)))
if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputElement)))
{
//AfxMessageBox("已经获取IHTMLInputTextElement");
CComBSTR value;
CComBSTR type;
pInputElement->get_type(&type);//获取输入框类型(密码框还是文本框)
CString strtype(type);
strtype.MakeUpper();
if (lstrcmpi(strtype, _T("TEXT")) == 0)
{
pInputElement->get_value(&value);//获取文本框的值
CString strvalue(value);
AfxMessageBox(strvalue);
}
}
}
}
}
}