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

Adquire uma licença para um pacote de DLC instalado (tipo de produto durável com pacote) para o jogo atual que o usuário tem direito de usar.

[XStoreRegisterPackageLicenseLost](/pt-BR/reference/system/xstore/functions/xstoreregisterpackagelicenselost) pode ser usado para monitorar se a licença obtida por esta API foi perdida.

<Note>Esta API não funciona com complementos duráveis sem pacote.</Note>
Para obter a licença de um durável sem pacote, use [XStoreAcquireLicenseForDurablesAsync](/pt-BR/reference/system/xstore/functions/xstoreacquirelicensefordurablesasync).

## Sintaxe

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

### Parâmetros

*storeContextHandle*   \_In\_\
Tipo: XStoreContextHandle

O identificador de contexto da loja para o usuário, retornado por [XStoreCreateContext](/pt-BR/reference/system/xstore/functions/xstorecreatecontext).

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

Uma cadeia de caracteres que identifica exclusivamente um pacote da loja. O identificador do pacote normalmente é retornado como parte de uma operação [XPackageEnumeratePackages](/pt-BR/reference/system/xpackage/functions/xpackageenumeratepackages). Para obter mais informações sobre identificadores de pacote, consulte [Gerenciar e licenciar conteúdo para download (DLC)](/pt-BR/publishing/xstore-commerce/xstore-dlc).

*async*   \_Inout\_\
Tipo: [XAsyncBlock\*](/pt-BR/reference/system/xasync/structs/xasyncblock)

Um [XAsyncBlock](/pt-BR/reference/system/xasync/structs/xasyncblock) que define o trabalho assíncrono que está sendo feito. O [XAsyncBlock](/pt-BR/reference/system/xasync/structs/xasyncblock) pode ser usado para sondar o status da chamada e recuperar os resultados da chamada. Para obter mais informações, consulte [XAsyncBlock](/pt-BR/reference/system/xasync/structs/xasyncblock).

### Valor retornado

Tipo: HRESULT

Código de êxito ou de erro HRESULT.

| Código de erro | Descrição |
| - | - |
| 0x89245208 `E_GAMEPACKAGE_NO_PACKAGE_IDENTIFIER` | O pacote deste produto não está instalado ou esta API está sendo chamada em um tipo durável sem pacote (use [XStoreAcquireLicenseForDurablesAsync](/pt-BR/reference/system/xstore/functions/xstoreacquirelicensefordurablesasync) em vez disso). |

## Comentários

Para recuperar a licença do pacote e o resultado da execução desta função, chame [XStoreAcquireLicenseForPackageResult](/pt-BR/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult) depois de chamar esta função.
Se quiser verificar se um pacote pode ser licenciado, chame [XStoreCanAcquireLicenseForPackageAsync](/pt-BR/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync).
Depois de adquirir uma licença, você deve verificar se ela é válida chamando [XStoreIsLicenseValid](/pt-BR/reference/system/xstore/functions/xstoreislicensevalid).
[XPackageMountWithUiAsync](/pt-BR/reference/system/xpackage/functions/xpackagemountwithuiasync) adquirirá automaticamente uma licença quando for chamada.
Se não for possível adquirir uma licença, a montagem do pacote falhará.

Este snippet de código mostra um exemplo de uso das seguintes APIs:

* [XStoreAcquireLicenseForPackageAsync](/pt-BR/reference/system/xstore/functions/xstoreacquirelicenseforpackageasync)
* [XStoreAcquireLicenseForPackageResult](/pt-BR/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult)
* [XStoreIsLicenseValid](/pt-BR/reference/system/xstore/functions/xstoreislicensevalid)
* [XStoreCloseLicenseHandle](/pt-BR/reference/system/xstore/functions/xstorecloselicensehandle)
* [XStoreRegisterPackageLicenseLost](/pt-BR/reference/system/xstore/functions/xstoreregisterpackagelicenselost)
* [XStoreUnregisterPackageLicenseLost](/pt-BR/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;
    }
}
```

## Requisitos

**Cabeçalho:** XStore.h (incluído em XGameRuntime.h)

**Biblioteca:** xgameruntime.lib

**Plataformas com suporte:** Windows, consoles da família XBOX One e consoles XBOX Series

## Documentação conceitual

* [Operações básicas da loja](/pt-BR/publishing/xstore-commerce/xstore-basic-operations)
* [Concedendo aos jogadores acesso a conteúdo complementar](/pt-BR/publishing/xstore-commerce/xstore-granting-access)
* [Habilitar testes de licenciamento](/pt-BR/publishing/xstore-commerce/xstore-licensing-setup)

## Confira também

[XStore](/pt-BR/reference/system/xstore/xstore_members)\
[XStoreAcquireLicenseForPackageAsync](/pt-BR/reference/system/xstore/functions/xstoreacquirelicenseforpackageasync)\
[XStoreAcquireLicenseForPackageResult](/pt-BR/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult)\
[XStoreIsLicenseValid](/pt-BR/reference/system/xstore/functions/xstoreislicensevalid)\
[XStoreCloseLicenseHandle](/pt-BR/reference/system/xstore/functions/xstorecloselicensehandle)\
[XStoreRegisterPackageLicenseLost](/pt-BR/reference/system/xstore/functions/xstoreregisterpackagelicenselost)\
[XStoreUnregisterPackageLicenseLost](/pt-BR/reference/system/xstore/functions/xstoreunregisterpackagelicenselost)\
[XStoreCanAcquireLicenseForPackageAsync](/pt-BR/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync)\
[XStoreCanAcquireLicenseForPackageResult](/pt-BR/reference/system/xstore/functions/xstorecanacquirelicenseforpackageresult)\
[Gerenciar e licenciar conteúdo para download (DLC)](/pt-BR/publishing/xstore-commerce/xstore-dlc)


## Related topics

- [XStoreAcquireLicenseForPackageAsync](/reference/system/xstore/functions/xstoreacquirelicenseforpackageasync.md)
- [XStoreAcquireLicenseForPackageResult](/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult.md)
- [XStoreCanAcquireLicenseForPackageAsync](/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync.md)
