here is my code
#include "iostream"
#include <Windows.h>
#include <stdio.h>
#include <AtlBase.h>
#include <AtlCom.h>
#include <UIAutomation.h>
bool find_url(IUIAutomation* uia, IUIAutomationElement* root)
{
std::cout << "find_url starting ..." << std::endl;
// The root window has several childs,
// one of them is a "pane" named "Google Chrome"
// This contains the toolbar. Find this "Google Chrome" pane:
CComPtr<IUIAutomationElement> pane;
CComPtr<IUIAutomationCondition> pane_cond;
uia->CreatePropertyCondition(UIA_ControlTypePropertyId,
CComVariant(UIA_PaneControlTypeId), &pane_cond);
CComPtr<IUIAutomationElementArray> arr;
if FAILED(root->FindAll(TreeScope_Children, pane_cond, &arr))
return false;
int count = 0;
arr->get_Length(&count);
std::cout << "start find_url count " << count << std::endl;
for (int i = 0; i < count; i++)
{
CComBSTR name;
if SUCCEEDED(arr->GetElement(i, &pane))
std::cout << "start find_url pane " << pane << std::endl;
if SUCCEEDED(pane->get_CurrentName(&name))
std::wcout << "start find_url pane name " << name.m_str << std::endl;
if (wcscmp(name, L"Chrome Legacy Window") == 0)
break;
pane.Release();
}
std::cout << "find_url panel chrome " << pane << std::endl;
if (!pane)
return false;
std::cout << "find_url gg chrome panel existed " << pane << " count : " << count << std::endl;
//look for first UIA_EditControlTypeId under "Google Chrome" pane
CComPtr<IUIAutomationElement> url;
CComPtr<IUIAutomationCondition> url_cond;
uia->CreatePropertyCondition(UIA_ControlTypePropertyId,
CComVariant(UIA_EditControlTypeId), &url_cond);
if FAILED(pane->FindFirst(TreeScope_Descendants, url_cond, &url))
return false;
std::cout << "CreatePropertyCondition url " << url << std::endl;
//get value of `url`
CComVariant var;
if FAILED(url->GetCurrentPropertyValue(UIA_ValueValuePropertyId, &var))
return false;
if (!var.bstrVal)
return false;
wprintf(L"find_url: %s\n", var.bstrVal);
//set new address ...
IValueProvider* pattern = nullptr;
if (FAILED(url->GetCurrentPattern(UIA_ValuePatternId, (IUnknown**)&pattern)))
return false;
//pattern->SetValue(L"somewhere.com");
pattern->Release();
INPUT input[2] = { INPUT_KEYBOARD };
input[0].ki.wVk = VK_RETURN;
input[1] = input[0];
input[1].ki.dwFlags |= KEYEVENTF_KEYUP;
SendInput(2, input, sizeof(INPUT));
return true;
}
int main() {
std::cout << "hello world" << std::endl;
HWND hwnd = nullptr;
while (true)
{
hwnd = FindWindowEx(nullptr, hwnd, L"Chrome_WidgetWin_1", nullptr);
if (!hwnd)
return 0;
if (IsWindowVisible(hwnd) && GetWindowTextLength(hwnd) > 0)
break;
}
std::cout << "CoInitializeEx" << std::endl;
//CoInitializeEx(nullptr, COINIT_MULTITHREADED);//<- pick the right one
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
CComPtr<IUIAutomation> uia;
while (true) {
unsigned int miliseconds = 1000;
Sleep(miliseconds);
std::cout << "SUCCEEDED start : " << SUCCEEDED(uia.CoCreateInstance(CLSID_CUIAutomation)) << std::endl;
if SUCCEEDED(uia.CoCreateInstance(CLSID_CUIAutomation))
{
CComPtr<IUIAutomationElement> root;
if SUCCEEDED(uia->ElementFromHandle(hwnd, &root))
find_url(uia, root);
}
}
uia.Release();
CoUninitialize();
return 1;
}