Advertisement
Guest User

main.cpp

a guest
Jan 6th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <QCoreApplication>
  2. #include <QDebug>
  3. #include "UIAutomation.h"
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     QCoreApplication a(argc, argv);
  8.  
  9.     IUIAutomation *uiauto;
  10.     CoInitialize(NULL);
  11.     HRESULT hr = CoCreateInstance(__uuidof(CUIAutomation), NULL, CLSCTX_INPROC_SERVER, __uuidof(IUIAutomation),(void**)&uiauto);
  12.  
  13.     HWND topWindow = GetForegroundWindow();
  14.  
  15.     if(topWindow == NULL)
  16.         return 0;
  17.  
  18.     IUIAutomationElement *windElem;
  19.     hr = uiauto->ElementFromHandle(topWindow, &windElem);
  20.     if(FAILED(hr))
  21.     {
  22.         qDebug() << "Can’t access the Window in focus!";
  23.         return 0;
  24.     }
  25.  
  26.     BSTR bWinName;
  27.     hr = windElem->get_CurrentName(&bWinName);
  28.     if(FAILED(hr))
  29.         return 0;
  30.  
  31.     QString windowName = QString::fromStdWString(std::wstring(bWinName, SysStringLen(bWinName)));
  32.     SysFreeString(bWinName);
  33.  
  34.     qDebug() << windowName;
  35.     if(uiauto != NULL)
  36.         uiauto->Release();
  37.  
  38.     CoUninitialize();
  39.  
  40.     return a.exec();
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement