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

# XStoreCreateContext

> XStoreCreateContext

# XStoreCreateContext

Creates a store context for the specified user.

## Syntax

```cpp theme={null}
HRESULT XStoreCreateContext(  
         const XUserHandle user,  
         XStoreContextHandle* storeContextHandle  
)  
```

### Parameters

*user*   \_In\_opt\_\
Type: XUserHandle

The user to create the store context for.\
This parameter is optional for PC games only: simply pass a nullptr value. On PC the user signed into the Microsoft Store is used automatically and this parameter is ignored.
A valid XUser is required for console games.

*storeContextHandle*   \_Out\_\
Type: XStoreContextHandle\*

On success contains the created store context.

### Return value

Type: HRESULT

HRESULT success or error code.

## Remarks

**NOTE:** On consoles, the XStoreContextHandle created by this function is invalidated after a Suspend or Quick Resume event. To safely handle these conditions we recommend closing a XStoreContextHandle and recreating it whenever the game resumes from a suspended state.

<Note>This function isn't safe to call on a time-sensitive thread. For more information, see [Time-sensitive threads](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads).</Note>

When you are finished with the **XStoreContextHandle** created by this function, you must close it with [XStoreCloseContextHandle](/reference/system/xstore/functions/xstoreclosecontexthandle). Failure to close the handle will cause a memory leak. The store context is used to identify the user to retrieve information for in a number of the **XStore** API calls. The following code snippet shows an example of creating a store context.

```cpp theme={null}
void CALLBACK GameLicenseChangedCallback(void* context)
{
    UNREFERENCED_PARAMETER(context);
    printf("**** License Changed ****\r\n");
}

void CreateStoreContextHandle(XUserHandle userHandle, XTaskQueueHandle taskQueueHandle)
{
    // As a rule, it would be a good idea to create one of these
    // and keep it alive through the lifetime of the game
    XStoreContextHandle storeContextHandle;

    HRESULT hr = XStoreCreateContext(userHandle, &storeContextHandle);
    if (FAILED(hr))
    {
        printf("Failed creating store context: 0x%x\r\n", hr);
        return;
    }

    XTaskQueueRegistrationToken gameLicenseChangedToken = { 0 };
    hr = XStoreRegisterGameLicenseChanged(
        storeContextHandle,
        taskQueueHandle,
        storeContextHandle,
        GameLicenseChangedCallback,
        &gameLicenseChangedToken);

    if (FAILED(hr))
    {
        printf("Failed register for game license changed callback: 0x%x\r\n", hr);
        XStoreCloseContextHandle(storeContextHandle);
        return;
    }

    //Unregistering the license changed event and closing the XStoreContextHandle would usually go in the cleanup/shutdown sections of your game code. 
    XStoreUnregisterGameLicenseChanged(
        storeContextHandle,
        gameLicenseChangedToken,
        true);

    XStoreCloseContextHandle(storeContextHandle);
}

```

## Requirements

**Header:** XStore.h (included in XGameRuntime.h)

**Library:** xgameruntime.lib

**Supported platforms:** Windows, XBOX One family consoles and XBOX Series consoles

## Conceptual documentation

* [Time-sensitive threads](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)
* [Basic store operations](/publishing/xstore-commerce/xstore-basic-operations)

## See also

[XStore](/reference/system/xstore/xstore_members)\
[XStoreCloseContextHandle](/reference/system/xstore/functions/xstoreclosecontexthandle)\
[XStoreRegisterGameLicenseChanged](/reference/system/xstore/functions/xstoreregistergamelicensechanged)


## Related topics

- [XStoreCloseContextHandle](/reference/system/xstore/functions/xstoreclosecontexthandle.md)
- [Basic store operations](/publishing/xstore-commerce/xstore-basic-operations.md)
- [XStoreQueryGameLicenseAsync](/reference/system/xstore/functions/xstorequerygamelicenseasync.md)
- [XStoreQueryConsumableBalanceRemainingAsync](/reference/system/xstore/functions/xstorequeryconsumablebalanceremainingasync.md)
- [XStoreUnregisterGameLicenseChanged](/reference/system/xstore/functions/xstoreunregistergamelicensechanged.md)
