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

为指定用户创建应用商店上下文。

## 语法

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

### 参数

*user*   \_In\_opt\_\
类型：XUserHandle

要为其创建应用商店上下文的用户。\
此参数仅对 PC 游戏是可选的：只需传递 nullptr 值即可。在 PC 上会自动使用登录到 Microsoft Store 的用户，并且忽略此参数。
主机游戏需要有效的 XUser。

*storeContextHandle*   \_Out\_\
类型：XStoreContextHandle\*

成功时包含所创建的应用商店上下文。

### 返回值

类型：HRESULT

HRESULT 成功或错误代码。

## 备注

**注意：** 在主机上，由此函数创建的 XStoreContextHandle 在挂起或快速恢复事件后会失效。若要安全地处理这些情况，我们建议每次游戏从挂起状态恢复时关闭 XStoreContextHandle 并重新创建它。

<Note>此函数在时间敏感线程上调用不安全。有关详细信息，请参阅[时间敏感线程](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)。</Note>

当你完成使用此函数创建的 **XStoreContextHandle** 后，必须使用 [XStoreCloseContextHandle](/reference/system/xstore/functions/xstoreclosecontexthandle) 关闭它。未能关闭句柄将导致内存泄漏。应用商店上下文用于在多个 **XStore** API 调用中标识要检索信息的用户。以下代码片段演示了如何创建应用商店上下文的示例。

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

```

## 要求

**头文件：** XStore.h（包含于 XGameRuntime.h 中）

**库：** xgameruntime.lib

**支持的平台：** Windows、XBOX One 系列主机和 XBOX Series 主机

## 概念文档

* [时间敏感线程](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)
* [基本应用商店操作](https://learn.microsoft.com/gaming/gdk/docs/store/commerce/fundamentals/xstore-basic-store-operations)

## 另请参阅

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


## Related topics

- [XStoreCloseContextHandle](/zh-CN/reference/system/xstore/functions/xstoreclosecontexthandle.md)
- [XStoreQueryGameLicenseAsync](/zh-CN/reference/system/xstore/functions/xstorequerygamelicenseasync.md)
- [XStoreQueryConsumableBalanceRemainingAsync](/zh-CN/reference/system/xstore/functions/xstorequeryconsumablebalanceremainingasync.md)
- [XStoreUnregisterGameLicenseChanged](/zh-CN/reference/system/xstore/functions/xstoreunregistergamelicensechanged.md)
- [XStoreShowRateAndReviewUIAsync](/zh-CN/reference/system/xstore/functions/xstoreshowrateandreviewuiasync.md)
