C++ 获取系统版本号

Posted bachelor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 获取系统版本号相关的知识,希望对你有一定的参考价值。

因用到系统版本号,区别对待。

    // 5.0 Windows 2000";
    // 5.1 Windows XP";
    // 5.2 Windows 2003";
    // 6.0 Windows Vista";
    // 6.1 Windows 7";
    // 6.2 Windows 8";
    // 6.3 Windows 8.1";
    // 10.0 Windows 10";
    //其他版本

方法1:

此方法是测下来 XP、win7、win10都通过的方法。

DWORD Major,Minor,Build;
void
GetOSVersion1() _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 Build = 0;

方法2:

 

DWORD Major,Minor,Build;

typedef NTSTATUS(WINAPI* _NtQueryInformationProcess)
(_In_ HANDLE ProcessHandle, _In_ PROCESSINFOCLASS ProcessInformationClass,
    _Out_ PVOID ProcessInformation, _In_ ULONG ProcessInformationLength, _Out_opt_ PULONG ReturnLength);
_NtQueryInformationProcess NtQueryInformationProcess_;

DWORD GetProcessPEBAddress(HANDLE hProc)

    PROCESS_BASIC_INFORMATION peb;
    DWORD tmp;
    NtQueryInformationProcess_ = (_NtQueryInformationProcess)GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtQueryInformationProcess");
    NtQueryInformationProcess_(hProc, ProcessBasicInformation, &peb, sizeof(PROCESS_BASIC_INFORMATION), &tmp);
    return (DWORD)peb.PebBaseAddress;


void GetOSVersionByHandle(HANDLE handle)

    DWORD pebAddress = GetProcessPEBAddress(handle);
    DWORD OSMajorVersionAddress = pebAddress + 0x0a4;
    DWORD OSMinorVersionAddress = pebAddress + 0x0a8;
    DWORD OSBuildNumberAddress = pebAddress + 0x0ac;
    ReadProcessMemory(handle, (void*)OSMajorVersionAddress, &Major, sizeof(Major), 0);
    ReadProcessMemory(handle, (void*)OSMinorVersionAddress, &Minor, sizeof(Minor), 0);
    ReadProcessMemory(handle, (void*)OSBuildNumberAddress, &Build, sizeof(Build), 0);



void GetOSVersion2()

    HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
    GetOSVersionByHandle(handle);

 

方法3:

GetVersionEx在win10版本以后,已经失效。

DWORD Major,Minor,Build;

void GetOSVersion3()

    OSVERSIONINFO osvi;                    //定义OSVERSIONINFO数据结构对象
    memset(&osvi, 0, sizeof(OSVERSIONINFO));        //开空间 
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);    //定义大小 
    GetVersionEx(&osvi);                    //获得版本信息

    Major = osvi.dwMajorVersion;
    Minor = osvi.dwMinorVersion;
    Build = osvi.dwBuildNumber;

 

技术图片             技术图片             技术图片

 

 编译xp的运行程序 需要设置工程的属性:

 

 技术图片

 

以上是关于C++ 获取系统版本号的主要内容,如果未能解决你的问题,请参考以下文章

Android获取系统(ROM)类别及版本号

android获取系统版本号

vb如何读取系统已安装软件并获取软件版本号

获取系统版本号

win10 系统下获取系统版本号为6.2的问题

获取系统版本号