Export (0) Print
Expand All

Using IAccessibleEx from a Client

This topic explains how clients access the IAccessibleEx implementation of a server and use it to get UI Automation properties and control patterns for UI elements.

The procedures and examples in this section assume an IAccessible client that is already in-process, and an existing Microsoft Active Accessibility server. They also assume that the client has already obtained an IAccessible object by using one of the accessibility framework functions such as AccessibleObjectFromEvent, AccessibleObjectFromPoint, or AccessibleObjectFromWindow.

Obtaining an IAccessibleEx Interface from the IAccessible Interface

A client that has an IAccessible interface for an accessible object can use it to obtain the corresponding IAccessibleEx interface by following these steps:

Handling the Child ID

Clients must be prepared for servers with a child ID other than CHILDID_SELF. After obtaining an IAccessibleEx interface from an IAccessible, clients must call IAccessibleEx::GetObjectForChild if the child ID is not CHILDID_SELF (indicating a parent object).

The following example shows how to get an IAccessibleEx for a particular IAccessible object and child ID.


	
HRESULT GetIAccessibleExFromIAccessible(IAccessible * pAcc, long idChild, 
                                           IAccessibleEx ** ppaex)
{
    *ppaex = NULL;

    // First, get IServiceProvider from the IAccessible.
    IServiceProvider * pSp = NULL;
    HRESULT hr = pAcc->QueryInterface(IID_IServiceProvider, (void **) & pSp);
    if(FAILED(hr))
        return hr;
    if(pSp == NULL)
        return E_NOINTERFACE;

    // Next, get the IAccessibleEx for the parent object.
    IAccessibleEx * paex = NULL;
    hr = pSp->QueryService(__uuidof(IAccessibleEx), __uuidof(IAccessibleEx),
                                                                 (void **)&paex);
    pSp->Release();
    if(FAILED(hr))
        return hr;
    if(paex == NULL)
        return E_NOINTERFACE;

    // If this is for CHILDID_SELF, we're done. Otherwise, we have a child ID and 
    // can request the object for child.
    if(idChild == CHILDID_SELF)
    {
        *ppaex = paex;
        return S_OK;
    }
    else
    {
        // Get the IAccessibleEx for the specified child.
        IAccessibleEx * paexChild = NULL;
        hr = paex->GetObjectForChild(idChild, &paexChild);
        paex->Release();
        if(FAILED(hr))
            return hr;
        if(paexChild == NULL)
            return E_NOINTERFACE;
        *ppaex = paexChild;
        return S_OK;
    }
}

Obtaining the IRawElementProviderSimple Interface

If a client has an IAccessibleEx interface, it can use QueryInterface to get to the IRawElementProviderSimple interface, as the following example shows.



HRESULT GetIRawElementProviderFromIAccessible(IAccessible * pAcc, long idChild,
                                                 IRawElementProviderSimple ** ppEl)
{
    * ppEl = NULL;

    // First, get the IAccessibleEx for the IAccessible and child ID pair.
    IAccessibleEx * paex;
    HRESULT hr = GetIAccessibleExFromIAccessible( pAcc, idChild, &paex );
    if(FAILED(hr))
        return hr;

    // Next, use QueryInterface.
    hr = paex->QueryInterface(__uuidof(IRawElementProviderSimple), (void **)ppEl);
    paex->Release();
    return hr;
}

Retrieving Control Patterns

If a client has access to the IRawElementProviderSimple interface, it can retrieve control pattern interfaces that have been implemented by providers, and can then call methods on those interfaces. The following example shows how to do this.



// Helper function to get a pattern interface from an IAccessible and child ID 
// pair. Gets the IAccessibleEx, then calls GetPatternObject and QueryInterface.
HRESULT GetPatternFromIAccessible(IAccessible * pAcc, long idChild,
                                     PATTERNID patternId, REFIID iid, void ** ppv)
{
    // First, get the IAccesibleEx for this IAccessible and child ID pair.
    IRawElementProviderSimple * pel;
    HRESULT hr = GetIRawElementProviderSimpleFromIAccessible(pAcc, idChild, &pel);
    if(FAILED(hr))
        return hr;
    if(pel == NULL)
        return E_NOINTERFACE;

    // Now get the pattern object.
    IUnknown * pPatternObject = NULL;
    hr = pel->GetPatternProvider(patternId, &pPatternObject);
    pel->Release();
    if(FAILED(hr))
        return hr;
    if(pPatternObject == NULL)
        return E_NOINTERFACE;

    // Finally, use QueryInterface to get the correct interface type.
    hr = pPatternObject->QueryInterface(iid, ppv);
    pPatternObject->Release();
    if(*ppv == NULL)
        return E_NOINTERFACE;
    return hr;
}

HRESULT CallInvokePatternMethod(IAccessible * pAcc, long idChild)
{
    IInvokeProvider * pPattern;
    HRESULT hr = GetPatternFromIAccessible(pAcc, varChild,
                                  UIA_InvokePatternId, __uuidof(IInvokeProvider),
                                  (void **)&pPattern);
    if(FAILED(hr))
        return hr;

    hr = pPattern->Invoke();
    pPattern->Release();
    return hr;
}

Retrieving Property Values

If a client has access to IRawElementProviderSimple, it can retrieve property values. The following example shows how to get values for the AutomationId and LabeledBy Microsoft UI Automation properties.



#include <initguid.h>
#include <uiautomationcoreapi.h> // Includes the UI Automation property GUID definitions.
#include <uiautomationcoreids.h> // Includes definitions of pattern/property IDs.

// Assume we already have a IRawElementProviderSimple * pEl.

VARIANT varValue;

// Get AutomationId property:
varValue.vt = VT_EMPTY;
HRESULT hr = pEl->GetPropertyValue(UIA_AutomationIdPropertyId, &varValue);
if(SUCCEEDED(hr))
{
    if(varValue.vt == VT_BSTR)
    {
        // AutomationId is varValue.bstrVal.
    }
    VariantClear(&varValue);
}


// Get LabeledBy property:
varValue.vt = VT_EMPTY;
hr = pEl->GetPropertyValue(UIA_LabeledByPropertyId, &varValue);
if(SUCCEEDED(hr))
{
    if(varValue.vt == VT_UNKNOWN || varValue.punkVal != NULL)
    {
        // Use QueryInterface to get IRawElementProviderSimple.
        IRawElementProviderSimple * pElLabel = NULL;
        hr = varValue.punkVal->QueryInterface(__uuidof(IRawElementProviderSimple),
                                              (void**)& pElLabel);
        if (SUCCEEDED(hr))
        {
            if(pElLabel != NULL)
            {
            // Use the pElLabel pointer here.
            pElLabel ->Release();
            }
        }
    }
    VariantClear(&varValue);
}

The preceding example applies to properties that are not associated with a control pattern. To access control pattern properties, a client must obtain and use a control pattern interface.

Retrieving an IAccessible Interface from an IRawElementProviderSimple Interface

If a client obtains the IRawElementProviderSimple interface for a UI element, the client can use that interface to obtain a corresponding IAccessible interface for the element. This is useful if the client needs to access the Microsoft Active Accessibility properties for the element.

A client can obtain the IRawElementProviderSimple interface as a property value (for example, by calling IRawElementProviderSimple::GetPropertyValue with UIA_LabeledByPropertyId), or as an item retreived by a method (for example, by calling ISelectionProvider::GetSelection to retrieve an array of IRawElementProviderSimple interfaces of selected elements). After obtaining the IRawElementProviderSimple interface, a client can use it to obtain a corresponding IAccessible by following these steps:

The following code snippet demonstrates how to obtain the IAccessible interface from a previously-obtained IRawElementProviderSimple interface.



// IRawElementProviderSimple * pVal - an element returned by a property or method
// from another IRawElementProviderSimple.

IAccessible * pAcc = NULL;
long idChild;

// First, try to use QueryInterface to get the IAccessibleEx interface.
IAccessibleEx * pAccEx;
HRESULT hr = pVal->QueryInterface(__uuidof(IAccessibleEx), (void**)&pAccEx);
if (SUCCEEDED(hr)
{
    if (!pAccEx)
    {
        // If QueryInterface fails, and the IRawElementProviderSimple was 
		      // obtained as a property or return value from another 
    		  // IRawElementProviderSimple, pass it to the 
		      // IAccessibleEx::ConvertReturnedValue method of the
        // originating element.

        pAccExOrig->ConvertReturnedElement(pVal, &pAccEx);
    }

    if (pAccEx)
    {
        // Call GetIAccessiblePair to get an IAccessible interface and 
		      // child ID.
        pAccEx->GetIAccessiblePair(&pAcc, &idChild);
    }

    // Finally, use the IAccessible interface and child ID.
    if (pAcc)
    {
        // Use IAccessible methods to get further information about this UI
    		  // element, or pass it to existing code that works in terms of 
    		  // IAccessible.
        ...
    }
}	 


 

 

Community Additions

ADD
Show:
© 2014 Microsoft