获取系统版本信息

 

#include "stdafx.h"

#include <iostream>
#include <Windows.h>
#include <stdlib.h>
#include <string>
#include <comdef.h>
#include <Wbemidl.h>

using namespace std;

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

std::string Wchar_tToString(const wchar_t *buffer)
{
    DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, buffer, -1, NULL, 0, NULL, FALSE);
    char *psText;  // 临时数组
    psText = new char[dwNum];
    WideCharToMultiByte(CP_OEMCP, NULL, buffer, -1, psText, dwNum, NULL, FALSE);
    std::string strTemp(psText);
    delete[]psText;// psText的清除
    return strTemp;
}

int GetSystemVersion(char* pSystemVersion)
{
    HRESULT hres;
    hres = CoInitializeEx(0, COINIT_MULTITHREADED);
    if (FAILED(hres))
    {
        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))
    {
        CoUninitialize();
        return -1;
    }

    IWbemLocator *pLoc = NULL;
    hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&pLoc);
    if (FAILED(hres))
    {
        CoUninitialize();
        return -1;
    }

    IWbemServices *pSvc = NULL;
    hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc);
    if (FAILED(hres))
    {
        pLoc->Release();
        CoUninitialize();
        return -1;
    }

    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))
    {
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return -1;
    }

    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_OperatingSystem"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
    if (FAILED(hres))
    {
        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"Version", 0, &vtProp, 0, 0);
        //WcharToChar(vtProp.bstrVal, pSystemVersion, 64);
        string str = Wchar_tToString(vtProp.bstrVal);
        strcpy(pSystemVersion,str.c_str());
        VariantClear(&vtProp);
        pclsObj->Release();
    }
    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
    CoUninitialize();

    return 0;
}


void GetOSVersion1(wstring &wstrSystemVerInfo)
{
    DWORD Major, Minor, Build;
    _asm
    {
        pushad
            mov ebx, fs:[0x18]; get self pointer from TEB
            mov eax, fs:[0x30]; get pointer to PEB / database
            mov ebx, [eax + 0A8h]; get OSMinorVersion
            mov eax, [eax + 0A4h]; get OSMajorVersion
            mov Minor, ebx
            mov Major, eax
            popad
    }
    if ((Major == 5) && (Minor == 0))
    {
        wstrSystemVerInfo = L"Windows 2000";
    }
    else if ((Major == 5) && (Minor == 1))
    {
        wstrSystemVerInfo = L"Windows XP";
    }
    else if ((Major == 5) && (Minor == 2))
    {
        wstrSystemVerInfo = L"Windows 2003";
    }
    else if ((Major == 6) && (Minor ==0))
    {
        wstrSystemVerInfo = L"Windows Vista";
    }
    else if ((Major == 6) && (Minor ==1))
    {
        wstrSystemVerInfo = L"Windows 7";
    }
    else if ((Major == 6) && (Minor == 2))
    {
        wstrSystemVerInfo = L"Windows 8";
    }
    else if ((Major == 6) && (Minor == 3))
    {
        wstrSystemVerInfo = L"Windows 8.1";
    }
    else if ((Major == 10) && (Minor == 0))
    {
        wstrSystemVerInfo = L"Windows 10";
    }
    else
    {
        wstrSystemVerInfo = L"other";
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    char pVersion[20];
    int retcode = 0;
    const char* s = "10.0.";
    char *p;
    retcode = GetSystemVersion(pVersion);
//     if (retcode == 0)
//     {
//         p = strstr(pVersion, s);
//         if (p != NULL)
//         {
//             cout << "Current Windows is 2016 Server! " << endl;
//             getchar();
//             return 2;
//         }
//         else
//         {
//             cout << "Current Windows is not 2016 Server! " << endl;
//             getchar();
//             return 0;
//         }
//     }

    printf("verion:%s\n", pVersion);

    wstring strSysInfo;
    GetOSVersion1(strSysInfo);

    wprintf(L"%s\n", strSysInfo.c_str());

    getchar();
    return 0;
}


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值