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

# XGameStreamingGetSessionId

> XGameStreamingGetSessionId

This API returns a null-terminated opaque string that uniquely identifies the streaming session of the specified client.

## Syntax

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

### Parameters

*client*   \_In\_\
Type: XGameStreamingClientId

The streaming client that is being queried.

*sessionIdSize*   \_In\_\
Type: size\_t

The size of the `sessionId` buffer.

*sessionId*   \_Out\_writes\_bytes\_to\_(sessionIdSize, \*sessionIdUsed)\
Type: char\*

The buffer that will populated with the session id of the specified client.

*sessionIdUsed*   \_Out\_opt\_\
Type: size\_t\*

The size in bytes of the value returned in the `sessionId` buffer (including null terminator).

### Return value

Type: HRESULT

Returns **S\_OK** if successful; otherwise, returns an error code.

#### Potential Errors

| Error Code | Error Value | Reason for Error |
| - | - | - |
| E\_GAMESTREAMING\_NOT\_INITIALIZED | 0x89245400 | The XGameStreaming runtime has not been initialized. Call [XGameStreamingInitialize](https://docs.xbox.com/reference/system/xgamestreaming/functions/xgamestreaminginitialize.md) before calling other APIs. |
| E\_GAMESTREAMING\_CLIENT\_NOT\_CONNECTED | 0x89245401 | The specified client is not connected. |
| E\_GAMESTREAMING\_NO\_DATA | 0x89245402 | The requested data is not available. The data may be available later. |
| E\_NOT\_SUFFICIENT\_BUFFER | 0x7A | *sessionId* is not large enough to hold the session id and null terminating character. |

For a list of error codes, see [Error Codes](https://docs.xbox.com/reference/errorcodes.md).

## Remarks

The string written to `sessionId` will never be larger than `SessionIdMaxBytes`, thus it can be pre-allocated. The layout/contents of a `sessionId` may change at a later date, and thus should only be interpreted as an opaque string. As with other client property APIs, it's recommended to use `XGameStreamingRegisterClientPropertiesChanged` to register a callback for changes to the `sessionId`.

## Example

```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;
        }
    }
}

```

## Requirements

**Header:** xgamestreaming.h\
**Library:** xgameruntime.lib\
**Supported platforms:** Windows, XBOX One family consoles and XBOX Series consoles

## See also

[XGameStreaming](https://docs.xbox.com/reference/system/xgamestreaming/xgamestreaming_members.md)
[XGameStreamingRegisterClientPropertiesChanged](https://docs.xbox.com/reference/system/xgamestreaming/functions/xgamestreamingregisterclientpropertieschanged.md)


## Related topics

- [XGameStreamingClientProperty](https://docs.xbox.com/reference/system/xgamestreaming/enums/xgamestreamingclientproperty.md)
- [XGameStreaming (API contents)](https://docs.xbox.com/reference/system/xgamestreaming/xgamestreaming_members.md)
- [Using C# wrappers for GDK APIs in your Unity project](https://docs.xbox.com/build/gdk-and-engines/unity/unity-api-wrappers.md)
- [Development Best Practices for Xbox Game Streaming](https://docs.xbox.com/build/core-features/common/game-streaming/game-streaming-optimizing-your-game.md)
- [XGameStreamingGetClients](https://docs.xbox.com/reference/system/xgamestreaming/functions/xgamestreaminggetclients.md)
