Skip to content

Instantly share code, notes, and snippets.

@kumatti1
Created Apr 6, 2017
Embed
What would you like to do?
Gist
//#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")
#define s L"microsoft-edge:https://gist.github.com/auth/github?return_to=gist"
#define strAppUserModelID L"Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge"
#define URL L"https://gist.github.com/auth/github?return_to=gist"
BOOL CALLBACK EnumWindowsProc(
_In_ HWND hwnd,
_In_ LPARAM lParam
);
BOOL CALLBACK EnumChildProc(
_In_ HWND hwnd,
_In_ LPARAM lParam
);
HRESULT GetDOM();
HRESULT Gist(MSHTML::IHTMLDocument6Ptr pDoc);
HWND hEdge = 0;
HWND hIES = 0;
DWORD dwProcessId = 0;
UINT msg = 0;
_COM_SMARTPTR_TYPEDEF(IApplicationActivationManager, __uuidof(IApplicationActivationManager));
HRESULT hoge()
{
HRESULT hr;
try {
//Edge起動
//ShellExecuteW(0, nullptr, s, nullptr, nullptr, SW_SHOWNORMAL);
IApplicationActivationManagerPtr spAppActivationManager;
HRESULT hr = spAppActivationManager.CreateInstance(CLSID_ApplicationActivationManager);
if (FAILED(hr))
{
_com_issue_error(hr);
}
spAppActivationManager->ActivateApplication(strAppUserModelID, URL, AO_NONE, &dwProcessId);
Sleep(3000);
EnumWindows(EnumWindowsProc, 0);
if (hEdge == 0)
return S_FALSE;
EnumChildWindows(hEdge, 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);
}
Gist(d);
}
}
}
catch (_com_error& e) {
hr = e.Error();
MessageBoxA(0, "", e.ErrorMessage(), MB_OK);
}
return hr;
}
HRESULT Gist(MSHTML::IHTMLDocument6Ptr pDoc)
{
HRESULT hr = S_OK;
try{
MSHTML::IHTMLInputElementPtr ID( pDoc->getElementById( L"login_field") );
ID->value = L"
MSHTML::IHTMLInputElementPtr PW( pDoc->getElementById( L"password") );
PW->value = L"
MSHTML::IHTMLDocument2Ptr pDoc2 ( pDoc );
MSHTML::IHTMLElementCollectionPtr pCol(pDoc2->forms);
MSHTML::IHTMLFormElementPtr pForm(pCol->item(_variant_t(0)));
MSHTML::IHTMLElementPtr pClick( pForm->item(_variant_t(L"commit")));
pClick->click();
}
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"TabWindowClass") == 0){
GetWindowTextW(hwnd, buf2, sizeof(buf2));
if (PathMatchSpecW(buf2, L"Sign in to GitHub*") == TRUE ){
hEdge = 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