如何从使用“Project Centennial转换器”转换为UWP的Win32应用程序中访问Windows.Services.Store命名空间以启用应用内购买?

11
我有一个本地的C++/MFC应用程序,使用VS 2008开发,没有.NET相关内容。使用Project Centennial转换器将其转换为UWP应用程序。现在我有一个.appx包,在Windows 10 v 1607中作为UWP应用程序运行。
我的下一个目标是在提交到Windows Store之前添加应用内购买支持。
问题是如何从纯Win32应用程序的本地C或C++代码中访问Windows.Services.Store命名空间?
3个回答

11

使用WRL。以下是购买应用内购买的示例:

#include <windows.h>
#include <Windows.Services.Store.h>
#include <wrl.h>

using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Services::Store;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;

#define CheckHr(hr) do { if (FAILED(hr)) __debugbreak(); } while (false)

const wchar_t kItemFriendlyName[] = L"10 coins";
const wchar_t kItemStoreId[] = L"ten_coins";

void OnPurchaseOperationDone(IAsyncOperation<StorePurchaseResult*>* operation, AsyncStatus status);

void Purchase10Coins()
{
    ComPtr<IStoreContextStatics> storeContextStatics;
    auto hr = RoGetActivationFactory(HStringReference(L"Windows.Services.Store.StoreContext").Get(), __uuidof(storeContextStatics), &storeContextStatics);
    CheckHr(hr);

    ComPtr<IStoreContext> storeContext;
    hr = storeContextStatics->GetDefault(&storeContext);
    CheckHr(hr);

    ComPtr<IStorePurchasePropertiesFactory> purchasePropertiesFactory;
    hr = RoGetActivationFactory(HStringReference(L"Windows.Services.Store.StorePurchaseProperties").Get(), __uuidof(purchasePropertiesFactory), &purchasePropertiesFactory);
    CheckHr(hr);

    ComPtr<IStorePurchaseProperties> purchaseProperties;
    hr = purchasePropertiesFactory->Create(HStringReference(kItemFriendlyName).Get(), &purchaseProperties);
    CheckHr(hr);

    ComPtr<IAsyncOperation<StorePurchaseResult*>> purchaseOperation;
    hr = storeContext->RequestPurchaseWithPurchasePropertiesAsync(HStringReference(kItemStoreId).Get(), purchaseProperties.Get(), &purchaseOperation);
    CheckHr(hr);

    // Change the following line to call Callback<IAsyncOperationCompletedHandler<StorePurchaseResult*>> if you want the callback to happen back on the UI thread
    // Implementing FtmBase allows it to fire on the thread the operation finished
    auto onCompletedCallback = Callback<Implements<RuntimeClassFlags<ClassicCom>, IAsyncOperationCompletedHandler<StorePurchaseResult*>, FtmBase>>(
        [](IAsyncOperation<StorePurchaseResult*>* operation, AsyncStatus status)
    {
        OnPurchaseOperationDone(operation, status);
        return S_OK;
    });

    hr = purchaseOperation->put_Completed(onCompletedCallback.Get());
    CheckHr(hr);
}

void OnPurchaseOperationDone(IAsyncOperation<StorePurchaseResult*>* operation, AsyncStatus status)
{
    if (status != AsyncStatus::Completed)
    {
        // It failed for some reason. Find out why.
        ComPtr<IAsyncInfo> asyncInfo;
        auto hr = operation->QueryInterface(__uuidof(asyncInfo), &asyncInfo);
        CheckHr(hr);

        HRESULT errorCode;
        hr = asyncInfo->get_ErrorCode(&errorCode);
        CheckHr(hr);

        // Do something with the errorCode


        // Return once error is handled
        return;
    }

    ComPtr<IStorePurchaseResult> purchaseResult;
    auto hr = operation->GetResults(&purchaseResult);
    CheckHr(hr);

    StorePurchaseStatus purchaseStatus;
    hr = purchaseResult->get_Status(&purchaseStatus);
    CheckHr(hr);

    switch (purchaseStatus)
    {
    case StorePurchaseStatus_Succeeded:
    case StorePurchaseStatus_AlreadyPurchased:
        // Success. Product was purchased
        break;

    case StorePurchaseStatus_NotPurchased:
        // User canceled the purchase
        break;

    case StorePurchaseStatus_NetworkError:
        // The device could not reach windows store
        break;

    case StorePurchaseStatus_ServerError:
        // Something broke on the server
        break;
    }
}

以下是如何检查应用程序是否在试用期:

void CheckIsTrial(std::function<void(bool)> onCompleted)
{
    ComPtr<IStoreContextStatics> storeContextStatics;
    auto hr = RoGetActivationFactory(HStringReference(L"Windows.Services.Store.StoreContext").Get(), __uuidof(storeContextStatics), &storeContextStatics);
    CheckHr(hr);

    ComPtr<IStoreContext> storeContext;
    hr = storeContextStatics->GetDefault(&storeContext);
    CheckHr(hr);

    ComPtr<IAsyncOperation<StoreAppLicense*>> getLicenseOperation;
    hr = storeContext->GetAppLicenseAsync(&getLicenseOperation);
    CheckHr(hr);

    hr = getLicenseOperation->put_Completed(Callback<Implements<RuntimeClassFlags<ClassicCom>, IAsyncOperationCompletedHandler<StoreAppLicense*>, FtmBase>>(
        [onCompleted{ std::move(onCompleted) }](IAsyncOperation<StoreAppLicense*>* operation, AsyncStatus status)
    {
        if (status != AsyncStatus::Completed)
        {
            // It failed for some reason. Find out why.
            ComPtr<IAsyncInfo> asyncInfo;
            auto hr = operation->QueryInterface(__uuidof(asyncInfo), &asyncInfo);
            CheckHr(hr);

            HRESULT errorCode;
            hr = asyncInfo->get_ErrorCode(&errorCode);
            CheckHr(hr);

            // Do something with the errorCode

            // Return once error is handled
            return S_OK;
        }

        ComPtr<IStoreAppLicense> appLicense;
        auto hr = operation->GetResults(&appLicense);
        CheckHr(hr);

        boolean isActive, isTrial = false;

        hr = appLicense->get_IsActive(&isActive);
        CheckHr(hr);

        if (isActive)
        {
            hr = appLicense->get_IsTrial(&isTrial);
            CheckHr(hr);
        }

        onCompleted(static_cast<bool>(isActive));
        return S_OK;
    }).Get());
    CheckHr(hr);
}

1

-1

按照以下更改,这段代码在我这里编译并成功运行:

1)#include <utility.h>

2)编写一个onCompleted处理程序:

void onCompleted(bool bActiveLicense)
{
//     App has active license or not
}

3) 将捕获更改如下:

[=, onCompleted{ std::move(onCompleted) }]

这可能是对@Sunius的答案(https://dev59.com/QFkS5IYBdhLWcg3wgXAQ#39762746)的有用评论。但它不是问题的独立答案。 - Martin Prikryl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接