antman1p
antman1p

Reputation: 524

Programmatically Check if Windows is Activated with C++

I am trying to write a C++ function that will tell the user if the windows OS they are currently using is activated of not.

I found a similar question Programmatically check if Windows 7 is activated , but this answer requires a UID argument. I DO NOT want the user to have to enter ANY arguments at all.

How do I programmatically Check if Windows is Activated with C++?

Upvotes: 2

Views: 2540

Answers (2)

Polar
Polar

Reputation: 3547

For some reason, the accepted answer failed on me. It's always returning false. I'll leave the code below for future purposes. It worked for me starting from Windows-Vista and, as of now Windows-10 version 20H2.

#define _WIN32_WINNT 0x600
#include <iostream>
#include <windows.h>
#include <slpublic.h>
#include <tchar.h>
#pragma comment(lib, "Slwga.lib")
#pragma comment(lib, "Rpcrt4.lib")

using std::cout;
using std::endl;

bool isGenuineWindows()
{
    GUID uid;
    RPC_WSTR rpc = (RPC_WSTR)_T("55c92734-d682-4d71-983e-d6ec3f16059f");
    UuidFromString(rpc, &uid);
    SL_GENUINE_STATE state;
    SLIsGenuineLocal(&uid, &state, NULL);
    return state == SL_GEN_STATE_IS_GENUINE;
}

int main()
{
    if (isGenuineWindows()) {
        cout << "Licensed" << endl;
    }
    else {
        cout << "Unlicensed" << endl;
    }
    return 0;
}

Upvotes: 0

Brandon
Brandon

Reputation: 23525

#define _WIN32_WINNT 0x600

#include <iostream>
#include <windows.h>
#include <slpublic.h>


/*'
From: C:/Windows/System32/SLMGR.vbs


' Copyright (c) Microsoft Corporation. All rights reserved.
'
' Windows Software Licensing Management Tool.
'
' Script Name: slmgr.vbs
'
' WMI class names
private const ServiceClass                            = "SoftwareLicensingService"
private const ProductClass                            = "SoftwareLicensingProduct"
private const TkaLicenseClass                         = "SoftwareLicensingTokenActivationLicense"
private const WindowsAppId                            = "55c92734-d682-4d71-983e-d6ec3f16059f"
*/


/** Use the WindowsAppId above to check if Windows OS itself is Genuine. **/
bool isGenuineWindows()
{
    //WindowsAppId
    unsigned char uuid_bytes[] = {0x35, 0x35, 0x63, 0x39, 0x32, 0x37, 0x33, 0x34, 0x2d, 0x64, 0x36,
                                0x38, 0x32, 0x2d, 0x34, 0x64, 0x37, 0x31, 0x2d, 0x39, 0x38, 0x33,
                                0x65, 0x2d, 0x64, 0x36, 0x65, 0x63, 0x33, 0x66, 0x31, 0x36, 0x30,
                                0x35, 0x39, 0x66};

    GUID uuid;
    SL_GENUINE_STATE state;

    UuidFromStringA(uuid_bytes, &uuid);
    SLIsGenuineLocal(&uuid, &state, nullptr);
    return state == SL_GEN_STATE_IS_GENUINE;
}

int main()
{
    std::cout<<isGenuineWindows();
    return 0;
}

Link against: librpcrt4.a and libslwga.a

Upvotes: 4

Related Questions