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;
}

                         

扫描二维码关注公众号,回复: 6932306 查看本文章

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

 

猜你喜欢

转载自www.cnblogs.com/Bachelor/p/11290159.html