XStoreAcquireLicenseForPackageAsync

获取用户有权使用的当前游戏的已安装 DLC 包(具有程序包产品类型的 Durable)的许可证。

XStoreRegisterPackageLicenseLost 可用于监视从此 API 获取的许可证是否丢失。

注意

如果没有程序包,此 API 将不适用于 Durable 加载项。 若要获取不带程序包的 Durables 许可证,请使用XStoreAcquireLicenseForDurablesAsync

语法

HRESULT XStoreAcquireLicenseForPackageAsync(  
         const XStoreContextHandle storeContextHandle,  
         const char* packageIdentifier,  
         XAsyncBlock* async  
)  

参数

storeContextHandle _In_
类型:XStoreContextHandle

XStoreCreateContext 返回的用户的应用商店上下文句柄。

packageIdentifier _In_z_
类型:char*

唯一标识 Microsoft Store 包的字符串。 这通常作为 XPackageEnumeratePackages 操作的一部分返回。 有关包标识符的详细信息,请参阅可下载内容 (DLC) 的管理和授权

async _Inout_
类型:XAsyncBlock*

用于定义正在进行的异步工作的 XAsyncBlockXAsyncBlock 可用于轮询调用的状态和检索调用结果。 有关详细信息,请参阅 XAsyncBlock

返回值

类型:HRESULT

HRESULT 成功或错误代码。

错误代码 说明
0x89245208 E_GAMEPACKAGE_NO_PACKAGE_IDENTIFIER 未安装此产品的程序包,或者在不带程序包类型的 Durable 上调用此包(请改用XStoreAcquireLicenseForDurablesAsync)。

备注

要检索程序包许可证以及此函数的执行结果,请在调用此函数后调用 XStoreAcquireLicenseForPackageResult。 如果您只想要检查程序包是否可以获得许可,请调用 XStoreCanAcquireLicenseForPackageAsync。 获得许可证后,您可以通过调用 XStoreIsLicenseValid 检查它是否有效。

下面的代码段显示一个使用以下 API 的示例。

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;
    }

    // Wait a while for the callbacks to run
    Sleep(5000);
}

要求

头文件:XStore.h(包含在 XGameRuntime.h 中)

库:xgameruntime.lib

支持平台:Windows、Xbox One 系列主机和 Xbox Series 主机

另请参阅

XStore
XStoreAcquireLicenseForPackageAsync
XStoreAcquireLicenseForPackageResult
XStoreIsLicenseValid
XStoreCloseLicenseHandle
XStoreRegisterPackageLicenseLost
XStoreUnregisterPackageLicenseLost
XStoreCanAcquireLicenseForPackageAsync
XStoreCanAcquireLicenseForPackageResult