> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XUserGetLocalId

> XUserGetLocalId

# XUserGetLocalId

Retrieves a unique game session id for a user.

## Syntax

```cpp theme={null}
HRESULT XUserGetLocalId(  
         XUserHandle user,  
         XUserLocalId* userLocalId  
)  
```

### Parameters

*user*   \_In\_\
Type: XUserHandle

A handle to the user to retrieve the local ID for.

*userLocalId*   \_Out\_\
Type: [XUserLocalId\*](/reference/system/xuser/structs/xuserlocalid)

Contains a unique game session id for a user.

### Return value

Type: HRESULT

HRESULT success or error code.\
For a list of error codes, see [Error Codes](/reference/errorcodes).

## Remarks

To retrieve the user ID for a specific user, call [XUserGetId](/reference/system/xuser/functions/xusergetid).

To retrieve the state of a specific user, call [XUserGetState](/reference/system/xuser/functions/xusergetstate).

To retrieve the age group for a specific user, call [XUserGetAgeGroup](/reference/system/xuser/functions/xusergetagegroup).

The **XUserLocalId** uniquely identifies a user within a gaming session. If you duplicate **XUserHandle**, both handles maintain the same **XUserLocalId**. Once the gaming session ends, the same user could have a different **XUserLocalId** during the next session. Do not use these IDs to track users across gaming sessions.

The following example demonstrates how to use the **XUserLocalId** to retrieve the default audio endpoint for a user.

```cpp theme={null}
HRESULT GetAudioDeviceAssociation(
    IMMDeviceEnumerator* audioDeviceEnumerator,
    XUserLocalId user,
    XUserDefaultAudioEndpointKind defaultAudioEndpointKind,
    _Outptr_result_maybenull_ IMMDevice** endpoint)
{
    wchar_t audioDeviceId[XUserAudioEndpointMaxUtf16Count];
    RETURN_IF_FAILED_WITH_EXPECTED(XUserGetDefaultAudioEndpointUtf16(user, defaultAudioEndpointKind, std::size(audioDeviceId), audioDeviceId, nullptr), E_NOTFOUND);

    return audioDeviceEnumerator->GetDevice(audioDeviceId, endpoint);
}

HRESULT GetAudioAssociations()
{
    Mwrl::ComPtr<IMMDeviceEnumerator> audioDeviceEnumerator;

    RETURN_IF_FAILED(CoCreateInstance(
        __uuidof(MMDeviceEnumerator),
        nullptr,
        CLSCTX_ALL,
        __uuidof(IMMDeviceEnumerator),
        (void**)&audioDeviceEnumerator));

    XUserLocalId userLocalId;
    RETURN_IF_FAILED(XUserGetLocalId(_handle.get(), &userLocalId));

    {
        wil::unique_cotaskmem_string id;
        Mwrl::ComPtr<IMMDevice> device;
        if (SUCCEEDED(GetAudioDeviceAssociation(audioDeviceEnumerator.Get(), userLocalId, XUserDefaultAudioEndpointKind::CommunicationRender, &device)))
        {
            RETURN_IF_FAILED(device->GetId(&id));
        }
        appLog.AddLog("Preferred render communication device id: %S\n", id.get());
    }

    {
        wil::unique_cotaskmem_string id;
        Mwrl::ComPtr<IMMDevice> device;
        if (SUCCEEDED(GetAudioDeviceAssociation(audioDeviceEnumerator.Get(), userLocalId, XUserDefaultAudioEndpointKind::CommunicationCapture, &device)))
        {
            RETURN_IF_FAILED(device->GetId(&id));
        }
        appLog.AddLog("Preferred capture communication device id: %S\n", id.get());
    }

    return S_OK;
}
```

## Requirements

**Header:** XUser.h

**Library:** xgameruntime.lib

**Supported platforms:** Windows, Steam Deck, XBOX One family consoles and XBOX Series consoles

## See also

[XUser](/reference/system/xuser/xuser_members)

[XUserGetId](/reference/system/xuser/functions/xusergetid)

[XUserGetState](/reference/system/xuser/functions/xusergetstate)

[XUserGetAgeGroup](/reference/system/xuser/functions/xusergetagegroup)


## Related topics

- [XUserLocalId](/reference/system/xuser/structs/xuserlocalid.md)
- [Users and input devices](/build/core-features/common/user/users-input-devices.md)
- [User identity and XUser](/build/core-features/common/user/player-identity-xuser.md)
- [XUser](/reference/system/xuser/xuser_members.md)
- [XUserFindUserByLocalId](/reference/system/xuser/functions/xuserfinduserbylocalid.md)
