int _tmain(int argc, _TCHAR* argv[]) { CoInitialize(NULL) ; IUIAutomation *pAutomation ; HRESULT hr = CoCreateInstance(__uuidof(CUIAutomation), NULL, CLSCTX_INPROC_SERVER, __uuidof(IUIAutomation), (void**)&pAutomation); if (SUCCEEDED(hr)) { printf("got IUIAutomation\r\n") ; IUIAutomationElement *pRootElement ; if (SUCCEEDED(pAutomation->GetRootElement(&pRootElement))) { BSTR bstrRootElementName ; pRootElement->get_CurrentName(&bstrRootElementName) ; IUIAutomationCondition *pCondition ; VARIANT varProperty ; VariantInit(&varProperty) ; varProperty.vt = VT_BSTR ; varProperty.bstrVal = SysAllocString(L"aboutButton") ; if (SUCCEEDED(pAutomation->CreatePropertyCondition(UIA_AutomationIdPropertyId, varProperty, &pCondition))) { IUIAutomationElement *pFound ; if (SUCCEEDED(pRootElement->FindFirst(TreeScope_Descendants, pCondition, &pFound))) { if (pFound != NULL) { printf("Found item\r\n") ; BSTR bstrName ; pFound->get_CurrentName(&bstrName) ; SysFreeString(bstrName) ; BSTR bstrItemType ; pFound->get_CurrentItemType(&bstrItemType) ; SysFreeString(bstrItemType) ; CONTROLTYPEID controlType ; pFound->get_CurrentControlType(&controlType) ; printf("Control type %d\r\n", controlType) ; IUIAutomationInvokePattern *pInvokePattern ; if (SUCCEEDED(pFound->GetCurrentPatternAs(UIA_InvokePatternId, __uuidof(IUIAutomationInvokePattern), (void **)&pInvokePattern))) { hr = pInvokePattern->Invoke() ; } } else printf("Did not find item\r\n") ; } } } } else printf("Unable to obtain IUIAutomation\r\n") ; return 0; }
//This function builds a tree of nodes given a rootNode and level //A node is represented by an UI element //The function builds the tree with DFS iteration HRESULT listTree(int level, IUIAutomationElement* rootNode, IUIAutomationTreeWalker* walker) { HRESULT hr; IUIAutomationElement* element = nullptr; auto base = std::wstring(level, L'-'); //Get the first element hr = walker->GetFirstChildElement(rootNode, &element); if (FAILED(hr) || element == nullptr) { return hr; } while (element != nullptr) { //Is this a control element? BOOL isControl; hr = element->get_CurrentIsControlElement(&isControl); if (FAILED(hr)) { logToFile(base + L"Failed element is control type"); logToFile(base + L"Error code: " + std::to_wstring(hr)); } else if (isControl) { //Yes it is -> print it's details BSTR controlName; BSTR controlType; CONTROLTYPEID controlTypeId; hr = element->get_CurrentName(&controlName); hr += element->get_CurrentLocalizedControlType(&controlType); hr += element->get_CurrentControlType(&controlTypeId); if (FAILED(hr)) { logToFile(base + L"Failed element control type"); } if (controlName != nullptr) { logToFile(base + L" NAME: " + static_cast<std::wstring>(controlName)); } if (controlType != nullptr) { logToFile(base + L" TYPE: " + static_cast<std::wstring>(controlType)); } if (controlTypeId != NULL) { logToFile(base + L" ID: " + std::to_wstring(controlTypeId)); } } //Continue with children of the current element listTree(level * 2, element, walker); //After that, the next element to be locked at, is it's sibling IUIAutomationElement* next = nullptr; hr = walker->GetNextSiblingElement(element, &next); if (FAILED(hr)) { logToFile(base + L"Failed next element"); logToFile(base + L"Error code: " + std::to_wstring(hr)); } //Free the memory used for the current element SAFE_RELEASE(element); //Update the pointer to continue iteration element = next; } return S_OK; }