How to Return Properties from a UI Automation Provider

This topic contains example code that shows how a Microsoft UI Automation provider returns properties of a UI element to client applications.

To retrieve a property value from the provider, UI Automation calls a provider's implementation of the IRawElementProviderSimple::GetPropertyValue method, passing the ID of the property to retrieve, and a pointer to a VARIANT structure. If the provider supports the specified property, it copies the data type and value of the property into the VARIANT structure. If the property is not supported, the provider sets the vt member of the VARIANT structure to VT_EMPTY.

IFACEMETHODIMP Provider::GetPropertyValue(PROPERTYID propertyId, VARIANT* pRetVal)
{
    // The Name property is typically the same as the Caption property of the 
    // control window, if it has one. Here, the Name is overridden for the 
    // sake of illustration. 
    if (propertyId == UIA_NamePropertyId) 
    {
        pRetVal->vt = VT_BSTR;
        pRetVal->bstrVal = SysAllocString(L"Custom button");
    }
    
    else if (propertyId == UIA_ControlTypePropertyId) 
    {
        pRetVal->vt = VT_I4;
        pRetVal->lVal = UIA_ButtonControlTypeId; 
    }

    else if (propertyId == UIA_IsContentElementPropertyId) 
    {
        pRetVal->vt = VT_BOOL;
        pRetVal->lVal = TRUE; 
    }

    else if (propertyId == UIA_IsControlElementPropertyId) 
    {
        pRetVal->vt = VT_BOOL;
        pRetVal->lVal = TRUE; 
    }

    //
    // Return other properties as appropriate for the control type. 
    //

    else
    {
        pRetVal->vt = VT_EMPTY;
        // UI Automation will attempt to get the property from the  
        // provider for window that hosts the control.
    }
    return S_OK;
}

Conceptual

UI Automation Properties Overview

How-To Topics for UI Automation Providers