Skip to content

Instantly share code, notes, and snippets.

@kumatti1
Created Apr 9, 2017
Embed
What would you like to do?
F12
//#define UNICODE
#include <stdio.h>
#include <windows.h>
#include <Oleacc.h>
#include <stdio.h>
#include <Shlwapi.h>
#include <shobjidl.h>
#import <mshtml.tlb>
#pragma comment(lib, "shlwapi.lib")
BOOL CALLBACK EnumWindowsProc(
_In_ HWND hwnd,
_In_ LPARAM lParam
);
BOOL CALLBACK EnumChildProc(
_In_ HWND hwnd,
_In_ LPARAM lParam
);
HRESULT GetDOM();
HRESULT F12(MSHTML::IHTMLDocument2Ptr pDoc);
HWND hF12 = 0;
HWND hIES = 0;
UINT msg = 0;
HRESULT hoge()
{
HRESULT hr;
try {
EnumWindows(EnumWindowsProc, 0);
if (hF12 == 0)
return S_FALSE;
EnumChildWindows(hF12, EnumChildProc, 0);
if (hIES == 0)
return S_FALSE;
GetDOM();
}
catch (_com_error& e) {
hr = e.Error();
MessageBoxA(0, "", e.ErrorMessage(), MB_OK);
}
return hr;
}
HRESULT GetDOM()
{
HRESULT hr;
try {
UINT msg = RegisterWindowMessageW(L"WM_HTML_GETOBJECT");
DWORD_PTR res = 0;
SendMessageTimeoutW(hIES, msg, 0, 0, SMTO_ABORTIFHUNG, 1000, &res);
MSHTML::IHTMLDocument2Ptr d = nullptr;
if (res) {
HRESULT hr = ObjectFromLresult(res, __uuidof(MSHTML::IHTMLDocument2Ptr), 0, (void**)&d);
if (d) {
for (;;) {
if (lstrcmpW(d->readyState, L"complete") == 0)
break;
::Sleep(100);
}
F12(d);
}
}
}
catch (_com_error& e) {
hr = e.Error();
MessageBoxA(0, "", e.ErrorMessage(), MB_OK);
}
return hr;
}
HRESULT F12(MSHTML::IHTMLDocument2Ptr pDoc)
{
HRESULT hr = S_OK;
try {
MessageBoxW(0, pDoc->url, L"", MB_OK);
}
catch (_com_error& e) {
hr = e.Error();
MessageBoxA(0, "", e.ErrorMessage(), MB_OK);
}
return hr;
}
BOOL CALLBACK EnumWindowsProc(
_In_ HWND hwnd,
_In_ LPARAM lParam
)
{
WCHAR buf1[255];
WCHAR buf2[255];
GetClassNameW(hwnd, buf1, sizeof(buf1));
if (lstrcmpW(buf1, L"F12FrameWindow") == 0) {
hF12 = hwnd;
return FALSE;
}
return TRUE;
}
BOOL CALLBACK EnumChildProc(
_In_ HWND hwnd,
_In_ LPARAM lParam
)
{
WCHAR buf[255];
GetClassNameW(hwnd, buf, sizeof(buf));
if (lstrcmpW(buf, L"Internet Explorer_Server") == 0) {
hIES = hwnd;
return FALSE;
}
return TRUE;
}
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
)
{
CoInitialize(nullptr);
hoge();
CoUninitialize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment