> ## 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

检索用户的唯一游戏会话 ID。

## 语法

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

### 参数

*user*   \_In\_\
类型：XUserHandle

要检索本地 ID 的用户的句柄。

*userLocalId*   \_Out\_\
类型：[XUserLocalId\*](/reference/system/xuser/structs/xuserlocalid)

包含用户的唯一游戏会话 ID。

### 返回值

类型：HRESULT

HRESULT 成功或错误代码。\
有关错误代码列表，请参阅[错误代码](/reference/errorcodes)。

## 注解

若要检索特定用户的用户 ID，请调用 [XUserGetId](/reference/system/xuser/functions/xusergetid)。

若要检索特定用户的状态，请调用 [XUserGetState](/reference/system/xuser/functions/xusergetstate)。

若要检索特定用户的年龄组，请调用 [XUserGetAgeGroup](/reference/system/xuser/functions/xusergetagegroup)。

**XUserLocalId** 在游戏会话中唯一标识一个用户。如果复制 **XUserHandle**，则两个句柄将维护相同的 **XUserLocalId**。一旦游戏会话结束，同一用户在下一次会话期间可能具有不同的 **XUserLocalId**。请勿使用这些 ID 跨游戏会话跟踪用户。

以下示例演示如何使用 **XUserLocalId** 检索用户的默认音频终结点。

```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;
}
```

## 要求

**头文件：** XUser.h

**库：** xgameruntime.lib

**支持的平台：** Windows、Steam Deck、XBOX One 系列主机和 XBOX Series 主机

## 另请参阅

[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](/zh-CN/reference/system/xuser/structs/xuserlocalid.md)
- [用户与输入设备](/zh-CN/build/core-features/common/user/users-input-devices.md)
- [用户身份与 XUser](/zh-CN/build/core-features/common/user/player-identity-xuser.md)
- [XUser](/zh-CN/reference/system/xuser/xuser_members.md)
- [面向 GDK 的 Unity C# API 包装器](/zh-CN/build/gdk-and-engines/unity/unity-api-wrappers.md)
