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

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](/reference/system/xstore/functions/xstoreregisterpackagelicenselost) 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.</Note>
To get the license for a Durable without a package use [XStoreAcquireLicenseForDurablesAsync](/reference/system/xstore/functions/xstoreacquirelicensefordurablesasync).

## 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](/reference/system/xstore/functions/xstorecreatecontext).

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

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

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

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

### 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](/reference/system/xstore/functions/xstoreacquirelicensefordurablesasync) instead). |

## Remarks

To retrieve the package license and the execution result of this function call [XStoreAcquireLicenseForPackageResult](/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult) after calling this function.
If you would like to check if a package can be licensed then call [XStoreCanAcquireLicenseForPackageAsync](/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync).
After acquiring a license, you must check if it's valid by calling [XStoreIsLicenseValid](/reference/system/xstore/functions/xstoreislicensevalid).
[XPackageMountWithUiAsync](/reference/system/xpackage/functions/xpackagemountwithuiasync) 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](/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

**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](/publishing/xstore-commerce/xstore-basic-operations)
* [Granting players access to add-on content](/publishing/xstore-commerce/xstore-granting-access)
* [Enabling licensing testing](/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)\
[Manage and license downloadable content (DLC)](/publishing/xstore-commerce/xstore-dlc)


## Related topics

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