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

# XStoreAcquireLicenseForPackageAsync

> XStoreAcquireLicenseForPackageAsync

# XStoreAcquireLicenseForPackageAsync

ユーザーが使用する権限を持っている、現在のゲームのインストール済み DLC パッケージ (パッケージ付きの耐久型製品) のライセンスを取得します。

[XStoreRegisterPackageLicenseLost](/reference/system/xstore/functions/xstoreregisterpackagelicenselost) を使用して、この API から取得したライセンスが失われたかどうかを監視できます。

<Note>この API は、パッケージのない耐久型アドオンでは動作しません。</Note>
パッケージのない耐久型のライセンスを取得するには、[XStoreAcquireLicenseForDurablesAsync](/reference/system/xstore/functions/xstoreacquirelicensefordurablesasync) を使用します。

## Syntax

```cpp theme={null}
HRESULT XStoreAcquireLicenseForPackageAsync(  
         const XStoreContextHandle storeContextHandle,  
         const char* packageIdentifier,  
         XAsyncBlock* async  
)  
```

### Parameters

*storeContextHandle*   \_In\_\
型: XStoreContextHandle

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

*packageIdentifier*   \_In\_z\_\
型: char\*

ストア パッケージを一意に識別する文字列。パッケージ識別子は通常、[XPackageEnumeratePackages](/reference/system/xpackage/functions/xpackageenumeratepackages) 操作の一部として返されます。パッケージ識別子の詳細については、[ダウンロード可能なコンテンツ (DLC) の管理およびライセンス](https://learn.microsoft.com/gaming/gdk/docs/store/commerce/fundamentals/xstore-manage-and-license-optional-packages) を参照してください。

*async*   \_Inout\_\
型: [XAsyncBlock\*](/reference/system/xasync/structs/xasyncblock)

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

### Return value

型: HRESULT

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

| エラー コード                                          | 説明                                                                                                                                                                                       |
| ------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 0x89245208 `E_GAMEPACKAGE_NO_PACKAGE_IDENTIFIER` | この製品のパッケージがインストールされていないか、この API がパッケージのない耐久型で呼び出されています (代わりに [XStoreAcquireLicenseForDurablesAsync](/reference/system/xstore/functions/xstoreacquirelicensefordurablesasync) を使用してください)。 |

## Remarks

パッケージのライセンスとこの関数の実行結果を取得するには、この関数を呼び出した後に [XStoreAcquireLicenseForPackageResult](/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult) を呼び出します。
パッケージにライセンスを付与できるかどうかを確認するには、[XStoreCanAcquireLicenseForPackageAsync](/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync) を呼び出します。
ライセンスを取得した後、[XStoreIsLicenseValid](/reference/system/xstore/functions/xstoreislicensevalid) を呼び出してライセンスが有効かどうかを確認する必要があります。
[XPackageMountWithUiAsync](/reference/system/xpackage/functions/xpackagemountwithuiasync) は呼び出されると自動的にライセンスを取得します。
ライセンスを取得できない場合、パッケージのマウントは失敗します。

このコード スニペットは、次の API の使用例を示しています。

* [XStoreAcquireLicenseForPackageAsync](/reference/system/xstore/functions/xstoreacquirelicenseforpackageasync)
* [XStoreAcquireLicenseForPackageResult](/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult)
* [XStoreIsLicenseValid](/reference/system/xstore/functions/xstoreislicensevalid)
* [XStoreCloseLicenseHandle](/reference/system/xstore/functions/xstorecloselicensehandle)
* [XStoreRegisterPackageLicenseLost](/reference/system/xstore/functions/xstoreregisterpackagelicenselost)
* [XStoreUnregisterPackageLicenseLost](/reference/system/xstore/functions/xstoreunregisterpackagelicenselost)

```cpp theme={null}
void CALLBACK PackageLicenseLostCallback(void* context)
{
    printf("**** License Lost ****\r\n");
}

void CALLBACK AcquireLicenseForPackageCallback(XAsyncBlock* asyncBlock)
{
    XStoreLicenseHandle licenseHandle = nullptr;

    HRESULT hr = XStoreAcquireLicenseForPackageResult(
        asyncBlock,
        &licenseHandle);

    if (FAILED(hr))
    {
        printf("Failed retrieve the license handle: 0x%x\r\n", hr);
        return;
    }

    bool isValid = XStoreIsLicenseValid(licenseHandle);
    printf("isValid: %s\r\n", isValid ? "true" : "false");

    if (isValid)
    {
        XTaskQueueHandle taskQueueHandle = reinterpret_cast<XTaskQueueHandle>(asyncBlock->context);
        XTaskQueueRegistrationToken token = { 0 };

        //  Todo: Save the licenseHandle to hold onto it for the life of the app or until you unload the package.
        // This allows us to be notified if the license becomes invalid and we need to handle unloading the content.
        hr = XStoreRegisterPackageLicenseLost(
            licenseHandle,
            taskQueueHandle,
            nullptr,
            PackageLicenseLostCallback,
            &token);

        if (FAILED(hr))
        {
            XStoreCloseLicenseHandle(licenseHandle);
            printf("Failed register license lost callback: 0x%x\r\n", hr);
            return;
        }

        // This would normally go in your cleanup code when releasing the license
        hr = XStoreUnregisterPackageLicenseLost(
            licenseHandle,
            token,
            true);

        if (FAILED(hr))
        {

            // This would normally go in your cleanup code when releasing the license
            XStoreCloseLicenseHandle(licenseHandle);
            printf("Failed unregister license lost callback: 0x%x\r\n", hr);
            return;
        }
    }

    XStoreCloseLicenseHandle(licenseHandle);
}

void AcquireLicenseForPackage(XStoreContextHandle storeContextHandle, XTaskQueueHandle taskQueueHandle, const char* packageIdentifier)
{
    auto asyncBlock = std::make_unique<XAsyncBlock>();
    ZeroMemory(asyncBlock.get(), sizeof(*asyncBlock));
    asyncBlock->queue = taskQueueHandle;
    asyncBlock->context = taskQueueHandle;
    asyncBlock->callback = AcquireLicenseForPackageCallback;

    HRESULT hr = XStoreAcquireLicenseForPackageAsync(
        storeContextHandle,
        packageIdentifier,
        asyncBlock.get());

    if (FAILED(hr))
    {
        printf("Failed to get product for package: 0x%x\r\n", hr);
        return;
    }
}
```

## Requirements

**ヘッダー:** XStore.h (XGameRuntime.h に含まれる)

**ライブラリ:** xgameruntime.lib

**サポートされているプラットフォーム:** Windows、XBOX One ファミリー本体および XBOX Series 本体

## Conceptual documentation

* [基本的なストア操作](https://learn.microsoft.com/gaming/gdk/docs/store/commerce/fundamentals/xstore-basic-store-operations)
* [プレイヤーへのアドオン コンテンツへのアクセス許可の付与](https://learn.microsoft.com/gaming/gdk/docs/store/commerce/fundamentals/xstore-granting-access-to-content)
* [ライセンス テストの有効化](/publishing/xstore-commerce/xstore-licensing-setup)

## See also

[XStore](/reference/system/xstore/xstore_members)\
[XStoreAcquireLicenseForPackageAsync](/reference/system/xstore/functions/xstoreacquirelicenseforpackageasync)\
[XStoreAcquireLicenseForPackageResult](/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult)\
[XStoreIsLicenseValid](/reference/system/xstore/functions/xstoreislicensevalid)\
[XStoreCloseLicenseHandle](/reference/system/xstore/functions/xstorecloselicensehandle)\
[XStoreRegisterPackageLicenseLost](/reference/system/xstore/functions/xstoreregisterpackagelicenselost)\
[XStoreUnregisterPackageLicenseLost](/reference/system/xstore/functions/xstoreunregisterpackagelicenselost)\
[XStoreCanAcquireLicenseForPackageAsync](/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync)\
[XStoreCanAcquireLicenseForPackageResult](/reference/system/xstore/functions/xstorecanacquirelicenseforpackageresult)\
[ダウンロード可能なコンテンツ (DLC) の管理およびライセンス](https://learn.microsoft.com/gaming/gdk/docs/store/commerce/fundamentals/xstore-manage-and-license-optional-packages)


## Related topics

- [XStoreAcquireLicenseForPackageResult](/ja-jp/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult.md)
- [XStoreCanAcquireLicenseForPackageAsync](/ja-jp/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync.md)
- [XStoreCloseLicenseHandle](/ja-jp/reference/system/xstore/functions/xstorecloselicensehandle.md)
- [XStoreRegisterPackageLicenseLost](/ja-jp/reference/system/xstore/functions/xstoreregisterpackagelicenselost.md)
- [XStoreUnregisterPackageLicenseLost](/ja-jp/reference/system/xstore/functions/xstoreunregisterpackagelicenselost.md)
