Skip to content

Instantly share code, notes, and snippets.

@kumatti1
Created December 6, 2015 02:43
Show Gist options
  • Save kumatti1/956d92bae61dfed28d18 to your computer and use it in GitHub Desktop.
Save kumatti1/956d92bae61dfed28d18 to your computer and use it in GitHub Desktop.
Edge制御
//http://www.ka-net.org/blog/?p=6033
//Microsoft Edgeを操作するVBAマクロ(DOM編) | 初心者備忘録
#define UNICODE
#include <stdio.h>
#include <windows.h>
#include <Oleacc.h>
#include <stdio.h>
#import <mshtml.tlb>
#define s L"microsoft-edge:http://www.yahoo.co.jp/"
BOOL CALLBACK EnumChildProcEdge(
_In_ HWND hwnd,
_In_ LPARAM lParam
);
BOOL CALLBACK EnumChildProcIES(
_In_ HWND hwnd,
_In_ LPARAM lParam
);
HRESULT GetDOM();
HWND hEdge = 0;
HWND hIES = 0;
void hoge()
{
//Edge起動
ShellExecuteW(0, nullptr, s, nullptr, nullptr, SW_SHOWNORMAL);
Sleep(2000);
EnumChildWindows(0, EnumChildProcEdge, 0);
if (hEdge == 0)
return;
EnumChildWindows(hEdge, EnumChildProcIES, 0);
if (hIES == 0)
return;
GetDOM();
}
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);
}
MSHTML::IHTMLDocument3Ptr dd = d;
MSHTML::IHTMLInputElementPtr pVlue = dd->getElementById(L"srchtxt");
pVlue->value = L"hoge";
}
}
}
catch (_com_error& e) {
hr = e.Error();
}
return hr;
}
BOOL CALLBACK EnumChildProcEdge(
_In_ HWND hwnd,
_In_ LPARAM lParam
)
{
WCHAR buf1[255];
WCHAR buf2[255];
GetClassNameW(hwnd, buf1, sizeof(buf1));
if (lstrcmpW(buf1, L"ApplicationFrameWindow") == 0){
GetWindowTextW(hwnd, buf2, sizeof(buf2));
if (wcsstr(buf2, L"Microsoft Edge") != nullptr){
hEdge = hwnd;
return FALSE;
}
}
return TRUE;
}
BOOL CALLBACK EnumChildProcIES(
_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