方法一:使用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;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1;
}
hres = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL
);
if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1;
}
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;
}
IWbemServices *pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"),
NULL,
NULL,
0,
NULL,
0,
0,
&pSvc
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1;
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
hres = CoSetProxyBlanket(
pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
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;
}
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if (0 == uReturn)
{
break;
}
VARIANT vtProp;
hr = pclsObj->Get(L"GracePeriodRemaining", 0, &vtProp, 0, 0);
wcout << " remain time : " << vtProp.llVal << endl;
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();
}
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
std::cout << "system is :" << (isActive ? "true" : "false") << std::endl;
return 0;
}
需要同时使用LicenseStatus和GracePeriodRemaining两个项去判断是否激活,只用一个不准。
注:使用系统是win7,看了一下win10里面的slmgr.vbs源码有点不一样,但是方法应该也是适用的
|