> ## 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\_\
Type: XUserHandle

ストア コンテキストを作成するユーザー。\
このパラメーターは PC ゲームでのみ省略可能です。単に nullptr 値を渡してください。PC では、Microsoft Store にサインインしているユーザーが自動的に使用され、このパラメーターは無視されます。
コンソール ゲームでは、有効な XUser が必要です。

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

成功時に、作成されたストア コンテキストが格納されます。

### 戻り値

Type: 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](/ja-jp/reference/system/xstore/functions/xstoreclosecontexthandle.md)
- [XStoreQueryGameLicenseAsync](/ja-jp/reference/system/xstore/functions/xstorequerygamelicenseasync.md)
- [XStoreQueryConsumableBalanceRemainingAsync](/ja-jp/reference/system/xstore/functions/xstorequeryconsumablebalanceremainingasync.md)
- [XStoreUnregisterGameLicenseChanged](/ja-jp/reference/system/xstore/functions/xstoreunregistergamelicensechanged.md)
- [XStoreCanAcquireLicenseForStoreIdAsync](/ja-jp/reference/system/xstore/functions/xstorecanacquirelicenseforstoreidasync.md)
