[Win32] [C ++]检查Win32应用程序的商店许可证 [英] [Win32][C++] Checking Store License of a Win32 Application

查看:133
本文介绍了[Win32] [C ++]检查Win32应用程序的商店许可证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试检查我的应用中的许可证是否有效。该应用程序是一个简单的win32程序,使用visual studio 2017开发。我的问题是,许可证检查总是返回true(激活),即使它不应该(产品从未上传到
商店)。


你能帮助我并告诉我我做错了吗?

 #include< Shobjidl.h> ; 
#include< Windows.Services.Store.h>
#include< wrl.h>

使用命名空间ABI :: Windows :: Foundation;
使用命名空间ABI :: Windows :: Services :: Store;
使用命名空间ABI :: Windows :: System;
使用名称空间Microsoft :: WRL;
使用名称空间Microsoft :: WRL :: Wrappers;

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

bool checkValidStoreLicense()
{
RoInitializeWrapper roinit(RO_INIT_MULTITHREADED);
HRESULT hr = roinit;
CheckHr(hr);

ComPtr< IStoreContextStatics> storeContextStatics;
hr = GetActivationFactory(HStringReference(L" Windows.Services.Store.StoreContext")。Get()/ *,__ uuidof(storeContextStatics)* /,& storeContextStatics);
CheckHr(hr);

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

ComPtr< IInitializeWithWindow> initWindow;
hr = storeContext-> QueryInterface(IID_PPV_ARGS(& initWindow));

HWND hwdlg = CreateWindowEx(
0,//可选窗口样式。
"示例窗口类",//窗口类
"示例窗口", //窗口文本
WS_OVERLAPPEDWINDOW,//窗口样式

//大小和位置
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,

NULL,/ /父窗口
NULL,//菜单
NULL,//实例句柄
NULL //附加应用程序数据
);
hr = initWindow-> Initialize(hwdlg);

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

ComPtr< IStoreAppLicense> appLicense;
//异步编程:设置回调然后等待事件触发
HANDLE hEvent = CreateEvent(NULL,FALSE,FALSE,0);

auto onCompletedCallback = Callback< IAsyncOperationCompletedHandler< StoreAppLicense *>>(
[& appLicense,hEvent](IAsyncOperation< StoreAppLicense *> *操作,AsyncStatus状态)
{
if(status!= AsyncStatus :: Completed){
//由于某种原因失败了。找出原因。
// ComPtr< IAsyncInfo> asyncInfo;
// operation-> QueryInterface(IID_PPV_ARGS(& asyncInfo));
// HRESULT errorCode; asyncInfo-> get_ErrorCode(& errorCode);
}
else {
auto hr = operation-> GetResults(& appLicense); //隐式返回
CheckHr(hr);
}

SetEvent(hEvent);
返回S_OK ;
});

hr = getLicenseOperation-> put_Completed(onCompletedCallback.Get());
//等待操作完成
WaitForSingleObject(hEvent,INFINITE);

boolean bHaveLicence = 0;
if(appLicense){
boolean isActive = false,isTrial = false;
hr = appLicense-> get_IsActive(& isActive);
if(isActive){
hr = appLicense-> get_IsTrial(& isTrial);
bHaveLicence =!isTrial;
}
}

CloseHandle(hEvent);
返回bHaveLicence!= 0;
}

解决方案

您好,


isActive属性用于标识应用程序的状态。如果你已经免费从商店下载了应用程序,那么它将永远是真的。如果你。如果您已从商店购买该应用程序,它也将返回true。有一个条件,
isActive将为您下载试用版的应用程序而返回false,当试用版过期时,isActive将返回false。如果试用版没有过期,isActive将返回true,您需要检查IsTrial属性。


现在的问题是,你如何获得应用程序?如果您免费下载或购买它或者您使用的是试用版,isActive将始终返回true。


祝您好运,


Roy


Hi,

I'm trying to check if the license is active in my app. The app is a plain win32 program that is developed with visual studio 2017. My problem is, that the license check always returns true (activated) even if it should not (product was never uploaded to the store).

Could you help me and tell me what I'm doing wrong?

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

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

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

bool checkValidStoreLicense()
{
    RoInitializeWrapper roinit(RO_INIT_MULTITHREADED);
    HRESULT hr = roinit;
    CheckHr(hr);

    ComPtr<IStoreContextStatics> storeContextStatics;
    hr = GetActivationFactory(HStringReference(L"Windows.Services.Store.StoreContext").Get()/*, __uuidof(storeContextStatics)*/, &storeContextStatics);
    CheckHr(hr);

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

    ComPtr<IInitializeWithWindow> initWindow;
    hr = storeContext->QueryInterface(IID_PPV_ARGS(&initWindow));

    HWND hwdlg = CreateWindowEx(
        0,                              // Optional window styles.
        "Sample Window Class",          // Window class
        "Sample Window",                // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

                                        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        NULL,       // Instance handle
        NULL        // Additional application data
    );
    hr = initWindow->Initialize(hwdlg);

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

    ComPtr<IStoreAppLicense> appLicense;
    // async programming: setup a callback then wait for the event to fire
    HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, 0);

    auto onCompletedCallback = Callback<IAsyncOperationCompletedHandler<StoreAppLicense*>>(
        [&appLicense, hEvent](IAsyncOperation<StoreAppLicense*>* operation, AsyncStatus status)
    {
        if (status != AsyncStatus::Completed) {
            // It failed for some reason. Find out why.
            // ComPtr<IAsyncInfo> asyncInfo;
            // operation->QueryInterface(IID_PPV_ARGS(&asyncInfo));
            // HRESULT errorCode; asyncInfo->get_ErrorCode(&errorCode);
        }
        else {
            auto hr = operation->GetResults(&appLicense); // implicit return
            CheckHr(hr);
        }

        SetEvent(hEvent);
        return S_OK;
    });

    hr = getLicenseOperation->put_Completed(onCompletedCallback.Get());
    // wait for the operation to complete
    WaitForSingleObject(hEvent, INFINITE);

    boolean bHaveLicence = 0;
    if (appLicense) {
        boolean isActive = false, isTrial = false;
        hr = appLicense->get_IsActive(&isActive);
        if (isActive) {
            hr = appLicense->get_IsTrial(&isTrial);
            bHaveLicence = !isTrial;
        }
    }

    CloseHandle(hEvent);
    return bHaveLicence != 0;
}

解决方案

Hi,

The isActive propertyis used to identify the status of the app. If you have downloaded the App form the Store for free, then it will is always true. If you. If you have purchased the app from the Store, it will also return true. There is one condition that isActive will retrun false that you download the app for a trial version, and when the trial version expires, isActive will retrun false. If the trial version doesn't expire, isActive will return true and you will need to check the IsTrial property.

Now the question is that, how you get the app? If you download it for free or purchase it or you are using a trial version, isActive will always return true.

Best regards,

Roy


这篇关于[Win32] [C ++]检查Win32应用程序的商店许可证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆