1.代码
//--------------获取效能提升门户主叫---------------------------------------//
#include <atlbase.h>
#include <mshtml.h>
#include <winuser.h>
#include <comdef.h>
#include <string.h>
void EnumIE(void);//处理网页
void EnummiddleFrame(IHTMLDocument2 * pIHTMLDocument2);//处理框架
void EnumleftFrame(IHTMLDocument2 * pIHTMLDocument2);//处理框架
CComModule _Module; //使用CComDispatchDriver ATL的智能指针,此处必须声明
#include <atlcom.h>
void EnumAllElement(IHTMLDocument2 * pIHTMLDocument2);//获取网页内元素
CString glb_strCaller;//全局主叫
void EnumIE(void)
{
CComPtr<IShellWindows> spShellWin;
HRESULT hr=spShellWin.CoCreateInstance(CLSID_ShellWindows);
if (FAILED(hr))
{
return;
}
long nCount=0; //取得浏览器实例个数(Explorer和IExplorer)
spShellWin->get_Count(&nCount);
if (0==nCount)
{
return;
}
for(int i=0; i<nCount; i++)
{
CComPtr<IDispatch> spDispIE;
hr=spShellWin->Item(CComVariant((long)i), &spDispIE);
if (FAILED(hr)) continue;
CComQIPtr<IWebBrowser2>spBrowser=spDispIE;
if (!spBrowser) continue;
CComPtr<IDispatch> spDispDoc;
hr=spBrowser->get_Document(&spDispDoc);
if (FAILED(hr)) continue;
CComQIPtr<IHTMLDocument2>spDocument2 =spDispDoc;
if (!spDocument2) continue;
//Modify by Fang jiansheng 2011-04-02
//*******************************************************************************
CString cIEUrl_Filter; //设置的URL(必须是此URL的网站才有效);
cIEUrl_Filter=""; //效能提升门户过滤的网址
//*******************************************************************************
CComBSTR IEUrl;
spBrowser->get_LocationURL(&IEUrl);
CString cIEUrl_Get; //从机器上取得的HTTP的完整的URL;
cIEUrl_Get=IEUrl;
cIEUrl_Get=cIEUrl_Get.Left(cIEUrl_Filter.GetLength()); //截取前面N位
if (strcmp(cIEUrl_Get,cIEUrl_Filter)==0)
{
// 程序运行到此,已经找到了IHTMLDocument2的接口指针
EnummiddleFrame(spDocument2);
}
}
}
//在框架内获取主叫
void EnumAllElement(IHTMLDocument2 * pIHTMLDocument2) //枚举所有字段
{
if (!pIHTMLDocument2) return;
HRESULT hr;
CComQIPtr<IHTMLElementCollection> spAllElement;
hr=pIHTMLDocument2->get_all(&spAllElement);//获取所有网页内所有元素
if (FAILED(hr)) return;
long nLength = 0;
spAllElement->get_length (&nLength);
for (int i = 0; i < nLength; i++)
{
CComPtr<IDispatch> pDisp;
hr = spAllElement->item(COleVariant((long)i),COleVariant((long)0),&pDisp); //获取单个元素
if(SUCCEEDED(hr))
{
//CComQIPtr <IHTMLElement, &IID_IHTMLElement> pElement(pDisp);
CComQIPtr<IHTMLElement, &IID_IHTMLElement> pElement;
pDisp->QueryInterface(&pElement);
BSTR bId;
pElement->get_id(&bId);//可以获取其他特征,根据具体元素而定
CString strId=bId;
if(!strId.IsEmpty() && strId=="callNo")//根据id是主叫号码获取值或作其他处理
{
IHTMLInputTextElement* input;
pDisp->QueryInterface(IID_IHTMLInputTextElement,(void**)&input);
BSTR bVal;
input->get_value(&bVal);
if(bVal==NULL) glb_strCaller="";
else glb_strCaller=bVal;
break;
}
}
}
}
//-----------结束---------------------//
2.执行代码:
void CDemoDlg::OnOK()
{
// TODO: Add extra validation here
::CoInitialize(NULL); //初始化COM
EnumIE(); //枚举浏览器
::CoUninitialize(); //释放COM
//CDialog::OnOK();
}