> ## 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 會在暫停或 Quick Resume 事件之後失效。為了安全地處理這些情況，建議您在遊戲從暫停狀態繼續時，關閉 XStoreContextHandle 並重新建立。

<Note>在時間敏感執行緒上呼叫此函式並不安全。如需詳細資訊，請參閱[時間敏感執行緒](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)。</Note>

當您使用完此函式建立的 **XStoreContextHandle** 時，必須使用 [XStoreCloseContextHandle](/zh-TW/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)
* [基本市集作業](/zh-TW/publishing/xstore-commerce/xstore-basic-operations)

## 另請參閱

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


## Related topics

- [XStoreCreateContext](/zh-CN/reference/system/xstore/functions/xstorecreatecontext.md)
- [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)
