none
RuntimeID usage in identifying UI Element in Automation using UIA RRS feed

  • Question

  • Hi,

    We are developing an automation code in C++ for a scenario of invoking all the icons in the start screen, desktop & charms bar and if any icon opens up a window application and it should get closed.

    We registered window event handler and invoking each control in the start screen.  If any controls up a window application, we are handling in HandleAutomationEvent method and closing the application by sending ALT+F4 key press.

    But we kind of see the window event getting triggered multiple times in case of any metro app getting opened up.  So overcome this issue, we got a suggestion from Winqual to compare the runtime IDs before generating ALT+F4 key press.

    Please find the below snapshot of the code on how we are using RunTimeIDs for overcoming the multiple window event triggering issue,

    SAFEARRAY prevRunTimeID; //Global
    WindowsOpenEvent()  // This will be called from HandleAutomationEvent for WindowOpenEvent
    {
          SAFEARRAY *winRunTimeID;
          hr = ((IUIAutuomationElement *)pSender)->GetRuntimeId(&winRunTimeID);
          hr = g_pAutomation->CompareRuntimeIds(winRunTimeID, &prevRunTimeID, &same);
          if(!same)
         {
           //Mutex lock
           Hr = SafeArrayCopyData(WinRunTimeID,&prevRunTimeID);
           if((FAILED(hr))
          {
             printf(“Hr [%x]\n”,hr);
          }
          //Mutex unlock
       }
    }
    MainThread()
    {
        g_pAutomation->AddWindowsEventHandler();
        memset(&prevRunTimeID,0,sizeof(SAFEARRAY));
    }

    With the above code, SafeArrayCopyData keeps failing with E_INVALIDARG return value. 

    I am suspicious on the memset call that we are making for the preRunTimeID (SAFEARRAY).  I am not sure how to initialize this variable.  I tried looking into SafeArrayCreate, but I am not sure how much we need to allocate for runtime ID.

    Is the runtime ID is of same size of all the UI elements? or does it vary?  Also, could you please tell us what would be the standard default size for the runtime ID? so that we can define a SAFEARRAY of the default size and use it for storing the runtime ID.

    Or could you please point us if we are making any other mistake in the above code?

    It would be good if we get some help on this issue.

    Thanks & Regards,

    Kanchana 

     

    Thursday, July 19, 2012 9:33 PM

Answers

  • On 7/19/2012 5:33 PM, Kanchanadevi Suriyamoorthy wrote:

    SAFEARRAY prevRunTimeID; //Global

    You can't declare a SAFEARRAY like this. It needs to be created with SafeArrayCreate or similar.

    I tried looking into SafeArrayCreate, but I am not sure how much we need to allocate for runtime ID.

    Why not just do something like this:

    SAFEARRAY* prev = NULL;
    SAFEARRAY* cur;
    GetRuntimeId(&cur);
    
    BOOL same = false;
    if (prev) CompareRuntimeIds(prev, cur, &same);
    if (!same) {
        if (prev) SafeArrayDestroy(prev);
        prev = cur;
    }

    Note that, in your current code, you appear to be leaking the safearray you are getting from GetRuntimeId. It's your responsibility to destroy it once you are done with it.


    Igor Tandetnik

    Thursday, July 19, 2012 11:46 PM

All replies

  • On 7/19/2012 5:33 PM, Kanchanadevi Suriyamoorthy wrote:

    SAFEARRAY prevRunTimeID; //Global

    You can't declare a SAFEARRAY like this. It needs to be created with SafeArrayCreate or similar.

    I tried looking into SafeArrayCreate, but I am not sure how much we need to allocate for runtime ID.

    Why not just do something like this:

    SAFEARRAY* prev = NULL;
    SAFEARRAY* cur;
    GetRuntimeId(&cur);
    
    BOOL same = false;
    if (prev) CompareRuntimeIds(prev, cur, &same);
    if (!same) {
        if (prev) SafeArrayDestroy(prev);
        prev = cur;
    }

    Note that, in your current code, you appear to be leaking the safearray you are getting from GetRuntimeId. It's your responsibility to destroy it once you are done with it.


    Igor Tandetnik

    Thursday, July 19, 2012 11:46 PM
  • Wow...!!! thats great... thanks for your help...!!! the issue is fixed...

    Regards,

    Kanchana

    Friday, July 20, 2012 8:47 PM