Iterate through all the tabs in Internet Explorer Windows

I found this code useful when you want to go through each tab and then look into their URLs and so on.
You can also quit each tab individually.
[sourcecode lang=”cpp”]
// Implementation
#include
#include
typedef BOOL (CALLBACK * IEENUMPROC)(HWND hwnd, LPVOID pif, LPARAM lParam);
static BOOL CALLBACK EnumIEChildWindows(HWND hwnd, LPARAM lParam)
{
TCHAR szClassName[100];
if (GetClassName(hwnd, szClassName, 100) &&
lstrcmp(szClassName, TEXT("Internet Explorer_Server")) == 0)
{
*((HWND *) lParam) = hwnd;
return FALSE;
}
return TRUE;
}
BOOL WINAPI EnumIEWindows(IEENUMPROC lpEnumFunc, REFIID riid, LPARAM lParam)
{
HWND hwnd;
HINSTANCE hInst;
LPFNOBJECTFROMLRESULT pfObjectFromLresult;
UINT uMsg = RegisterWindowMessage(TEXT("WM_HTML_GETOBJECT"));
if (!(hInst = LoadLibrary(TEXT("OLEACC.DLL"))))
{
// Active Accessibility is not available on this machine.
// Windows 2000/XP have AA installed. It is available for other
// platforms as a redistributable.
return FALSE;
}
if (!(pfObjectFromLresult = (LPFNOBJECTFROMLRESULT) GetProcAddress(hInst, "ObjectFromLresult")))
{
FreeLibrary(hInst);
return FALSE;
}
hwnd = FindWindowEx(NULL, NULL, TEXT("IEFrame"), NULL);
while (hwnd != NULL)
{
HWND hwndChild = NULL;
EnumChildWindows(hwnd, EnumIEChildWindows, (LPARAM) &hwndChild);
if (hwndChild)
{
void * pif;
LRESULT lr;
HRESULT hr;
if (SendMessageTimeout(hwndChild, uMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD_PTR *) &lr))
{
hr = pfObjectFromLresult(lr, riid, 0, &pif);
if (SUCCEEDED(hr))
{
if (!lpEnumFunc(hwnd, pif, lParam)) break;
}
} // else document is not ready.
}
hwnd = FindWindowEx(NULL, hwnd, TEXT("IEFrame"), NULL);
}
FreeLibrary(hInst);
return TRUE;
}
—————————————————————
// Optional header file
#ifndef IEENUM_INCLUDED
#define IEENUM_INCLUDED
#include
typedef BOOL (CALLBACK * IEENUMPROC)(HWND hwnd, LPVOID pif, LPARAM lParam);
BOOL WINAPI EnumIEWindows(IEENUMPROC lpEnumFunc, REFIID riid, LPARAM lParam);
#endif
—————————————————————————-
// Sample usage
#include
#include
BOOL CALLBACK EnumIEWindowsProc(HWND hwnd, LPVOID pif, LPARAM lParam)
{
IHTMLDocument2 * pDoc = (IHTMLDocument2 *) pif;
BSTR bstr = NULL;
VARIANT vt;
// — Manipulate document object. —
pDoc->get_URL(&bstr);
MessageBoxW(NULL, bstr, NULL, 0);
SysFreeString(bstr);
V_VT(&vt) = VT_BSTR;
V_BSTR(&vt) = SysAllocString(L"yellow");
pDoc->put_bgColor(vt);
VariantClear(&vt);
// — Shows how to get parent window. —
IHTMLWindow2 * pWindow = NULL;
pDoc->get_parentWindow(&pWindow);
if (pWindow)
{
bstr = SysAllocString(L"Hello World");
pWindow->alert(bstr);
SysFreeString(bstr);
pWindow->Release();
}
// — Shows how to get parent web browser (See Q172763). —
IWebBrowser2 * pBrowser = NULL;
IServiceProvider * psp = NULL;
pDoc->QueryInterface(IID_IServiceProvider, (void **) &psp);
if (psp)
{
psp->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, (void **) &pBrowser);
if (pBrowser)
{
pBrowser->get_LocationName(&bstr);
MessageBoxW(NULL, bstr, NULL, 0);
SysFreeString(bstr);
pBrowser->Release();
}
psp->Release();
}
// Generic release of pif.
((IUnknown *) pif)->Release();
// Return true to conntinue enumeration.
return TRUE;
}
int main(void)
{
CoInitialize(NULL);
if (!EnumIEWindows(EnumIEWindowsProc, IID_IHTMLDocument2, 0))
{
MessageBox(NULL, TEXT("Active Accessibility is not installed. Please install Active Accessibility."),
TEXT("Fatal Error"), MB_ICONSTOP);
return GetLastError();
}
CoUninitialize();
return 0;
[/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *