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

# XGameStreamingGetSessionId

> XGameStreamingGetSessionId

# XGameStreamingGetSessionId

此 API 會傳回以 null 結尾的不透明字串，用來唯一識別指定用戶端的串流工作階段。

## 語法

```cpp theme={null}
HRESULT XGameStreamingGetSessionId(
        XGameStreamingClientId client,
        size_t sessionIdSize,
        char* sessionId,
        size_t* sessionIdUsed
)
```

### 參數

*client*   \_In\_\
類型：XGameStreamingClientId

要查詢的串流用戶端。

*sessionIdSize*   \_In\_\
類型：size\_t

`sessionId` 緩衝區的大小。

*sessionId*   \_Out\_writes\_bytes\_to\_(sessionIdSize, \*sessionIdUsed)\
類型：char\*

將填入指定用戶端之工作階段識別碼的緩衝區。

*sessionIdUsed*   \_Out\_opt\_\
類型：size\_t\*

`sessionId` 緩衝區中所傳回值的大小 (以位元組為單位，包括 null 結束字元)。

### 傳回值

類型：HRESULT

如果成功，則傳回 **S\_OK**；否則傳回錯誤碼。

#### 可能的錯誤

| 錯誤碼 | 錯誤值 | 錯誤原因 |
| - | - | - |
| E\_GAMESTREAMING\_NOT\_INITIALIZED | 0x89245400 | XGameStreaming 執行階段尚未初始化。請先呼叫 [XGameStreamingInitialize](/zh-TW/reference/system/xgamestreaming/functions/xgamestreaminginitialize)，再呼叫其他 API。 |
| E\_GAMESTREAMING\_CLIENT\_NOT\_CONNECTED | 0x89245401 | 指定的用戶端未連線。 |
| E\_GAMESTREAMING\_NO\_DATA | 0x89245402 | 要求的資料無法使用。資料稍後可能會可供使用。 |
| E\_NOT\_SUFFICIENT\_BUFFER | 0x7A | *sessionId* 不夠大，無法容納工作階段識別碼和 null 結束字元。 |

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

## 備註

寫入 `sessionId` 的字串永遠不會大於 `SessionIdMaxBytes`，因此可以預先配置。`sessionId` 的配置/內容日後可能會變更，因此只應將其解讀為不透明字串。如同其他用戶端屬性 API，建議使用 `XGameStreamingRegisterClientPropertiesChanged` 註冊回呼，以接收 `sessionId` 的變更。

## 範例

```C++ theme={null}
// Register for client properties changed notifications 
void GameStreamingClientManager::OnClientConnected(XGameStreamingClientId client)
{
    XGameStreamingClientPropertiesChangedRegistrationToken token = {0};
    XGameStreamingRegisterClientPropertiesChanged(
            client, m_taskQueue, this, &OnClientPropertiesChanged, &token);
}

void GameStreamingClientManager::OnClientPropertiesChanged(
    void* context,
    XGameStreamingClientId client,
    uint32_t updatedPropertiesCount,
    XGameStreamingClientProperty* updatedProperties)
{
    for (uint32_t i = 0; i < updatedPropertiesCount; ++i)
    {
        switch (updatedProperties[i])
        {
        case XGameStreamingClientProperty::SessionId:
        {
            // allocate memory for sessionId
            char sessionId[SessionIdMaxBytes];

            // get session id of the client
            size_t bytesUsed = 0;
            HRESULT hr = XGameStreamingGetSessionId(client, _countof(sessionId), sessionId, &bytesUsed);

            if (SUCCEEDED(hr)) {
                // Game logic to use session id
            }
            else if (hr == E_GAMESTREAMING_NO_DATA) {
                // Game logic to handle no data for client
            }
            else
            {
                // Default error case
                LogFormat(L"XGameStreamingGetSessionId failed %x", hr);
            }

            break;
        }

        default:
            // A characteristic we are not tracking - do nothing
            break;
        }
    }
}

```

## 需求

**標頭：** xgamestreaming.h\
**程式庫：** xgameruntime.lib\
**支援的平台：** Windows、XBOX One 系列主機和 XBOX Series 主機

## 另請參閱

[XGameStreaming](/zh-TW/reference/system/xgamestreaming/xgamestreaming_members)
[XGameStreamingRegisterClientPropertiesChanged](/zh-TW/reference/system/xgamestreaming/functions/xgamestreamingregisterclientpropertieschanged)


## Related topics

- [XGameStreamingGetSessionId](/reference/system/xgamestreaming/functions/xgamestreaminggetsessionid.md)
- [XGameStreamingClientProperty](/reference/system/xgamestreaming/enums/xgamestreamingclientproperty.md)
- [XGameStreaming](/reference/system/xgamestreaming/xgamestreaming_members.md)
