简单获取系统版本示例


一般由系统的dwMajorVersion和dwMinorVersion就可以得到系统版本:

Windows 3.0,

Windows 3.1,

Windows NT, 95(4.0),

Windows 2000  (5.0), 

Windows XP    (5.1), .... 

Windows 7     (6.1), ..... ,

Windows 8.1   (6.3)



struct WindowsNTOSInfo

{

    DWORD dwMajorVersion;

    DWORD dwMinorVersion;

    WORD wServicePackMajor;    

    //const TCHAR * pcszOSDisplayName;

};


const WindowsNTOSInfo KnownVersionsOfWindows[] =

{

    { 6, 3, 0,   },//win8.1,server2012 r2

    { 6, 2, 0,   },//win8,server2012


    { 6, 1, 1,  },//win7,win2008r2 sp1

    { 6, 1, 0,  },//win7,win2008r2


    { 5, 1, 3,  },//winxp sp3

    { 5, 1, 2,  },//winxp sp2

    { 5, 1, 1,  },//winxp sp1

    { 5, 1, 0,  },//winxp


    { 6, 0, 2,  },//WinVista,server2008 SP2

    { 6, 0, 1,  },//WinVista,Server2008 Sp1

    { 6, 0, 0,  },//WinVista,Server2008


    { 5, 2, 2,  },//Windows Server 2003 Sp2

    { 5, 2, 1,  },//Windows Server 2003 Sp1

    { 5, 2, 0,  },//Windows Server 2003



    { 5, 1, 4,  }, //Windows Server 2000 Sp4

    { 5, 1, 3,  }, //Windows Server 2000 Sp3

    { 5, 1, 2,  }, //Windows Server 2000 Sp2

    { 5, 1, 2,  }, //Windows Server 2000 Sp1

    { 5, 1, 0,  }, //Windows Server 2000

};


以下程序可以得出系统是win7还是xp

1)采用VerifyVersionInfo函数

BOOL IsWin7()
{
	OSVERSIONINFOEX osvi;
	DWORDLONG dwlConditionMask = 0;

	// Initialize the OSVERSIONINFOEX structure.

	ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
	osvi.dwMajorVersion = 6;
	osvi.dwMinorVersion = 1;
	//osvi.wServicePackMajor = 1;

	// Initialize the condition mask.

	VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION,
		VER_EQUAL);
	VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION,
		VER_EQUAL);
	//VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMAJOR,	VER_EQUAL);

	// Perform the test.

	return VerifyVersionInfo(
		&osvi,
		VER_MAJORVERSION | VER_MINORVERSION,
		dwlConditionMask);
}


BOOL IsXP()
{
	OSVERSIONINFOEX osvi;
	DWORDLONG dwlConditionMask = 0;

	// Initialize the OSVERSIONINFOEX structure.

	ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
	osvi.dwMajorVersion = 5;
	osvi.dwMinorVersion = 1;
	//osvi.wServicePackMajor = 1;

	// Initialize the condition mask.

	VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION,
		VER_EQUAL);
	VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION,
		VER_EQUAL);
	//VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMAJOR,	VER_EQUAL);

	// Perform the test.

	return VerifyVersionInfo(
		&osvi,
		VER_MAJORVERSION | VER_MINORVERSION,
		dwlConditionMask);
}

int _tmain(int argc, TCHAR* argv[])
{
	if (IsWin7())
		cout << "win 7" << endl;
	else if (IsXP())
		cout << "xp" << endl;
	else
		cout << "unrecognized system" << endl;

	system("pause");
	return 0;
}


2).

#include <iostream>
#include <Windows.h>	 // VersionHelpers.h 依赖于 windows.h
#include <VersionHelpers.h> // Windows SDK 8.1 才有喔
using namespace std;
int main(void)
{
if (IsWindows8OrGreater())
cout << "Win 8" << endl;
else if (IsWindows7OrGreater())
cout << "Win 7" << endl;
else if (IsWindowsXPOrGreater())
cout << "xp" << endl;
else
cout << "unrecognized system" << endl;

system("pause");
}


你可能感兴趣的:(简单获取系统版本示例)