Winapi 如何从转换为UWP的Win32应用程序访问Windows.Services.Store命名空间;“百年转换器项目”;要启用应用内购买?
我有一个原生的C++/MFC应用程序,它是在VS2008中开发的,没有.NET的东西,我使用。现在我有了一个.appx包,它作为UWP应用程序在Windows10V1607中运行 我的下一个目标是在提交到Windows应用商店之前添加应用内购买支持Winapi 如何从转换为UWP的Win32应用程序访问Windows.Services.Store命名空间;“百年转换器项目”;要启用应用内购买?,winapi,uwp,windows-10,desktop-bridge,Winapi,Uwp,Windows 10,Desktop Bridge,我有一个原生的C++/MFC应用程序,它是在VS2008中开发的,没有.NET的东西,我使用。现在我有了一个.appx包,它作为UWP应用程序在Windows10V1607中运行 我的下一个目标是在提交到Windows应用商店之前添加应用内购买支持 问题是,我如何从纯Win32应用程序访问命名空间,从原生的C或C++代码?< P>使用WRL。下面是一个关于如何购买应用内购买的示例: #include <windows.h> #include <Windows.Services.
问题是,我如何从纯Win32应用程序访问命名空间,从原生的C或C++代码?
< P>使用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;
}
}
#包括
#包括
#包括
使用名称空间ABI::Windows::Foundation;
使用名称空间ABI::Windows::Services::Store;
使用名称空间Microsoft::WRL;
使用名称空间Microsoft::WRL::Wrappers;
#定义CheckHr(hr)do{if(FAILED(hr))uu debugbreak();}while(false)
const wchar_t kItemFriendlyName[]=L“10枚硬币”;
const wchar_t kItemStoreId[]=L“十枚硬币”;
void OnPurchaseOperationDone(IAsyncOperation*操作,异步状态);
无效购买10个币()
{
ComPtr storeContextStatics;
auto hr=RoGetActivationFactory(HStringReference(L“Windows.Services.Store.StoreContext”).Get()、u uuidof(storeContextStatics)和storeContextStatics);
CheckHr(hr);
ComPtr-storeContext;
hr=storeContextStatics->GetDefault(&storeContext);
CheckHr(hr);
ComPtr PurchaseProperties工厂;
hr=RoGetActivationFactory(HStringReference(L“Windows.Services.Store.StorePurchaseProperties”).Get(),u uuidof(purchasePropertiesFactory),&purchasePropertiesFactory);
CheckHr(hr);
ComPtr purchaseProperties;
hr=purchasePropertiesFactory->Create(HStringReference(kItemFriendlyName).Get(),&purchaseProperties);
CheckHr(hr);
ComPtr采购操作;
hr=storeContext->RequestPurchaseWithPurchasePropertiesAsync(HStringReference(kItemStoreId).Get(),purchaseProperties.Get(),&purchaseOperation);
CheckHr(hr);
//如果希望回调发生在UI线程上,请将下面的行更改为callcallback
//实现FtmBase允许它在操作完成时触发线程
自动onCompletedCallback=回调(
[](IAsyncOperation*操作,异步状态)
{
OnPurchaseOperationDone(操作、状态);
返回S_OK;
});
hr=purchaseOperation->put_Completed(onCompletedCallback.Get());
CheckHr(hr);
}
void OnPurchaseOperationDone(IAsyncOperation*操作,异步状态)
{
如果(状态!=异步状态::已完成)
{
//因为某种原因它失败了。找出原因。
ComPtr异步信息;
自动hr=操作->查询接口(\uuuuuidof(asyncInfo),&asyncInfo);
CheckHr(hr);
HRESULT错误代码;
hr=asyncInfo->get_ErrorCode(&ErrorCode);
CheckHr(hr);
//对错误代码做点什么
//处理错误后返回
返回;
}
ComPtr purchaseResult;
自动hr=操作->获取结果(&purchaseResult);
CheckHr(hr);
StorePurchaseStatus采购状态;
hr=采购结果->获取采购状态(&purchaseStatus);
CheckHr(hr);
交换机(购买状态)
{
案例StorePurchaseStatus_成功:
案例商店采购状态\u已采购:
//成功。产品已购买
打破
案例商店采购状态\u未采购:
//用户取消了购买
打破
案例库采购状态\u网络错误:
//设备无法访问windows应用商店
打破
case StorePurchaseStatus_服务器错误:
//服务器上有东西坏了
打破
}
}
以下是如何检查应用程序是否处于试用期:
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);
}
void CheckIsTrial(标准::函数未完成)
{
ComPtr storeContextStatics;
auto hr=RoGetActivationFactory(HStringReference(L“Windows.Services.Store.StoreContext”).Get()、u uuidof(storeContextStatics)和storeContextStatics);
CheckHr(hr);
ComPtr-storeContext;
hr=storeContextStatics->GetDefault(&storeContext);
CheckHr(hr);
ComPtr getLicenseOperation;
hr=storeContext->GetAppLicenseAsync(&getLicenseOperation);
CheckHr(hr);
hr=getLicenseOperation->put\u已完成(回调(
[onCompleted{std::move(onCompleted)}](IAsyncOperation*操作,异步状态)
{
如果(状态!=异步状态::已完成)
{
//因为某种原因它失败了。找出原因。
ComPtr异步信息;
自动hr=操作->查询接口(\uuuuuidof(asyncInfo),&asyncInfo);
CheckHr(hr);
HRESULT错误代码;
hr=asyncInfo->get_ErrorCode(&ErrorCode);
CheckHr(hr);
//对错误代码做点什么
//处理错误后返回
返回S_OK;
}
ComPtr appLicense;
自动hr=操作->获取结果(&appLicense);
CheckHr(hr);
布尔值isActive,isTrial=false;
hr=appLicense->get\u IsActive(&IsActive);
CheckHr(hr);
如果(isActive)
{
hr=appLicense->get_-IsTrial(&IsTrial);
CheckHr(hr);
}
未完成(静态(isActive));
返回S_OK;
}).Get());
CheckHr(hr);
}
参见此处:
它说:
注意:在使用桌面网桥的Windows桌面应用程序中,
您必须添加一些附加代码来配置StoreContext对象
在应用程序可以使用此对象之前。有关详细信息,请参见使用
使用桌面的桌面应用程序中的StoreContext类
桥。
通过以下更改,我完成了这项工作: 1)
#包括
2) 编写一个未完成的处理程序:
void onCompleted(bool bActiveLicense)
{
// App has active license or not
}
3) 将捕获更改如下:
[=, onCompleted{ std::move(onCompleted) }]
谢谢我从哪里获取
Windows.Services.Store.h
?它在Windows SDK中。它在我的机器上:“C:\ProgramFiles(x86)\Windows Kits\10\Include\10.0.14393.0\winrt\Windows.services.store.h