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

将填充指定客户端会话 ID 的缓冲区。

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

在 `sessionId` 缓冲区中返回的值的大小（以字节为单位，包括 null 终止符）。

### 返回值

类型：HRESULT

如果成功，则返回 **S\_OK**；否则返回错误代码。

#### 潜在错误

| 错误代码                                     | 错误值        | 错误原因                                                                                                                                       |
| ---------------------------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| E\_GAMESTREAMING\_NOT\_INITIALIZED       | 0x89245400 | XGameStreaming 运行时尚未初始化。在调用其他 API 之前，请先调用 [XGameStreamingInitialize](/reference/system/xgamestreaming/functions/xgamestreaminginitialize)。 |
| E\_GAMESTREAMING\_CLIENT\_NOT\_CONNECTED | 0x89245401 | 指定的客户端未连接。                                                                                                                                 |
| E\_GAMESTREAMING\_NO\_DATA               | 0x89245402 | 请求的数据不可用。数据可能稍后可用。                                                                                                                         |
| E\_NOT\_SUFFICIENT\_BUFFER               | 0x7A       | *sessionId* 不够大，无法容纳会话 ID 和 null 终止符字符。                                                                                                    |

有关错误代码列表，请参阅[错误代码](/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](/reference/system/xgamestreaming/xgamestreaming_members)
[XGameStreamingRegisterClientPropertiesChanged](/reference/system/xgamestreaming/functions/xgamestreamingregisterclientpropertieschanged)


## Related topics

- [XGameStreamingClientProperty](/zh-CN/reference/system/xgamestreaming/enums/xgamestreamingclientproperty.md)
- [XGameStreaming](/zh-CN/reference/system/xgamestreaming/xgamestreaming_members.md)
- [面向 GDK 的 Unity C# API 包装器](/zh-CN/build/gdk-and-engines/unity/unity-api-wrappers.md)
- [XGameStreamingGetClients](/zh-CN/reference/system/xgamestreaming/functions/xgamestreaminggetclients.md)
- [XGameStreamingGetConnectionState](/zh-CN/reference/system/xgamestreaming/functions/xgamestreaminggetconnectionstate.md)
