15 个解决方案
#1
一般都是用API GetVersionEx
windows 7还没有正式版,还没稳定,有可能有变化,当前不需要过多考虑。
The GetVersionEx function obtains extended information about the version of the operating system that is currently running.
To obtain information for the operating system on a remote computer, use the NetWkstaGetInfo function, the Win32_OperatingSystem WMI class, or the OperatingSystem property of the IADsComputer interface.
To compare the current system version to a required version, use the VerifyVersionInfo function instead of using GetVersionEx to perform the comparison yourself.
BOOL GetVersionEx(
LPOSVERSIONINFO lpVersionInfo
);
Parameters
lpVersionInfo
[in, out] Pointer to an OSVERSIONINFO data structure that the function fills with operating system version information.
Before calling the GetVersionEx function, set the dwOSVersionInfoSize member of this structure to sizeof(OSVERSIONINFO).
Windows NT 4.0 SP6 and later: This member can be a pointer to an OSVERSIONINFOEX structure. Set the dwOSVersionInfoSize member to sizeof(OSVERSIONINFOEX) to identify the structure type.
Return Values
If the function succeeds, the return value is a nonzero value.
If the function fails, the return value is zero. To get extended error information, call GetLastError. The function fails if you specify an invalid value for the dwOSVersionInfoSize member of the OSVERSIONINFO or OSVERSIONINFOEX structure.
Remarks
Identifying the current operating system is usually not the best way to determine whether a particular operating system feature is present. This is because the operating system may have had new features added in a redistributable DLL. Rather than using GetVersionEx to determine the operating system platform or version number, test for the presence of the feature itself. For more information, see Operating System Version.
To verify whether the current operating system is either Windows XP Media Center Edition or Tablet PC Edition, use the GetSystemMetrics function.
Example Code [C++]
When using the GetVersionEx function to determine whether your application is running on a particular version of the operating system, check for version numbers that are greater than or equal to the desired version numbers. This ensures that the test succeeds for later versions of the operating system. For example, if your application requires Windows XP, use the following test.
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx (&osvi);
bIsWindowsXPorLater =
( (osvi.dwMajorVersion > 5) ||
( (osvi.dwMajorVersion == 5) && (osvi.dwMinorVersion >= 1) );
windows 7还没有正式版,还没稳定,有可能有变化,当前不需要过多考虑。
The GetVersionEx function obtains extended information about the version of the operating system that is currently running.
To obtain information for the operating system on a remote computer, use the NetWkstaGetInfo function, the Win32_OperatingSystem WMI class, or the OperatingSystem property of the IADsComputer interface.
To compare the current system version to a required version, use the VerifyVersionInfo function instead of using GetVersionEx to perform the comparison yourself.
BOOL GetVersionEx(
LPOSVERSIONINFO lpVersionInfo
);
Parameters
lpVersionInfo
[in, out] Pointer to an OSVERSIONINFO data structure that the function fills with operating system version information.
Before calling the GetVersionEx function, set the dwOSVersionInfoSize member of this structure to sizeof(OSVERSIONINFO).
Windows NT 4.0 SP6 and later: This member can be a pointer to an OSVERSIONINFOEX structure. Set the dwOSVersionInfoSize member to sizeof(OSVERSIONINFOEX) to identify the structure type.
Return Values
If the function succeeds, the return value is a nonzero value.
If the function fails, the return value is zero. To get extended error information, call GetLastError. The function fails if you specify an invalid value for the dwOSVersionInfoSize member of the OSVERSIONINFO or OSVERSIONINFOEX structure.
Remarks
Identifying the current operating system is usually not the best way to determine whether a particular operating system feature is present. This is because the operating system may have had new features added in a redistributable DLL. Rather than using GetVersionEx to determine the operating system platform or version number, test for the presence of the feature itself. For more information, see Operating System Version.
To verify whether the current operating system is either Windows XP Media Center Edition or Tablet PC Edition, use the GetSystemMetrics function.
Example Code [C++]
When using the GetVersionEx function to determine whether your application is running on a particular version of the operating system, check for version numbers that are greater than or equal to the desired version numbers. This ensures that the test succeeds for later versions of the operating system. For example, if your application requires Windows XP, use the following test.
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx (&osvi);
bIsWindowsXPorLater =
( (osvi.dwMajorVersion > 5) ||
( (osvi.dwMajorVersion == 5) && (osvi.dwMinorVersion >= 1) );
#2
进来看看
#3
那天回答完那个问题,我就改写好了,支持Windows 7(版本号为6.1)。呵呵。
#include <shlwapi.h>
#include <tchar.h>
#include <stdio.h>
#define BUFSIZE 256
#define PRODUCT_ULTIMATE 0x00000001
#define PRODUCT_HOME_BASIC 0x00000002
#define PRODUCT_HOME_PREMIUM 0x00000003
#define PRODUCT_ENTERPRISE 0x00000004
#define PRODUCT_HOME_BASIC_N 0x00000005
#define PRODUCT_BUSINESS 0x00000006
#define PRODUCT_STANDARD_SERVER 0x00000007
#define PRODUCT_DATACENTER_SERVER 0x00000008
#define PRODUCT_SMALLBUSINESS_SERVER 0x00000009
#define PRODUCT_ENTERPRISE_SERVER 0x0000000A
#define PRODUCT_STARTER 0x0000000B
#define PRODUCT_DATACENTER_SERVER_CORE 0x0000000C
#define PRODUCT_STANDARD_SERVER_CORE 0x0000000D
#define PRODUCT_ENTERPRISE_SERVER_CORE 0x0000000E
#define PRODUCT_ENTERPRISE_SERVER_IA64 0x0000000F
#define PRODUCT_BUSINESS_N 0x00000010
#define PRODUCT_WEB_SERVER 0x00000011
#define PRODUCT_CLUSTER_SERVER 0x00000012
#define PRODUCT_HOME_SERVER 0x00000013
#define PRODUCT_STORAGE_EXPRESS_SERVER 0x00000014
#define PRODUCT_STORAGE_STANDARD_SERVER 0x00000015
#define PRODUCT_STORAGE_WORKGROUP_SERVER 0x00000016
#define PRODUCT_STORAGE_ENTERPRISE_SERVER 0x00000017
#define PRODUCT_SERVER_FOR_SMALLBUSINESS 0x00000018
#define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM 0x00000019
#define PRODUCT_HOME_PREMIUM_N 0x0000001A
#define PRODUCT_ENTERPRISE_N 0x0000001B
#define PRODUCT_ULTIMATE_N 0x0000001C
#define PRODUCT_WEB_SERVER_CORE 0x0000001D
#define PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT 0x0000001E
#define PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY 0x0000001F
#define PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING 0x00000020
#define PRODUCT_SMALLBUSINESS_SERVER_PRIME 0x00000021
#define PRODUCT_HOME_PREMIUM_SERVER 0x00000022
#define PRODUCT_SERVER_FOR_SMALLBUSINESS_V 0x00000023
#define PRODUCT_STANDARD_SERVER_V 0x00000024
#define PRODUCT_DATACENTER_SERVER_V 0x00000025
#define PRODUCT_ENTERPRISE_SERVER_V 0x00000026
#define PRODUCT_DATACENTER_SERVER_CORE_V 0x00000027
#define PRODUCT_STANDARD_SERVER_CORE_V 0x00000028
#define PRODUCT_ENTERPRISE_SERVER_CORE_V 0x00000029
#define PRODUCT_HYPERV 0x0000002A
#define SM_TABLETPC 86
#define SM_MEDIACENTER 87
#define SM_STARTER 88
#define SM_SERVERR2 89
#define VER_SERVER_NT 0x80000000
#define VER_WORKSTATION_NT 0x40000000
#define VER_SUITE_SMALLBUSINESS 0x00000001
#define VER_SUITE_ENTERPRISE 0x00000002
#define VER_SUITE_BACKOFFICE 0x00000004
#define VER_SUITE_COMMUNICATIONS 0x00000008
#define VER_SUITE_TERMINAL 0x00000010
#define VER_SUITE_SMALLBUSINESS_RESTRICTED 0x00000020
#define VER_SUITE_EMBEDDEDNT 0x00000040
#define VER_SUITE_DATACENTER 0x00000080
#define VER_SUITE_SINGLEUSERTS 0x00000100
#define VER_SUITE_PERSONAL 0x00000200
#define VER_SUITE_BLADE 0x00000400
#define VER_SUITE_EMBEDDED_RESTRICTED 0x00000800
#define VER_SUITE_SECURITY_APPLIANCE 0x00001000
#define VER_SUITE_STORAGE_SERVER 0x00002000
#define VER_SUITE_COMPUTE_SERVER 0x00004000
#define VER_SUITE_WH_SERVER 0x00008000
typedef VOID (WINAPI *GETNATIVESYSTEMINFO)(LPSYSTEM_INFO);
typedef BOOL (WINAPI *GETPRODUCTINFO)(DWORD , DWORD, DWORD, DWORD, PDWORD);
#4
BOOL CrnGetWindowsVersion(LPTSTR pszOS)
{
OSVERSIONINFOEX osvi;
SYSTEM_INFO si;
GETNATIVESYSTEMINFO pGNSI;
GETPRODUCTINFO pGPI;
BOOL bOsVersionInfoEx;
DWORD dwType;
ZeroMemory(&si, sizeof(SYSTEM_INFO));
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *)&osvi);
if (!bOsVersionInfoEx)
return 1;
// Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
pGNSI = (GETNATIVESYSTEMINFO)GetProcAddress(
GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
if (NULL != pGNSI)
pGNSI(&si);
else
GetSystemInfo(&si);
if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4)
{
strncpy(pszOS, TEXT("Microsoft "), BUFSIZE);
// Test for the specific product.
if (osvi.dwMajorVersion == 6)
{
if (osvi.dwMinorVersion == 0)
{
if (osvi.wProductType == VER_NT_WORKSTATION)
strcat(pszOS, TEXT("Windows Vista "));
else
strcat(pszOS, TEXT("Windows Server 2008 "));
}
else if (osvi.dwMinorVersion == 1)
strcat(pszOS, TEXT("Windows 7 "));
pGPI = (GETPRODUCTINFO)GetProcAddress(
GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
if (pGPI)
{
pGPI(6, 0, 0, 0, &dwType);
}
switch (dwType)
{
case PRODUCT_ULTIMATE:
strcat(pszOS, TEXT("Ultimate Edition"));
break;
case PRODUCT_HOME_PREMIUM:
strcat(pszOS, TEXT("Home Premium Edition"));
break;
case PRODUCT_HOME_BASIC:
strcat(pszOS, TEXT("Home Basic Edition"));
break;
case PRODUCT_ENTERPRISE:
strcat(pszOS, TEXT("Enterprise Edition"));
break;
case PRODUCT_BUSINESS:
strcat(pszOS, TEXT("Business Edition"));
break;
case PRODUCT_STARTER:
strcat(pszOS, TEXT("Starter Edition"));
break;
case PRODUCT_CLUSTER_SERVER:
strcat(pszOS, TEXT("Cluster Server Edition"));
break;
case PRODUCT_DATACENTER_SERVER:
strcat(pszOS, TEXT("Datacenter Edition"));
break;
case PRODUCT_DATACENTER_SERVER_CORE:
strcat(pszOS, TEXT("Datacenter Edition (core installation)"));
break;
case PRODUCT_ENTERPRISE_SERVER:
strcat(pszOS, TEXT("Enterprise Edition"));
break;
case PRODUCT_ENTERPRISE_SERVER_CORE:
strcat(pszOS, TEXT("Enterprise Edition (core installation)"));
break;
case PRODUCT_ENTERPRISE_SERVER_IA64:
strcat(pszOS, TEXT("Enterprise Edition for Itanium-based Systems"));
break;
case PRODUCT_SMALLBUSINESS_SERVER:
strcat(pszOS, TEXT("Small Business Server"));
break;
case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
strcat(pszOS, TEXT("Small Business Server Premium Edition"));
break;
case PRODUCT_STANDARD_SERVER:
strcat(pszOS, TEXT("Standard Edition"));
break;
case PRODUCT_STANDARD_SERVER_CORE:
strcat(pszOS, TEXT("Standard Edition (core installation)"));
break;
case PRODUCT_WEB_SERVER:
strcat(pszOS, TEXT("Web Server Edition"));
break;
default:
strcat(pszOS, TEXT("Unknown"));
break;
}
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
strcat(pszOS, TEXT(", 64-bit"));
else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
strcat(pszOS, TEXT(", 32-bit"));
}
if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
{
if (GetSystemMetrics(SM_SERVERR2))
strcat(pszOS, TEXT("Windows Server 2003 R2, "));
else if (osvi.wSuiteMask == VER_SUITE_STORAGE_SERVER)
strcat(pszOS, TEXT("Windows Storage Server 2003"));
else if (osvi.wProductType == VER_NT_WORKSTATION
&& si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
strcat(pszOS, TEXT("Windows XP Professional x64 Edition"));
else
strcat(pszOS, TEXT("Windows Server 2003, "));
// Test for the server type.
if (osvi.wProductType != VER_NT_WORKSTATION)
{
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
{
if ((osvi.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
strcat(pszOS, TEXT("Datacenter Edition for Itanium-based Systems"));
else if ((osvi.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
strcat(pszOS, TEXT("Enterprise Edition for Itanium-based Systems"));
}
else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
{
if ((osvi.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
strcat(pszOS, TEXT("Datacenter x64 Edition"));
else if ((osvi.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
strcat(pszOS, TEXT("Enterprise x64 Edition"));
else
strcat(pszOS, TEXT("Standard x64 Edition"));
}
else
{
if ((osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER) == VER_SUITE_COMPUTE_SERVER)
strcat(pszOS, TEXT("Compute Cluster Edition"));
else if ((osvi.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
strcat(pszOS, TEXT("Datacenter Edition"));
else if ((osvi.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
strcat(pszOS, TEXT("Enterprise Edition"));
else if ((osvi.wSuiteMask & VER_SUITE_BLADE) == VER_SUITE_BLADE)
strcat(pszOS, TEXT("Web Edition"));
else
strcat(pszOS, TEXT("Standard Edition"));
}
}
}
#5
if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
{
strcat(pszOS, TEXT("Windows XP "));
if ((osvi.wSuiteMask & VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL)
strcat(pszOS, TEXT("Home Edition"));
else
strcat(pszOS, TEXT("Professional"));
}
if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
{
strcat(pszOS, TEXT("Windows 2000 "));
if (osvi.wProductType == VER_NT_WORKSTATION)
{
strcat(pszOS, TEXT("Professional"));
}
else
{
if ((osvi.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
strcat(pszOS, TEXT("Datacenter Server"));
else if ((osvi.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
strcat(pszOS, TEXT("Advanced Server"));
else
strcat(pszOS, TEXT("Server"));
}
}
// Include service pack (if any) and build number.
UINT uLen = strlen(osvi.szCSDVersion);
if (uLen > 0)
{
strcat(pszOS, TEXT(" "));
strcat(pszOS, osvi.szCSDVersion);
}
TCHAR szBuf[80];
sprintf(szBuf, TEXT(" (build %d)"), osvi.dwBuildNumber);
strcat(pszOS, szBuf);
return TRUE;
}
else
{
MessageBox(0,
TEXT("Does not support this version of Windows."),
TEXT(__FUNC__),
MB_ICONWARNING | MB_OK | MB_TASKMODAL | MB_SETFOREGROUND);
return FALSE;
}
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TCHAR sz[256];
if (CrnGetWindowsVersion(sz))
ShowMessage(sz);
}
#6
学习中
#7
太复杂了。
#8
强大!!!
#9
谢谢妖哥和三十年孤独!可以结帖了。
#10
真厉害,学习了
#11
登陆了怎么还是看不到
#12
学习啦 真牛啊!
#13
貌似很复杂。。。不过信息很具体~~
#14
留个爪印,日后用到。
#15
我的神哦,这么复杂啊~~
#1
一般都是用API GetVersionEx
windows 7还没有正式版,还没稳定,有可能有变化,当前不需要过多考虑。
The GetVersionEx function obtains extended information about the version of the operating system that is currently running.
To obtain information for the operating system on a remote computer, use the NetWkstaGetInfo function, the Win32_OperatingSystem WMI class, or the OperatingSystem property of the IADsComputer interface.
To compare the current system version to a required version, use the VerifyVersionInfo function instead of using GetVersionEx to perform the comparison yourself.
BOOL GetVersionEx(
LPOSVERSIONINFO lpVersionInfo
);
Parameters
lpVersionInfo
[in, out] Pointer to an OSVERSIONINFO data structure that the function fills with operating system version information.
Before calling the GetVersionEx function, set the dwOSVersionInfoSize member of this structure to sizeof(OSVERSIONINFO).
Windows NT 4.0 SP6 and later: This member can be a pointer to an OSVERSIONINFOEX structure. Set the dwOSVersionInfoSize member to sizeof(OSVERSIONINFOEX) to identify the structure type.
Return Values
If the function succeeds, the return value is a nonzero value.
If the function fails, the return value is zero. To get extended error information, call GetLastError. The function fails if you specify an invalid value for the dwOSVersionInfoSize member of the OSVERSIONINFO or OSVERSIONINFOEX structure.
Remarks
Identifying the current operating system is usually not the best way to determine whether a particular operating system feature is present. This is because the operating system may have had new features added in a redistributable DLL. Rather than using GetVersionEx to determine the operating system platform or version number, test for the presence of the feature itself. For more information, see Operating System Version.
To verify whether the current operating system is either Windows XP Media Center Edition or Tablet PC Edition, use the GetSystemMetrics function.
Example Code [C++]
When using the GetVersionEx function to determine whether your application is running on a particular version of the operating system, check for version numbers that are greater than or equal to the desired version numbers. This ensures that the test succeeds for later versions of the operating system. For example, if your application requires Windows XP, use the following test.
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx (&osvi);
bIsWindowsXPorLater =
( (osvi.dwMajorVersion > 5) ||
( (osvi.dwMajorVersion == 5) && (osvi.dwMinorVersion >= 1) );
windows 7还没有正式版,还没稳定,有可能有变化,当前不需要过多考虑。
The GetVersionEx function obtains extended information about the version of the operating system that is currently running.
To obtain information for the operating system on a remote computer, use the NetWkstaGetInfo function, the Win32_OperatingSystem WMI class, or the OperatingSystem property of the IADsComputer interface.
To compare the current system version to a required version, use the VerifyVersionInfo function instead of using GetVersionEx to perform the comparison yourself.
BOOL GetVersionEx(
LPOSVERSIONINFO lpVersionInfo
);
Parameters
lpVersionInfo
[in, out] Pointer to an OSVERSIONINFO data structure that the function fills with operating system version information.
Before calling the GetVersionEx function, set the dwOSVersionInfoSize member of this structure to sizeof(OSVERSIONINFO).
Windows NT 4.0 SP6 and later: This member can be a pointer to an OSVERSIONINFOEX structure. Set the dwOSVersionInfoSize member to sizeof(OSVERSIONINFOEX) to identify the structure type.
Return Values
If the function succeeds, the return value is a nonzero value.
If the function fails, the return value is zero. To get extended error information, call GetLastError. The function fails if you specify an invalid value for the dwOSVersionInfoSize member of the OSVERSIONINFO or OSVERSIONINFOEX structure.
Remarks
Identifying the current operating system is usually not the best way to determine whether a particular operating system feature is present. This is because the operating system may have had new features added in a redistributable DLL. Rather than using GetVersionEx to determine the operating system platform or version number, test for the presence of the feature itself. For more information, see Operating System Version.
To verify whether the current operating system is either Windows XP Media Center Edition or Tablet PC Edition, use the GetSystemMetrics function.
Example Code [C++]
When using the GetVersionEx function to determine whether your application is running on a particular version of the operating system, check for version numbers that are greater than or equal to the desired version numbers. This ensures that the test succeeds for later versions of the operating system. For example, if your application requires Windows XP, use the following test.
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx (&osvi);
bIsWindowsXPorLater =
( (osvi.dwMajorVersion > 5) ||
( (osvi.dwMajorVersion == 5) && (osvi.dwMinorVersion >= 1) );
#2
进来看看
#3
那天回答完那个问题,我就改写好了,支持Windows 7(版本号为6.1)。呵呵。
#include <shlwapi.h>
#include <tchar.h>
#include <stdio.h>
#define BUFSIZE 256
#define PRODUCT_ULTIMATE 0x00000001
#define PRODUCT_HOME_BASIC 0x00000002
#define PRODUCT_HOME_PREMIUM 0x00000003
#define PRODUCT_ENTERPRISE 0x00000004
#define PRODUCT_HOME_BASIC_N 0x00000005
#define PRODUCT_BUSINESS 0x00000006
#define PRODUCT_STANDARD_SERVER 0x00000007
#define PRODUCT_DATACENTER_SERVER 0x00000008
#define PRODUCT_SMALLBUSINESS_SERVER 0x00000009
#define PRODUCT_ENTERPRISE_SERVER 0x0000000A
#define PRODUCT_STARTER 0x0000000B
#define PRODUCT_DATACENTER_SERVER_CORE 0x0000000C
#define PRODUCT_STANDARD_SERVER_CORE 0x0000000D
#define PRODUCT_ENTERPRISE_SERVER_CORE 0x0000000E
#define PRODUCT_ENTERPRISE_SERVER_IA64 0x0000000F
#define PRODUCT_BUSINESS_N 0x00000010
#define PRODUCT_WEB_SERVER 0x00000011
#define PRODUCT_CLUSTER_SERVER 0x00000012
#define PRODUCT_HOME_SERVER 0x00000013
#define PRODUCT_STORAGE_EXPRESS_SERVER 0x00000014
#define PRODUCT_STORAGE_STANDARD_SERVER 0x00000015
#define PRODUCT_STORAGE_WORKGROUP_SERVER 0x00000016
#define PRODUCT_STORAGE_ENTERPRISE_SERVER 0x00000017
#define PRODUCT_SERVER_FOR_SMALLBUSINESS 0x00000018
#define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM 0x00000019
#define PRODUCT_HOME_PREMIUM_N 0x0000001A
#define PRODUCT_ENTERPRISE_N 0x0000001B
#define PRODUCT_ULTIMATE_N 0x0000001C
#define PRODUCT_WEB_SERVER_CORE 0x0000001D
#define PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT 0x0000001E
#define PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY 0x0000001F
#define PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING 0x00000020
#define PRODUCT_SMALLBUSINESS_SERVER_PRIME 0x00000021
#define PRODUCT_HOME_PREMIUM_SERVER 0x00000022
#define PRODUCT_SERVER_FOR_SMALLBUSINESS_V 0x00000023
#define PRODUCT_STANDARD_SERVER_V 0x00000024
#define PRODUCT_DATACENTER_SERVER_V 0x00000025
#define PRODUCT_ENTERPRISE_SERVER_V 0x00000026
#define PRODUCT_DATACENTER_SERVER_CORE_V 0x00000027
#define PRODUCT_STANDARD_SERVER_CORE_V 0x00000028
#define PRODUCT_ENTERPRISE_SERVER_CORE_V 0x00000029
#define PRODUCT_HYPERV 0x0000002A
#define SM_TABLETPC 86
#define SM_MEDIACENTER 87
#define SM_STARTER 88
#define SM_SERVERR2 89
#define VER_SERVER_NT 0x80000000
#define VER_WORKSTATION_NT 0x40000000
#define VER_SUITE_SMALLBUSINESS 0x00000001
#define VER_SUITE_ENTERPRISE 0x00000002
#define VER_SUITE_BACKOFFICE 0x00000004
#define VER_SUITE_COMMUNICATIONS 0x00000008
#define VER_SUITE_TERMINAL 0x00000010
#define VER_SUITE_SMALLBUSINESS_RESTRICTED 0x00000020
#define VER_SUITE_EMBEDDEDNT 0x00000040
#define VER_SUITE_DATACENTER 0x00000080
#define VER_SUITE_SINGLEUSERTS 0x00000100
#define VER_SUITE_PERSONAL 0x00000200
#define VER_SUITE_BLADE 0x00000400
#define VER_SUITE_EMBEDDED_RESTRICTED 0x00000800
#define VER_SUITE_SECURITY_APPLIANCE 0x00001000
#define VER_SUITE_STORAGE_SERVER 0x00002000
#define VER_SUITE_COMPUTE_SERVER 0x00004000
#define VER_SUITE_WH_SERVER 0x00008000
typedef VOID (WINAPI *GETNATIVESYSTEMINFO)(LPSYSTEM_INFO);
typedef BOOL (WINAPI *GETPRODUCTINFO)(DWORD , DWORD, DWORD, DWORD, PDWORD);
#4
BOOL CrnGetWindowsVersion(LPTSTR pszOS)
{
OSVERSIONINFOEX osvi;
SYSTEM_INFO si;
GETNATIVESYSTEMINFO pGNSI;
GETPRODUCTINFO pGPI;
BOOL bOsVersionInfoEx;
DWORD dwType;
ZeroMemory(&si, sizeof(SYSTEM_INFO));
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *)&osvi);
if (!bOsVersionInfoEx)
return 1;
// Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
pGNSI = (GETNATIVESYSTEMINFO)GetProcAddress(
GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
if (NULL != pGNSI)
pGNSI(&si);
else
GetSystemInfo(&si);
if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4)
{
strncpy(pszOS, TEXT("Microsoft "), BUFSIZE);
// Test for the specific product.
if (osvi.dwMajorVersion == 6)
{
if (osvi.dwMinorVersion == 0)
{
if (osvi.wProductType == VER_NT_WORKSTATION)
strcat(pszOS, TEXT("Windows Vista "));
else
strcat(pszOS, TEXT("Windows Server 2008 "));
}
else if (osvi.dwMinorVersion == 1)
strcat(pszOS, TEXT("Windows 7 "));
pGPI = (GETPRODUCTINFO)GetProcAddress(
GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
if (pGPI)
{
pGPI(6, 0, 0, 0, &dwType);
}
switch (dwType)
{
case PRODUCT_ULTIMATE:
strcat(pszOS, TEXT("Ultimate Edition"));
break;
case PRODUCT_HOME_PREMIUM:
strcat(pszOS, TEXT("Home Premium Edition"));
break;
case PRODUCT_HOME_BASIC:
strcat(pszOS, TEXT("Home Basic Edition"));
break;
case PRODUCT_ENTERPRISE:
strcat(pszOS, TEXT("Enterprise Edition"));
break;
case PRODUCT_BUSINESS:
strcat(pszOS, TEXT("Business Edition"));
break;
case PRODUCT_STARTER:
strcat(pszOS, TEXT("Starter Edition"));
break;
case PRODUCT_CLUSTER_SERVER:
strcat(pszOS, TEXT("Cluster Server Edition"));
break;
case PRODUCT_DATACENTER_SERVER:
strcat(pszOS, TEXT("Datacenter Edition"));
break;
case PRODUCT_DATACENTER_SERVER_CORE:
strcat(pszOS, TEXT("Datacenter Edition (core installation)"));
break;
case PRODUCT_ENTERPRISE_SERVER:
strcat(pszOS, TEXT("Enterprise Edition"));
break;
case PRODUCT_ENTERPRISE_SERVER_CORE:
strcat(pszOS, TEXT("Enterprise Edition (core installation)"));
break;
case PRODUCT_ENTERPRISE_SERVER_IA64:
strcat(pszOS, TEXT("Enterprise Edition for Itanium-based Systems"));
break;
case PRODUCT_SMALLBUSINESS_SERVER:
strcat(pszOS, TEXT("Small Business Server"));
break;
case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
strcat(pszOS, TEXT("Small Business Server Premium Edition"));
break;
case PRODUCT_STANDARD_SERVER:
strcat(pszOS, TEXT("Standard Edition"));
break;
case PRODUCT_STANDARD_SERVER_CORE:
strcat(pszOS, TEXT("Standard Edition (core installation)"));
break;
case PRODUCT_WEB_SERVER:
strcat(pszOS, TEXT("Web Server Edition"));
break;
default:
strcat(pszOS, TEXT("Unknown"));
break;
}
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
strcat(pszOS, TEXT(", 64-bit"));
else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
strcat(pszOS, TEXT(", 32-bit"));
}
if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
{
if (GetSystemMetrics(SM_SERVERR2))
strcat(pszOS, TEXT("Windows Server 2003 R2, "));
else if (osvi.wSuiteMask == VER_SUITE_STORAGE_SERVER)
strcat(pszOS, TEXT("Windows Storage Server 2003"));
else if (osvi.wProductType == VER_NT_WORKSTATION
&& si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
strcat(pszOS, TEXT("Windows XP Professional x64 Edition"));
else
strcat(pszOS, TEXT("Windows Server 2003, "));
// Test for the server type.
if (osvi.wProductType != VER_NT_WORKSTATION)
{
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
{
if ((osvi.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
strcat(pszOS, TEXT("Datacenter Edition for Itanium-based Systems"));
else if ((osvi.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
strcat(pszOS, TEXT("Enterprise Edition for Itanium-based Systems"));
}
else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
{
if ((osvi.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
strcat(pszOS, TEXT("Datacenter x64 Edition"));
else if ((osvi.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
strcat(pszOS, TEXT("Enterprise x64 Edition"));
else
strcat(pszOS, TEXT("Standard x64 Edition"));
}
else
{
if ((osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER) == VER_SUITE_COMPUTE_SERVER)
strcat(pszOS, TEXT("Compute Cluster Edition"));
else if ((osvi.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
strcat(pszOS, TEXT("Datacenter Edition"));
else if ((osvi.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
strcat(pszOS, TEXT("Enterprise Edition"));
else if ((osvi.wSuiteMask & VER_SUITE_BLADE) == VER_SUITE_BLADE)
strcat(pszOS, TEXT("Web Edition"));
else
strcat(pszOS, TEXT("Standard Edition"));
}
}
}
#5
if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
{
strcat(pszOS, TEXT("Windows XP "));
if ((osvi.wSuiteMask & VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL)
strcat(pszOS, TEXT("Home Edition"));
else
strcat(pszOS, TEXT("Professional"));
}
if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
{
strcat(pszOS, TEXT("Windows 2000 "));
if (osvi.wProductType == VER_NT_WORKSTATION)
{
strcat(pszOS, TEXT("Professional"));
}
else
{
if ((osvi.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
strcat(pszOS, TEXT("Datacenter Server"));
else if ((osvi.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
strcat(pszOS, TEXT("Advanced Server"));
else
strcat(pszOS, TEXT("Server"));
}
}
// Include service pack (if any) and build number.
UINT uLen = strlen(osvi.szCSDVersion);
if (uLen > 0)
{
strcat(pszOS, TEXT(" "));
strcat(pszOS, osvi.szCSDVersion);
}
TCHAR szBuf[80];
sprintf(szBuf, TEXT(" (build %d)"), osvi.dwBuildNumber);
strcat(pszOS, szBuf);
return TRUE;
}
else
{
MessageBox(0,
TEXT("Does not support this version of Windows."),
TEXT(__FUNC__),
MB_ICONWARNING | MB_OK | MB_TASKMODAL | MB_SETFOREGROUND);
return FALSE;
}
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TCHAR sz[256];
if (CrnGetWindowsVersion(sz))
ShowMessage(sz);
}
#6
学习中
#7
太复杂了。
#8
强大!!!
#9
谢谢妖哥和三十年孤独!可以结帖了。
#10
真厉害,学习了
#11
登陆了怎么还是看不到
#12
学习啦 真牛啊!
#13
貌似很复杂。。。不过信息很具体~~
#14
留个爪印,日后用到。
#15
我的神哦,这么复杂啊~~