Last active
December 8, 2017 08:19
-
-
Save lniwn/2b3b864ba040ebaabd3fae1657a43a2d to your computer and use it in GitHub Desktop.
get ie hwnd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// level: IE窗口的层级,一共三级 | |
// 0: Shell Embedding | |
// 1: Shell DocObject View | |
// 2: Internet Explorer_Server | |
HWND getIEHandle(QAxWidget* pWidget, int level) | |
{ | |
assert(pWidget); | |
CComPtr<IWebBrowser2> pWebBrowser2; | |
pWidget->queryInterface( IID_IWebBrowser2, (void**)&pWebBrowser2 ); | |
assert(pWebBrowser2); | |
CComPtr<IServiceProvider> pServiceProvider; | |
// 1.|--Shell Embedding | |
if (SUCCEEDED(pWebBrowser2->QueryInterface(IID_IServiceProvider, | |
(void **)&pServiceProvider))) | |
{ | |
CComPtr<IOleWindow> pWindow; | |
if (SUCCEEDED(pServiceProvider->QueryService( | |
SID_SShellBrowser, IID_IOleWindow, (void **)&pWindow))) | |
{ | |
HWND hwndBrowser = NULL; | |
if (SUCCEEDED(pWindow->GetWindow(&hwndBrowser))) | |
{ | |
if (level == 0) | |
{ | |
return hwndBrowser; | |
} | |
// 2.|--Shell DocObject View | |
HWND hchildwnd = ::GetWindow(hwndBrowser, GW_CHILD); | |
while (hchildwnd) | |
{ | |
TCHAR wndname[MAX_PATH] = {0}; | |
::GetClassName(hchildwnd, wndname, MAX_PATH); | |
if (wcscmp(wndname, L"Shell DocObject View") == 0) | |
{ | |
if (level == 1) | |
{ | |
return hchildwnd; | |
} | |
// 3.|--Internet Explorer_Server | |
HWND hiewnd = ::GetWindow(hchildwnd, GW_CHILD); | |
while (hiewnd) | |
{ | |
TCHAR wndname[MAX_PATH] = {0}; | |
::GetClassName(hiewnd, wndname, MAX_PATH); | |
if (wcscmp(wndname, L"Internet Explorer_Server") == 0) | |
{ | |
return hiewnd; | |
} | |
hiewnd = ::GetNextWindow(hiewnd, GW_HWNDNEXT); | |
} | |
return NULL; | |
} | |
hchildwnd = ::GetNextWindow(hchildwnd, GW_HWNDNEXT); | |
} | |
return NULL; | |
} | |
} | |
} | |
return NULL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment