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

擷取使用者的唯一遊戲工作階段識別碼。

## 語法

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

### 參數

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

要擷取其本機識別碼之使用者的控制代碼。

*userLocalId*   \_Out\_\
類型：[XUserLocalId\*](/zh-TW/reference/system/xuser/structs/xuserlocalid)

包含使用者的唯一遊戲工作階段識別碼。

### 傳回值

類型：HRESULT

HRESULT 成功或錯誤碼。\
如需錯誤碼清單，請參閱[錯誤碼](/zh-TW/reference/errorcodes)。

## 備註

若要擷取特定使用者的使用者識別碼，請呼叫 [XUserGetId](/zh-TW/reference/system/xuser/functions/xusergetid)。

若要擷取特定使用者的狀態，請呼叫 [XUserGetState](/zh-TW/reference/system/xuser/functions/xusergetstate)。

若要擷取特定使用者的年齡層，請呼叫 [XUserGetAgeGroup](/zh-TW/reference/system/xuser/functions/xusergetagegroup)。

**XUserLocalId** 可在遊戲工作階段內唯一識別使用者。如果您複製 **XUserHandle**，兩個控制代碼都會保有相同的 **XUserLocalId**。遊戲工作階段結束後，同一個使用者在下一個工作階段中可能會有不同的 **XUserLocalId**。請勿使用這些識別碼跨遊戲工作階段追蹤使用者。

下列範例示範如何使用 **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](/zh-TW/reference/system/xuser/xuser_members)

[XUserGetId](/zh-TW/reference/system/xuser/functions/xusergetid)

[XUserGetState](/zh-TW/reference/system/xuser/functions/xusergetstate)

[XUserGetAgeGroup](/zh-TW/reference/system/xuser/functions/xusergetagegroup)


## Related topics

- [XUserGetLocalId](/reference/system/xuser/functions/xusergetlocalid.md)
- [XUserLocalId](/reference/system/xuser/structs/xuserlocalid.md)
