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

# XUserLocalId

> XUserLocalId

# XUserLocalId

指定用户的游戏会话 ID。

## 语法

```cpp theme={null}
typedef struct XUserLocalId {  
    uint64_t value;  
} XUserLocalId  
```

### 成员

*value*\
类型：uint64\_t

用户的游戏会话 ID。

## 备注

XUserLocalId 唯一标识游戏会话内的用户。如果我们复制 **XUserHandle**，两个句柄将保持相同的 XUserLocalId。一旦游戏会话结束，同一用户在下一次会话中可能会有不同的 XUserLocalId。你不能使用这些 ID 跨游戏会话追踪用户。这些 ID 仅在每个游戏会话中有效。

"null" XUserLocalId 的值等于 **XUserNullUserLocalId**。

以下回调函数和函数将 XUserLocalId 结构或指向它的指针作为参数：

* [XUserChangeEventCallback](https://docs.xbox.com/zh-CN/reference/system/xuser/functions/xuserchangeeventcallback.md)：用于用户更改事件的回调函数。
* [XUserDefaultAudioEndpointUtf16ChangedCallback](https://docs.xbox.com/zh-CN/reference/system/xuser/functions/xuserdefaultaudioendpointutf16changedcallback.md)：当用户的默认音频终结点更改时调用的回调。
* [XUserFindUserByLocalId](https://docs.xbox.com/zh-CN/reference/system/xuser/functions/xuserfinduserbylocalid.md)：使用特定本地 ID 检索用户句柄的函数。
* [XUserGetDefaultAudioEndpointUtf16](https://docs.xbox.com/zh-CN/reference/system/xuser/functions/xusergetdefaultaudioendpointutf16.md)：检索特定用户默认音频终结点的函数。
* [XUserGetLocalId](https://docs.xbox.com/zh-CN/reference/system/xuser/functions/xusergetlocalid.md)：检索用户唯一游戏会话 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

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

## 另请参阅

[XUser](https://docs.xbox.com/zh-CN/reference/system/xuser/xuser_members.md)

[XUserChangeEventCallback](https://docs.xbox.com/zh-CN/reference/system/xuser/functions/xuserchangeeventcallback.md)

[XUserDefaultAudioEndpointUtf16ChangedCallback](https://docs.xbox.com/zh-CN/reference/system/xuser/functions/xuserdefaultaudioendpointutf16changedcallback.md)

[XUserFindUserByLocalId](https://docs.xbox.com/zh-CN/reference/system/xuser/functions/xuserfinduserbylocalid.md)

[XUserGetDefaultAudioEndpointUtf16](https://docs.xbox.com/zh-CN/reference/system/xuser/functions/xusergetdefaultaudioendpointutf16.md)

[XUserGetLocalId](https://docs.xbox.com/zh-CN/reference/system/xuser/functions/xusergetlocalid.md)


## Related topics

- [XUserGetLocalId](https://docs.xbox.com/zh-CN/reference/system/xuser/functions/xusergetlocalid.md)
- [XUserFindUserByLocalId](https://docs.xbox.com/zh-CN/reference/system/xuser/functions/xuserfinduserbylocalid.md)
- [APP_LOCAL_DEVICE_ID](https://docs.xbox.com/zh-CN/reference/system/xuser/structs/app_local_device_id.md)
- [XUserDeviceAssociationChange](https://docs.xbox.com/zh-CN/reference/system/xuser/structs/xuserdeviceassociationchange.md)
- [XUserChangeEventCallback](https://docs.xbox.com/zh-CN/reference/system/xuser/functions/xuserchangeeventcallback.md)
