> ## 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/zh-CN/reference/system/xstore/functions/xstoreregisterpackagelicenselost.md) 监视从此 API 获取的许可证是否丢失。

\[!NOTE] 此 API 不适用于不带包的耐用型加载项。
若要获取不带包的耐用型产品的许可证，请使用 [XStoreAcquireLicenseForDurablesAsync](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstoreacquirelicensefordurablesasync.md)。

## 语法

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

### 参数

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

由 [XStoreCreateContext](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstorecreatecontext.md) 返回的用户的商店上下文句柄。

*packageIdentifier*   \_In\_z\_\
类型：char\*

用于唯一标识商店包的字符串。包标识符通常作为 [XPackageEnumeratePackages](https://docs.xbox.com/zh-CN/reference/system/xpackage/functions/xpackageenumeratepackages.md) 操作的一部分返回。有关包标识符的详细信息，请参阅[管理和授权可下载内容 (DLC)](https://docs.xbox.com/zh-CN/publishing/xstore-commerce/xstore-dlc.md)。

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

定义正在执行的异步工作的 [XAsyncBlock](https://docs.xbox.com/zh-CN/reference/system/xasync/structs/xasyncblock.md)。可使用 [XAsyncBlock](https://docs.xbox.com/zh-CN/reference/system/xasync/structs/xasyncblock.md) 轮询调用状态并检索调用结果。有关详细信息，请参阅 [XAsyncBlock](https://docs.xbox.com/zh-CN/reference/system/xasync/structs/xasyncblock.md)。

### 返回值

类型：HRESULT

HRESULT 成功或错误代码。

| 错误代码 | 说明 |
| - | - |
| 0x89245208 `E_GAMEPACKAGE_NO_PACKAGE_IDENTIFIER` | 未安装此产品的包，或者对不带包的耐用型产品调用此 API（请改用 [XStoreAcquireLicenseForDurablesAsync](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstoreacquirelicensefordurablesasync.md)）。 |

## 备注

要检索包许可证以及此函数调用的执行结果，请在调用此函数后调用 [XStoreAcquireLicenseForPackageResult](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult.md)。
如果要检查包是否可以授权，请调用 [XStoreCanAcquireLicenseForPackageAsync](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync.md)。
获得许可证后，必须通过调用 [XStoreIsLicenseValid](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstoreislicensevalid.md) 检查其是否有效。
调用 [XPackageMountWithUiAsync](https://docs.xbox.com/zh-CN/reference/system/xpackage/functions/xpackagemountwithuiasync.md) 时将自动获取许可证。
如果无法获取许可证，则包装载失败。

此代码片段展示了使用以下 API 的示例：

* [XStoreAcquireLicenseForPackageAsync](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstoreacquirelicenseforpackageasync.md)
* [XStoreAcquireLicenseForPackageResult](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult.md)
* [XStoreIsLicenseValid](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstoreislicensevalid.md)
* [XStoreCloseLicenseHandle](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstorecloselicensehandle.md)
* [XStoreRegisterPackageLicenseLost](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstoreregisterpackagelicenselost.md)
* [XStoreUnregisterPackageLicenseLost](https://docs.xbox.com/zh-CN/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;
    }
}
```

## 要求

**头文件：** XStore.h（包含在 XGameRuntime.h 中）

**库：** xgameruntime.lib

**支持的平台：** Windows、XBOX One 系列主机和 XBOX Series 主机

## 概念文档

* [基本商店操作](https://docs.xbox.com/zh-CN/publishing/xstore-commerce/xstore-basic-operations.md)
* [授予玩家访问加载项内容的权限](https://docs.xbox.com/zh-CN/publishing/xstore-commerce/xstore-granting-access.md)
* [启用授权测试](https://docs.xbox.com/zh-CN/publishing/xstore-commerce/xstore-licensing-setup.md)

## 另请参阅

[XStore](https://docs.xbox.com/zh-CN/reference/system/xstore/xstore_members.md)\
[XStoreAcquireLicenseForPackageAsync](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstoreacquirelicenseforpackageasync.md)\
[XStoreAcquireLicenseForPackageResult](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult.md)\
[XStoreIsLicenseValid](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstoreislicensevalid.md)\
[XStoreCloseLicenseHandle](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstorecloselicensehandle.md)\
[XStoreRegisterPackageLicenseLost](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstoreregisterpackagelicenselost.md)\
[XStoreUnregisterPackageLicenseLost](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstoreunregisterpackagelicenselost.md)\
[XStoreCanAcquireLicenseForPackageAsync](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync.md)\
[XStoreCanAcquireLicenseForPackageResult](https://docs.xbox.com/zh-CN/reference/system/xstore/functions/xstorecanacquirelicenseforpackageresult.md)\
[管理和授权可下载内容 (DLC)](https://docs.xbox.com/zh-CN/publishing/xstore-commerce/xstore-dlc.md)


## Related topics

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