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

# 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](/zh-TW/reference/system/xuser/functions/xuserchangeeventcallback)：使用者變更事件的回呼函式。
* [XUserDefaultAudioEndpointUtf16ChangedCallback](/zh-TW/reference/system/xuser/functions/xuserdefaultaudioendpointutf16changedcallback)：當使用者的預設音訊端點變更時叫用的回呼。
* [XUserFindUserByLocalId](/zh-TW/reference/system/xuser/functions/xuserfinduserbylocalid)：使用特定本機 ID 擷取使用者控制代碼的函式。
* [XUserGetDefaultAudioEndpointUtf16](/zh-TW/reference/system/xuser/functions/xusergetdefaultaudioendpointutf16)：擷取特定使用者之預設音訊端點的函式。
* [XUserGetLocalId](/zh-TW/reference/system/xuser/functions/xusergetlocalid)：擷取使用者唯一遊戲工作階段 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](/zh-TW/reference/system/xuser/xuser_members)

[XUserChangeEventCallback](/zh-TW/reference/system/xuser/functions/xuserchangeeventcallback)

[XUserDefaultAudioEndpointUtf16ChangedCallback](/zh-TW/reference/system/xuser/functions/xuserdefaultaudioendpointutf16changedcallback)

[XUserFindUserByLocalId](/zh-TW/reference/system/xuser/functions/xuserfinduserbylocalid)

[XUserGetDefaultAudioEndpointUtf16](/zh-TW/reference/system/xuser/functions/xusergetdefaultaudioendpointutf16)

[XUserGetLocalId](/zh-TW/reference/system/xuser/functions/xusergetlocalid)


## Related topics

- [XUserGetLocalId](/zh-TW/reference/system/xuser/functions/xusergetlocalid.md)
- [APP_LOCAL_DEVICE_ID](/zh-TW/reference/system/xuser/structs/app_local_device_id.md)
- [XUserFindUserByLocalId](/zh-TW/reference/system/xuser/functions/xuserfinduserbylocalid.md)
- [XUserDeviceAssociationChange](/zh-TW/reference/system/xuser/structs/xuserdeviceassociationchange.md)
- [XUserChangeEventCallback](/zh-TW/reference/system/xuser/functions/xuserchangeeventcallback.md)
