#include <iostream>
#include <tchar.h>
#import <mshtml.tlb>
#import <shdocvw.dll>
void GetAllTablesInRunningIE()
{
// Import the following files in your stdafx.h
// #import <mshtml.tlb> // Internet Explorer 5
// Refer to "Connect to Internet Explorer Instances, From your own Process. " in www.codeguru.com
// enumeration all shell windows, you have to
//modify according your own need.
// masterz vc2003 windows2003 IE6
SHDocVw::IShellWindowsPtr m_spSHWinds;
if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK)
{
long nCount = m_spSHWinds->GetCount();
for (long i = 0; i < nCount; i++)
{
IDispatchPtr spDisp;
_variant_t va(i, VT_I4);
spDisp = m_spSHWinds->Item(va);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
if (spBrowser != NULL)
{
IDispatchPtr pDispDoc;
if(spBrowser->get_Document(&pDispDoc) == S_OK && pDispDoc!= 0 )
{
MSHTML::IHTMLDocument2Ptr spHtmlDocument(pDispDoc);
if(spHtmlDocument == NULL)
continue;
MSHTML::IHTMLElementCollectionPtr pColl=NULL;
HRESULT hr=spHtmlDocument->get_all(&pColl);
if(pColl == NULL)
continue;
IDispatchPtr pDispCol = pColl->tags(_variant_t("table"));
MSHTML::IHTMLElementCollectionPtr pAllTables(pDispCol);
if(pAllTables)
{
std::cout<<"There are "<<pAllTables->Getlength()<<" in the document"<<std::endl;
}
}
}
}
}
else {
std::cout<<("Shell Windows interface is not available")<<std::endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
GetAllTablesInRunningIE();
CoUninitialize();
system("pause");
return 0;
}
1 WebBrowser Control Overviews and Tutorials
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/browser_control_ovw_entry.asp
2 俺只知道应该是通过IHTMLDocument2接口来操作
http://dev.csdn.net/develop/article/28/28107.shtm