> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XStoreAcquireLicenseForPackageAsync

> XStoreAcquireLicenseForPackageAsync

# XStoreAcquireLicenseForPackageAsync

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

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

\[!NOTE] この API は、パッケージなしの永続型アドオンでは動作しません。
パッケージなしの永続型のライセンスを取得するには、[XStoreAcquireLicenseForDurablesAsync](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstoreacquirelicensefordurablesasync.md) を使用します。

## Syntax

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

### Parameters

*storeContextHandle*   \_In\_\
型: XStoreContextHandle

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

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

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

*async*   \_Inout\_\
型: [XAsyncBlock\*](https://docs.xbox.com/ja-jp/reference/system/xasync/structs/xasyncblock.md)

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

### Return value

型: HRESULT

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

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

## Remarks

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

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

* [XStoreAcquireLicenseForPackageAsync](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstoreacquirelicenseforpackageasync.md)
* [XStoreAcquireLicenseForPackageResult](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult.md)
* [XStoreIsLicenseValid](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstoreislicensevalid.md)
* [XStoreCloseLicenseHandle](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstorecloselicensehandle.md)
* [XStoreRegisterPackageLicenseLost](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstoreregisterpackagelicenselost.md)
* [XStoreUnregisterPackageLicenseLost](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstoreunregisterpackagelicenselost.md)

```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://docs.xbox.com/ja-jp/publishing/xstore-commerce/xstore-basic-operations.md)
* [プレイヤーへのアドオン コンテンツへのアクセス許可の付与](https://docs.xbox.com/ja-jp/publishing/xstore-commerce/xstore-granting-access.md)
* [ライセンス テストの有効化](https://docs.xbox.com/ja-jp/publishing/xstore-commerce/xstore-licensing-setup.md)

## See also

[XStore](https://docs.xbox.com/ja-jp/reference/system/xstore/xstore_members.md)\
[XStoreAcquireLicenseForPackageAsync](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstoreacquirelicenseforpackageasync.md)\
[XStoreAcquireLicenseForPackageResult](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult.md)\
[XStoreIsLicenseValid](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstoreislicensevalid.md)\
[XStoreCloseLicenseHandle](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstorecloselicensehandle.md)\
[XStoreRegisterPackageLicenseLost](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstoreregisterpackagelicenselost.md)\
[XStoreUnregisterPackageLicenseLost](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstoreunregisterpackagelicenselost.md)\
[XStoreCanAcquireLicenseForPackageAsync](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync.md)\
[XStoreCanAcquireLicenseForPackageResult](https://docs.xbox.com/ja-jp/reference/system/xstore/functions/xstorecanacquirelicenseforpackageresult.md)\
[ダウンロード可能なコンテンツ (DLC) の管理およびライセンス](https://docs.xbox.com/ja-jp/publishing/xstore-commerce/xstore-dlc.md)


## Related topics

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