【wmi】C++获取windows激活状态

2021/11/25 7:12:54

本文主要是介绍【wmi】C++获取windows激活状态,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

方法一:使用SLIsGenuineLocal

这个方法获取到的激活状态不准

bool IsGenuineWindows()
{
	GUID uid;
	RPC_WSTR rpc = (RPC_WSTR)_T("55c92734-d682-4d71-983e-d6ec3f16059f");
	UuidFromString(rpc, &uid);

	HINSTANCE hInstance;
	hInstance = LoadLibrary(_T("slwga.dll"));
	if (hInstance == NULL)
		return false;

	typedef HRESULT(WINAPI* SLIsGenuineLocal_Ptr)(__in CONST SLID* pAppId, __out SL_GENUINE_STATE* pGenuineState, __inout_opt SL_NONGENUINE_UI_OPTIONS* pUIOptions);

	SLIsGenuineLocal_Ptr fnSLIsGenuineLocal = (SLIsGenuineLocal_Ptr)GetProcAddress(hInstance, "SLIsGenuineLocal");
	if (fnSLIsGenuineLocal == NULL)
		return false;
	
	SL_GENUINE_STATE state;
	HRESULT hr = fnSLIsGenuineLocal(&uid, &state, NULL);
	return state == SL_GENUINE_STATE::SL_GEN_STATE_IS_GENUINE;
}

方法二,使用slmgr.vbs中的方法

步骤一:cmd窗口输入slmgr.vbs -xpr查看目前激活状态,使用win7系统验证
在这里插入图片描述
查看slmgr.vbs源码
在这里插入图片描述

结合wmi读写,结合成代码

#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>

#pragma comment(lib, "wbemuuid.lib")

int main(int argc, char **argv)
{
	HRESULT hres;
	int isActive = false;
	// Step 1: --------------------------------------------------
	// Initialize COM. ------------------------------------------

	hres = CoInitializeEx(0, COINIT_MULTITHREADED);
	if (FAILED(hres))
	{
		cout << "Failed to initialize COM library. Error code = 0x"
			<< hex << hres << endl;
		return 1;                  // Program has failed.
	}

	// Step 2: --------------------------------------------------
	// Set general COM security levels --------------------------

	hres = CoInitializeSecurity(
		NULL,
		-1,                          // COM authentication
		NULL,                        // Authentication services
		NULL,                        // Reserved
		RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
		RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  
		NULL,                        // Authentication info
		EOAC_NONE,                   // Additional capabilities 
		NULL                         // Reserved
		);


	if (FAILED(hres))
	{
		cout << "Failed to initialize security. Error code = 0x"
			<< hex << hres << endl;
		CoUninitialize();
		return 1;                    // Program has failed.
	}

	// Step 3: ---------------------------------------------------
	// Obtain the initial locator to WMI -------------------------

	IWbemLocator *pLoc = NULL;

	hres = CoCreateInstance(
		CLSID_WbemLocator,
		0,
		CLSCTX_INPROC_SERVER,
		IID_IWbemLocator, (LPVOID *)&pLoc);

	if (FAILED(hres))
	{
		cout << "Failed to create IWbemLocator object."
			<< " Err code = 0x"
			<< hex << hres << endl;
		CoUninitialize();
		return 1;                 // Program has failed.
	}

	// Step 4: -----------------------------------------------------
	// Connect to WMI through the IWbemLocator::ConnectServer method

	IWbemServices *pSvc = NULL;

	// Connect to the root\cimv2 namespace with
	// the current user and obtain pointer pSvc
	// to make IWbemServices calls.
	hres = pLoc->ConnectServer(
		_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
		NULL,                    // User name. NULL = current user
		NULL,                    // User password. NULL = current
		0,                       // Locale. NULL indicates current
		NULL,                    // Security flags.
		0,                       // Authority (for example, Kerberos)
		0,                       // Context object 
		&pSvc                    // pointer to IWbemServices proxy
		);

	if (FAILED(hres))
	{
		cout << "Could not connect. Error code = 0x"
			<< hex << hres << endl;
		pLoc->Release();
		CoUninitialize();
		return 1;                // Program has failed.
	}

	cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;


	// Step 5: --------------------------------------------------
	// Set security levels on the proxy -------------------------

	hres = CoSetProxyBlanket(
		pSvc,                        // Indicates the proxy to set
		RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
		RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
		NULL,                        // Server principal name 
		RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
		RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
		NULL,                        // client identity
		EOAC_NONE                    // proxy capabilities 
		);

	if (FAILED(hres))
	{
		cout << "Could not set proxy blanket. Error code = 0x"
			<< hex << hres << endl;
		pSvc->Release();
		pLoc->Release();
		CoUninitialize();
		return 1;               // Program has failed.
	}

	// Step 6: --------------------------------------------------
	// Use the IWbemServices pointer to make requests of WMI ----

	// For example, get the name of the operating system
	IEnumWbemClassObject* pEnumerator = NULL;
	hres = pSvc->ExecQuery(
		bstr_t("WQL"),
		bstr_t("select ID,ApplicationId,PartialProductKey,LicenseIsAddon,Description,Name,LicenseStatus,GracePeriodRemaining from SoftwareLicensingProduct where ApplicationID = '55c92734-d682-4d71-983e-d6ec3f16059f' and PartialProductKey is not null"),
		WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
		NULL,
		&pEnumerator);

	if (FAILED(hres))
	{
		cout << "Query for operating system name failed."
			<< " Error code = 0x"
			<< hex << hres << endl;
		pSvc->Release();
		pLoc->Release();
		CoUninitialize();
		return 1;               // Program has failed.
	}

	// Step 7: -------------------------------------------------
	// Get the data from the query in step 6 -------------------

	IWbemClassObject *pclsObj = NULL;
	ULONG uReturn = 0;

	while (pEnumerator)
	{
		HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
			&pclsObj, &uReturn);

		if (0 == uReturn)
		{
			break;
		}

		VARIANT vtProp;

		// Get the value of the Name property
		hr = pclsObj->Get(L"GracePeriodRemaining", 0, &vtProp, 0, 0);
		wcout << " remain time : " << vtProp.llVal << endl;
		// Get the value of the Name property
		VARIANT vtProp2;
		hr = pclsObj->Get(L"LicenseStatus", 0, &vtProp2, 0, 0);
		wcout << " LicenseStatus time : " << vtProp2.lVal << endl;
		if (vtProp.llVal == 0 || vtProp2.lVal==1)
		{
			isActive = true;
		}
		VariantClear(&vtProp);
		VariantClear(&vtProp2);
		pclsObj->Release();
	}

	// Cleanup
	// ========

	pSvc->Release();
	pLoc->Release();
	pEnumerator->Release();
	CoUninitialize();
	std::cout << "system is :" << (isActive ? "true" : "false") << std::endl;
	return 0;   // Program successfully completed.

}

需要同时使用LicenseStatus和GracePeriodRemaining两个项去判断是否激活,只用一个不准。

注:使用系统是win7,看了一下win10里面的slmgr.vbs源码有点不一样,但是方法应该也是适用的



这篇关于【wmi】C++获取windows激活状态的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程