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

# XStoreQueryEntitledProductsAsync

> XStoreQueryEntitledProductsAsync

# XStoreQueryEntitledProductsAsync

傳回使用者擁有權利之所有與目前遊戲相關產品的商店資訊。

## 語法

```cpp theme={null}
HRESULT XStoreQueryEntitledProductsAsync(  
         const XStoreContextHandle storeContextHandle,  
         XStoreProductKind productKinds,  
         uint32_t maxItemsToRetrievePerPage,  
         XAsyncBlock* async  
)  
```

### 參數

*storeContextHandle*   \_In\_\
類型：XStoreContextHandle

由 [XStoreCreateContext](/zh-TW/reference/system/xstore/functions/xstorecreatecontext) 傳回之使用者的商店內容控制代碼。

*productKinds*   \_In\_\
類型：[XStoreProductKind](/zh-TW/reference/system/xstore/enums/xstoreproductkind)

要查詢的產品種類。

*maxItemsToRetrievePerPage*   \_In\_\
類型：uint32\_t

每頁要擷取的項目數目上限。
每頁傳回的項目數目可介於 0 到 `maxItemsToRetrievePerPage` 之間。

*async*   \_Inout\_\
類型：[XAsyncBlock\*](/zh-TW/reference/system/xasync/structs/xasyncblock)

定義正在進行之非同步工作的 [XAsyncBlock](/zh-TW/reference/system/xasync/structs/xasyncblock)。
[XAsyncBlock](/zh-TW/reference/system/xasync/structs/xasyncblock) 可用來輪詢呼叫的狀態並擷取呼叫結果。
如需詳細資訊，請參閱 [XAsyncBlock](/zh-TW/reference/system/xasync/structs/xasyncblock)。

### 傳回值

類型：HRESULT

成功時傳回 S\_OK，否則傳回錯誤碼。

## 備註

呼叫 [XStoreQueryEntitledProductsResult](/zh-TW/reference/system/xstore/functions/xstorequeryentitledproductsresult) 以擷取商店產品資訊與此函式的執行結果。
請在 `XStoreQueryEntitledProductsAsync` 的回呼函式中執行 [XStoreQueryEntitledProductsResult](/zh-TW/reference/system/xstore/functions/xstorequeryentitledproductsresult)。
結果包含一個 `XStoreProductQueryHandle`，您可以呼叫 [XStoreEnumerateProductsQuery](/zh-TW/reference/system/xstore/functions/xstoreenumerateproductsquery) 來列舉它。

<Note>
  玩家擁有權利的項目越多，此 API 傳回所需的時間可能就越長。

  此 API 支援與 [XStoreQueryAssociatedProductsAsync](/zh-TW/reference/system/xstore/functions/xstorequeryassociatedproductsasync) 中所述相同的分頁模式。

  分頁在沙箱與 RETAIL 之間的行為可能不同。請驗證您的分頁實作，並務必在發行前於 RETAIL 中進行測試。

  如需詳細資訊，請參閱[分頁](/zh-TW/reference/system/xstore/functions/xstorequeryassociatedproductsasync#paging)。
</Note>

您無法事先得知此查詢會傳回的產品數目。

下列程式碼片段提供範例，示範如何查詢使用者對與目前遊戲相關之內容的權利。
結果會跨多個頁面處理。

```cpp theme={null}
struct QueryContext
{
    uint32_t count = 0;  // used to accumulate count of products returned across pages
    XStoreProductQueryHandle handle = nullptr;  // handle to reuse for paged operations
};

void CleanupProductQuery(XAsyncBlock* async)
{
    if (!async) return;

    auto* ctx = static_cast<QueryContext*>(async->context);
    if (ctx)
    {
        if (ctx->handle)
        {
            XStoreCloseProductsQueryHandle(ctx->handle);
            ctx->handle = nullptr;
        }

        delete ctx;
        async->context = nullptr;
    }

    delete async;
}

bool CALLBACK ProductEnumerationCallback(const XStoreProduct* product, void* context)
{
    // Use result of query
    auto* ctx = static_cast<QueryContext*>(context);

    // Empty results are possible and should be skipped
    if (product)
    {
        printf("%s %s %u\n", product->title, product->storeId, product->productKind);
        ctx->count++;
    }

    return true;
}

void CALLBACK QueryEntitledProductsCallback(XAsyncBlock* async)
{
    auto* ctx = static_cast<QueryContext*>(async->context);

    HRESULT hr = XStoreQueryEntitledProductsResult(async, &ctx->handle);
    if (FAILED(hr)) { CleanupProductQuery(async); return; }

    hr = XStoreEnumerateProductsQuery(ctx->handle, ctx, ProductEnumerationCallback);
    if (FAILED(hr)) { CleanupProductQuery(async); return; }

    if (XStoreProductsQueryHasMorePages(ctx->handle))
    {
        QueryNextPage(async);
        return;
    }

    printf("Enumeration complete, %u entitled products found\n", ctx->count);
    CleanupProductQuery(async);
}

void QueryEntitledProducts(XStoreContextHandle storeContextHandle, XTaskQueueHandle taskQueueHandle)
{
    auto async = new XAsyncBlock{};
    async->context = new QueryContext{ 0, nullptr };
    async->queue = taskQueueHandle; 
    async->callback = QueryEntitledProductsCallback; 
 
    XStoreProductKind typeFilter = 
        XStoreProductKind::Consumable | 
        XStoreProductKind::Durable | 
        XStoreProductKind::Game |
        XStoreProductKind::Pass |
        XStoreProductKind::UnmanagedConsumable;
 
    HRESULT hr = XStoreQueryEntitledProductsAsync( 
        storeContextHandle, 
        typeFilter, 
        25,           // Products per page (25 is a good default, can set lower to verify paging in sandbox)
        async);

    if (FAILED(hr))
    {
        printf("Failed to get entitled products: 0x%x\r\n", hr); 
        CleanupProductQuery(async);
    }
}

// In RETAIL, content will be returned across multiple pages.
// Paging is not complete until XStoreProductsQueryHasMorePages returns false.
void QueryNextPage(XAsyncBlock *async)
{
    async->callback = [](XAsyncBlock* async)
    {
        auto* ctx = static_cast<QueryContext*>(async->context);

        XStoreProductQueryHandle next = nullptr;
        HRESULT hr = XStoreProductsQueryNextPageResult(async, &next);
        if (FAILED(hr)) { CleanupProductQuery(async); return; }

        // Safe swap: avoids leaks if 'next' is a new handle, avoids double-close if identical
        if (next && next != ctx->handle)
        {
            if (ctx->handle) XStoreCloseProductsQueryHandle(ctx->handle);
            ctx->handle = next;
        }
        else
        {
            ctx->handle = next; // covers null or same-handle cases
        }

        hr = XStoreEnumerateProductsQuery(ctx->handle, ctx, ProductEnumerationCallback);
        if (FAILED(hr)) { CleanupProductQuery(async); return; }

        if (XStoreProductsQueryHasMorePages(ctx->handle))
        {
            QueryNextPage(async);
            return;
        }

        printf("Enumeration complete, %u entitled products found\n", ctx->count);
        CleanupProductQuery(async);
    };

    auto* ctx = static_cast<QueryContext*>(async->context);
    HRESULT hr = XStoreProductsQueryNextPageAsync(ctx->handle, async);
    if (FAILED(hr))
    {
        CleanupProductQuery(async);
    }
}

```

## 需求

**標頭：** XStore.h (包含在 XGameRuntime.h 中)

**程式庫：** xgameruntime.lib

**支援的平台：** Windows、XBOX One 系列主機和 XBOX Series 主機

## 概念文件

* [在遊戲中實作遊戲內購買](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/pc-dev/tutorials/pc-e2e-guide/e2e-services/e2e-in-game-purchases)
* [基本商店作業](/zh-TW/publishing/xstore-commerce/xstore-basic-operations)
* [如何使用不含套件的耐久性產品](/zh-TW/publishing/xstore-commerce/xstore-dwob)
* [授與玩家附加元件內容的存取權](/zh-TW/publishing/xstore-commerce/xstore-granting-access)
* [GDK 商務系統概觀](/zh-TW/publishing/xstore-commerce/xstore-overview)
* [Microsoft Store API 的細微速率限制](/zh-TW/publishing/xstore-commerce/xstore-fgrl)

## 另請參閱

[XStore](/zh-TW/reference/system/xstore/xstore_members)\
[XStoreQueryEntitledProductsResult](/zh-TW/reference/system/xstore/functions/xstorequeryentitledproductsresult)
[XStoreQueryAssociatedProductsAsync](/zh-TW/reference/system/xstore/functions/xstorequeryassociatedproductsasync)\
[XStoreQueryEntitledProductsAsync](/zh-TW/reference/system/xstore/functions/xstorequeryentitledproductsasync)\
[XStoreQueryProductsAsync](/zh-TW/reference/system/xstore/functions/xstorequeryproductsasync)\
[XStoreQueryProductForCurrentGameAsync](/zh-TW/reference/system/xstore/functions/xstorequeryproductforcurrentgameasync)\
[XStoreQueryProductForPackageAsync](/zh-TW/reference/system/xstore/functions/xstorequeryproductforpackageasync)\
[基本商店作業](/zh-TW/publishing/xstore-commerce/xstore-basic-operations)\
[授與玩家附加元件內容的存取權](/zh-TW/publishing/xstore-commerce/xstore-granting-access)


## Related topics

- [XStoreQueryEntitledProductsAsync](/reference/system/xstore/functions/xstorequeryentitledproductsasync.md)
- [XStoreQueryEntitledProductsResult](/reference/system/xstore/functions/xstorequeryentitledproductsresult.md)
- [XStoreQueryProductsAsync](/reference/system/xstore/functions/xstorequeryproductsasync.md)
- [XStoreQueryProductForPackageAsync](/reference/system/xstore/functions/xstorequeryproductforpackageasync.md)
- [XStoreQueryProductsResult](/reference/system/xstore/functions/xstorequeryproductsresult.md)
