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

# XStoreQueryAssociatedProductsForStoreIdAsync

> XStoreQueryAssociatedProductsForStoreIdAsync

# XStoreQueryAssociatedProductsForStoreIdAsync

ストア ID で指定されたターゲット製品に関連する、購入可能な製品のストア登録情報を取得します。

## 構文

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

### パラメーター

*storeContextHandle*   \_In\_\
Type: XStoreContextHandle

[XStoreCreateContext](/ja-jp/reference/system/xstore/functions/xstorecreatecontext) から返される、ユーザーのストア コンテキスト ハンドル。

*storeId*   \_In\_\
Type: const char\*

関連製品を取得する対象の製品のストア ID。

*productKinds*   \_In\_\
Type: [XStoreProductKind](/ja-jp/reference/system/xstore/enums/xstoreproductkind)

返す製品の種類。

*maxItemsToRetrievePerPage*   \_In\_\
Type: uint32\_t

1 ページあたりに取得する項目の最大数。
1 ページあたりに返される項目の数は、0 から `maxItemsToRetrievePerPage` まで異なる場合があります。

*async*   \_Inout\_\
Type: [XAsyncBlock\*](/ja-jp/reference/system/xasync/structs/xasyncblock)

実行中の非同期処理を定義する [XAsyncBlock](/ja-jp/reference/system/xasync/structs/xasyncblock)。
[XAsyncBlock](/ja-jp/reference/system/xasync/structs/xasyncblock) を使用して、呼び出しの状態のポーリングと呼び出し結果の取得を行います。
詳細については、[XAsyncBlock](/ja-jp/reference/system/xasync/structs/xasyncblock) を参照してください。

### 戻り値

Type: HRESULT

HRESULT の成功またはエラー コード。

## 解説

この関数のストア登録情報と実行結果を取得するには、この関数を呼び出した後に [XStoreQueryAssociatedProductsForStoreIdResult](/ja-jp/reference/system/xstore/functions/xstorequeryassociatedproductsforstoreidresult) を呼び出します。
[XStoreQueryAssociatedProductsForStoreIdResult](/ja-jp/reference/system/xstore/functions/xstorequeryassociatedproductsforstoreidresult) には、[XStoreEnumerateProductsQuery](/ja-jp/reference/system/xstore/functions/xstoreenumerateproductsquery) を呼び出して列挙する `XStoreProductQueryHandle` が含まれます。

> [!NOTE]
>
> この API は [XStoreQueryAssociatedProductsAsync](/ja-jp/reference/system/xstore/functions/xstorequeryassociatedproductsasync) で説明されているのと同じページング パターンをサポートします。
>
> ページングは、サンドボックスと RETAIL の間で動作が異なる場合があります。
> ページング実装を検証し、リリース前に必ず RETAIL でテストしてください。
>
> 詳細については、[ページング](/ja-jp/reference/system/xstore/functions/xstorequeryassociatedproductsasync#paging)を参照してください。

このクエリが返す製品の数を事前に知ることはできません。

この API を使用して、ストア ID を使用してターゲット製品に関連付けられたアドオンを検索します。
この機能は、特にフランチャイズ ゲーム ハブに便利です。フランチャイズ ゲーム ハブでは複数のゲームのアドオンを列挙できますが、これらのアドオンがどのゲームに関連付けられているかを判別できません。

使用シナリオの例:

* 開発者は `XPackageEnumeratePackages` を実行してインストール済みの [XPackageKind::Game](/ja-jp/reference/system/xpackage/enums/xpackagekind) 製品を列挙し、その中の 1 つのゲームに対して、`XPackageDetails` から取得した storeId を使用して `XStoreQueryAssociatedProductsForStoreIdAsync` を実行します。開発者は [XPackageKind::Content](/ja-jp/reference/system/xpackage/enums/xpackagekind) 用に `XPackageEnumeratePackages` を実行してインストール済み DLC を列挙します。コンテンツ列挙と製品クエリの結果を相互参照することで、開発者は特定のゲームにどの DLC がインストールされているかを識別できるようになります。

* 開発者は `XStoreQueryAssociatedProductsAsync` を実行して現在のゲームに関連付けられている購入可能な製品のリストを取得します。次に、ターゲット ゲームの `XStoreProduct` の storeId を使用して `XStoreQueryAssociatedProductsForStoreIdAsync` を実行し、関連付けられている DLC を調べます。

次のコード スニペットは、ストア ID を使用してターゲット製品に関連付けられた製品をクエリする例を示しています。
結果は複数のページにわたって処理されます。

```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 QueryAssociatedProductsForStoreIdCallback(XAsyncBlock* async)
{
    auto* ctx = static_cast<QueryContext*>(async->context);

    HRESULT hr = XStoreQueryAssociatedProductsForStoreIdResult(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 products found\n", ctx->count);
    CleanupProductQuery(async);
}

void QueryAssociatedProductsForStoreId(XStoreContextHandle storeContextHandle, XTaskQueueHandle taskQueueHandle, const char* storeId)
{
    auto async = new XAsyncBlock{};
    async->context = new QueryContext{ 0, nullptr };
    async->queue = taskQueueHandle; 
    async->callback = QueryAssociatedProductsForStoreIdCallback; 
 
    XStoreProductKind typeFilter = 
        XStoreProductKind::Consumable | 
        XStoreProductKind::Durable | 
        XStoreProductKind::Game |
        XStoreProductKind::Pass |
        XStoreProductKind::UnmanagedConsumable;
 
    HRESULT hr = XStoreQueryAssociatedProductsForStoreIdAsync( 
        storeContextHandle, 
        storeId, 
        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 associated products for %s: 0x%x\r\n", storeId, 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 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 本体

## 関連概念ドキュメント

* [ストアの基本操作](/ja-jp/publishing/xstore-commerce/xstore-basic-operations)

## 関連項目

[XStore](/ja-jp/reference/system/xstore/xstore_members)\
[XStoreQueryAssociatedProductsForStoreIdResult](/ja-jp/reference/system/xstore/functions/xstorequeryassociatedproductsforstoreidresult)\
[XStoreQueryAssociatedProductsAsync](/ja-jp/reference/system/xstore/functions/xstorequeryassociatedproductsasync)\
[XStoreEnumerateProductsQuery](/ja-jp/reference/system/xstore/functions/xstoreenumerateproductsquery)\
[XStoreProductsQueryHasMorePages](/ja-jp/reference/system/xstore/functions/xstoreproductsqueryhasmorepages)\
[XStoreProductsQueryNextPageAsync](/ja-jp/reference/system/xstore/functions/xstoreproductsquerynextpageasync)


## Related topics

- [XStoreQueryAssociatedProductsForStoreIdResult](/ja-jp/reference/system/xstore/functions/xstorequeryassociatedproductsforstoreidresult.md)
- [基本的な Store 操作](/ja-jp/publishing/xstore-commerce/xstore-basic-operations.md)
- [XStore (API の内容)](/ja-jp/reference/system/xstore/xstore_members.md)
- [XStoreQueryAssociatedProductsAsync](/ja-jp/reference/system/xstore/functions/xstorequeryassociatedproductsasync.md)
- [XStoreQueryAssociatedProductsResult](/ja-jp/reference/system/xstore/functions/xstorequeryassociatedproductsresult.md)
