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

Acquires a license to an installed DLC package (Durable with a package product type) for the current game that the user is entitled to use.

[XStoreRegisterPackageLicenseLost](https://docs.xbox.com/reference/system/xstore/functions/xstoreregisterpackagelicenselost.md) can be used to monitor if the license obtained from this API is lost.

\[!NOTE] This API won't work with durable add-ons without a package.
To get the license for a Durable without a package use [XStoreAcquireLicenseForDurablesAsync](https://docs.xbox.com/reference/system/xstore/functions/xstoreacquirelicensefordurablesasync.md).

## Syntax

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

### Parameters

*storeContextHandle*   \_In\_\
Type: XStoreContextHandle

The store context handle for the user returned by [XStoreCreateContext](https://docs.xbox.com/reference/system/xstore/functions/xstorecreatecontext.md).

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

A string that uniquely identifies a store package. The package identifier is typically returned as part of an [XPackageEnumeratePackages](https://docs.xbox.com/reference/system/xpackage/functions/xpackageenumeratepackages.md) operation. For more information about package identifiers, see [Manage and license downloadable content (DLC)](https://docs.xbox.com/publishing/xstore-commerce/xstore-dlc.md).

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

An [XAsyncBlock](https://docs.xbox.com/reference/system/xasync/structs/xasyncblock.md) defining the asynchronous work being done. The [XAsyncBlock](https://docs.xbox.com/reference/system/xasync/structs/xasyncblock.md) can be used to poll for the call's status and retrieve call results. For more information, see [XAsyncBlock](https://docs.xbox.com/reference/system/xasync/structs/xasyncblock.md).

### Return value

Type: HRESULT

HRESULT success or error code.

| Error code | Description |
| - | - |
| 0x89245208 `E_GAMEPACKAGE_NO_PACKAGE_IDENTIFIER` | Package for this product isn't installed, or this API is being called on a Durable without a package type (use [XStoreAcquireLicenseForDurablesAsync](https://docs.xbox.com/reference/system/xstore/functions/xstoreacquirelicensefordurablesasync.md) instead). |

## Remarks

To retrieve the package license and the execution result of this function call [XStoreAcquireLicenseForPackageResult](https://docs.xbox.com/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult.md) after calling this function.
If you would like to check if a package can be licensed then call [XStoreCanAcquireLicenseForPackageAsync](https://docs.xbox.com/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync.md).
After acquiring a license, you must check if it's valid by calling [XStoreIsLicenseValid](https://docs.xbox.com/reference/system/xstore/functions/xstoreislicensevalid.md).
[XPackageMountWithUiAsync](https://docs.xbox.com/reference/system/xpackage/functions/xpackagemountwithuiasync.md) will automatically acquire a license when called.
If a license can't be acquired, the package fails to mount.

This code snippet shows an example of using the following APIs:

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

**Header:** XStore.h (included in XGameRuntime.h)

**Library:** xgameruntime.lib

**Supported platforms:** Windows, XBOX One family consoles, and XBOX Series consoles

## Conceptual documentation

* [Basic store operations](https://docs.xbox.com/publishing/xstore-commerce/xstore-basic-operations.md)
* [Granting players access to add-on content](https://docs.xbox.com/publishing/xstore-commerce/xstore-granting-access.md)
* [Enabling licensing testing](https://docs.xbox.com/publishing/xstore-commerce/xstore-licensing-setup.md)

## See also

[XStore](https://docs.xbox.com/reference/system/xstore/xstore_members.md)\
[XStoreAcquireLicenseForPackageAsync](https://docs.xbox.com/reference/system/xstore/functions/xstoreacquirelicenseforpackageasync.md)\
[XStoreAcquireLicenseForPackageResult](https://docs.xbox.com/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult.md)\
[XStoreIsLicenseValid](https://docs.xbox.com/reference/system/xstore/functions/xstoreislicensevalid.md)\
[XStoreCloseLicenseHandle](https://docs.xbox.com/reference/system/xstore/functions/xstorecloselicensehandle.md)\
[XStoreRegisterPackageLicenseLost](https://docs.xbox.com/reference/system/xstore/functions/xstoreregisterpackagelicenselost.md)\
[XStoreUnregisterPackageLicenseLost](https://docs.xbox.com/reference/system/xstore/functions/xstoreunregisterpackagelicenselost.md)\
[XStoreCanAcquireLicenseForPackageAsync](https://docs.xbox.com/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync.md)\
[XStoreCanAcquireLicenseForPackageResult](https://docs.xbox.com/reference/system/xstore/functions/xstorecanacquirelicenseforpackageresult.md)\
[Manage and license downloadable content (DLC)](https://docs.xbox.com/publishing/xstore-commerce/xstore-dlc.md)


## Related topics

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