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

為使用者有權使用的目前遊戲，取得已安裝 DLC 套件 (含套件的耐久性產品類型) 的授權。

[XStoreRegisterPackageLicenseLost](/zh-TW/reference/system/xstore/functions/xstoreregisterpackagelicenselost) 可用來監視從此 API 取得的授權是否遺失。

<Note>此 API 不適用於不含套件的耐久性附加元件。</Note>
若要取得不含套件之耐久性產品的授權，請使用 [XStoreAcquireLicenseForDurablesAsync](/zh-TW/reference/system/xstore/functions/xstoreacquirelicensefordurablesasync)。

## 語法

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

### 參數

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

使用者的市集內容控制代碼，由 [XStoreCreateContext](/zh-TW/reference/system/xstore/functions/xstorecreatecontext) 傳回。

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

唯一識別市集套件的字串。套件識別碼通常會在 [XPackageEnumeratePackages](/zh-TW/reference/system/xpackage/functions/xpackageenumeratepackages) 作業中傳回。如需套件識別碼的詳細資訊，請參閱[管理及授權可下載內容 (DLC)](/zh-TW/publishing/xstore-commerce/xstore-dlc)。

*async*   \_Inout\_\
類型：[XAsyncBlock\*](/zh-TW/reference/system/xasync/structs/xasyncblock)

定義正在進行之非同步工作的 [XAsyncBlock](/zh-TW/reference/system/xasync/structs/xasyncblock)。[XAsyncBlock](/zh-TW/reference/system/xasync/structs/xasyncblock) 可用來輪詢呼叫的狀態並擷取呼叫結果。如需詳細資訊，請參閱 [XAsyncBlock](/zh-TW/reference/system/xasync/structs/xasyncblock)。

### 傳回值

類型：HRESULT

HRESULT 成功或錯誤碼。

| 錯誤碼 | 描述 |
| - | - |
| 0x89245208 `E_GAMEPACKAGE_NO_PACKAGE_IDENTIFIER` | 未安裝此產品的套件，或是正在對不含套件類型的耐久性產品呼叫此 API (請改用 [XStoreAcquireLicenseForDurablesAsync](/zh-TW/reference/system/xstore/functions/xstoreacquirelicensefordurablesasync))。 |

## 備註

若要擷取套件授權及此函式的執行結果，請在呼叫此函式之後呼叫 [XStoreAcquireLicenseForPackageResult](/zh-TW/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult)。
如果您想要檢查套件是否可以授權，請呼叫 [XStoreCanAcquireLicenseForPackageAsync](/zh-TW/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync)。
取得授權之後，您必須呼叫 [XStoreIsLicenseValid](/zh-TW/reference/system/xstore/functions/xstoreislicensevalid) 來檢查授權是否有效。
[XPackageMountWithUiAsync](/zh-TW/reference/system/xpackage/functions/xpackagemountwithuiasync) 在呼叫時會自動取得授權。
如果無法取得授權，套件就會掛接失敗。

此程式碼片段示範如何使用下列 API：

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

## 需求

**標頭：** XStore.h (包含在 XGameRuntime.h 中)

**程式庫：** xgameruntime.lib

**支援的平台：** Windows、XBOX One 系列主機和 XBOX Series 主機

## 概念文件

* [基本市集作業](/zh-TW/publishing/xstore-commerce/xstore-basic-operations)
* [授予玩家附加元件內容的存取權](/zh-TW/publishing/xstore-commerce/xstore-granting-access)
* [啟用授權測試](/zh-TW/publishing/xstore-commerce/xstore-licensing-setup)

## 另請參閱

[XStore](/zh-TW/reference/system/xstore/xstore_members)\
[XStoreAcquireLicenseForPackageAsync](/zh-TW/reference/system/xstore/functions/xstoreacquirelicenseforpackageasync)\
[XStoreAcquireLicenseForPackageResult](/zh-TW/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult)\
[XStoreIsLicenseValid](/zh-TW/reference/system/xstore/functions/xstoreislicensevalid)\
[XStoreCloseLicenseHandle](/zh-TW/reference/system/xstore/functions/xstorecloselicensehandle)\
[XStoreRegisterPackageLicenseLost](/zh-TW/reference/system/xstore/functions/xstoreregisterpackagelicenselost)\
[XStoreUnregisterPackageLicenseLost](/zh-TW/reference/system/xstore/functions/xstoreunregisterpackagelicenselost)\
[XStoreCanAcquireLicenseForPackageAsync](/zh-TW/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync)\
[XStoreCanAcquireLicenseForPackageResult](/zh-TW/reference/system/xstore/functions/xstorecanacquirelicenseforpackageresult)\
[管理及授權可下載內容 (DLC)](/zh-TW/publishing/xstore-commerce/xstore-dlc)


## Related topics

- [XStoreAcquireLicenseForPackageResult](/zh-TW/reference/system/xstore/functions/xstoreacquirelicenseforpackageresult.md)
- [XStoreCanAcquireLicenseForPackageAsync](/zh-TW/reference/system/xstore/functions/xstorecanacquirelicenseforpackageasync.md)
- [XStoreRegisterPackageLicenseLost](/zh-TW/reference/system/xstore/functions/xstoreregisterpackagelicenselost.md)
- [XStoreUnregisterPackageLicenseLost](/zh-TW/reference/system/xstore/functions/xstoreunregisterpackagelicenselost.md)
- [XStoreCloseLicenseHandle](/zh-TW/reference/system/xstore/functions/xstorecloselicensehandle.md)
