GetVersionEx The GetVersionEx function obtains extended information about the version of the operating system that is currently running.BOOL GetVersionEx( LPOSVERSIONINFO lpVersionInformation // pointer to version // information structure );
Parameters lpVersionInformation 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 the OSVERSIONINFO data structure to sizeof(OSVERSIONINFO). Windows NT 5.0 and later: This member can be a pointer to an OSVERSIONINFOEX structure. Set the dwOSVersionInfoSize member to sizeof(OSVERSIONINFOEX) to identify the type of structure. Return Values If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, callGetLastError. The function fails if you specify an invalid value for the dwOSVersionInfoSize member of the OSVERSIONINFO or OSVERSIONINFOEX structure. Res 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 98, use the following test:GetVersionEx (&osvi); bIsWindows98orLater = (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) && ( (osvi.dwMajorVersion > 4) || ( (osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion > 0) ) );
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.To determine the best way to test for a feature, refer to the documentation for the feature of interest. The following list discusses some common techniques for feature detection: You can test for the presence of the functions associated with a feature. To test for the presence of a function in a system DLL, call theLoadLibrary function to load the DLL. Then call theGetProcAddress function to determine whether the function of interest is present in the DLL. Use the pointer returned by GetProcAddress to call the function. Note that even if the function is present, it may be a stub that just returns an error code such as ERROR_CALL_NOT_IMPLEMENTED. You can determine the presence of some features by using the GetSystemMetrics function. For example, you can detect multiple display monitors by calling GetSystemMetrics(SM_CMONITORS). There are several versions of the redistributable DLLs that implement shell and common control features. For information about determining which versions are present on the system your application is running on, see the topicShell Versions. Windows CE: The value of the dwPlatformID member of the OSVERSIONINFO structure will be VER_PLATFORM_WIN32_CE.QuickInfo Windows NT: Requires version 3.5 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winbase.h. Import Library: Use kernel32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT.
Getting the System Version The following example uses the GetVersionEx function to display the version of the currently running operating system. Relying on version information is not the best way to test for a feature. Instead, refer to the documentation for the feature of interest. For more information on common techniques for feature detection, see Operating System Version. If you must require a particular operating system, be sure to use it as a minimum supported version, rather than design the test for the one operating system. This way, your detection code will continue to work on future versions of Windows. #include <windows.h> #include <stdio.h>BOOL DisplaySystemVersion() { OSVERSIONINFOEX osvi; BOOL bOsVersionInfoEx; // Try calling GetVersionEx using the OSVERSIONINFOEX structure. // // If that fails, try using the OSVERSIONINFO structure. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) ) { // If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO. osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) return FALSE; } switch (osvi.dwPlatformId) { case VER_PLATFORM_WIN32_NT: // Test for the product. if ( osvi.dwMajorVersion <= 4 ) printf("Microsoft Windows NT "); if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) printf ("Microsoft Windows 2000 "); if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) printf ("Microsoft Windows XP "); // Test for product type. if( bOsVersionInfoEx ) { if ( osvi.wProductType == VER_NT_WORKSTATION ) { if( osvi.wSuiteMask & VER_SUITE_PERSONAL ) printf ( "Personal " ); else printf ( "Professional " ); } else if ( osvi.wProductType == VER_NT_SERVER ) { if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) printf ( "DataCenter Server " ); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) printf ( "Advanced Server " ); else printf ( "Server " ); } } else { HKEY hKey; char szProductType[80]; DWORD dwBufLen; RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0, KEY_QUERY_VALUE, &hKey ); RegQueryValueEx( hKey, "ProductType", NULL, NULL, (LPBYTE) szProductType, &dwBufLen); RegCloseKey( hKey ); if ( lstrcmpi( "WINNT", szProductType) == 0 ) printf( "Professional " ); if ( lstrcmpi( "LANMANNT", szProductType) == 0 ) printf( "Server " ); if ( lstrcmpi( "SERVERNT", szProductType) == 0 ) printf( "Advanced Server " ); } // Display version, service pack (if any), and build number. if ( osvi.dwMajorVersion <= 4 ) { printf ("version %d.%d %s (Build %d)\n", osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); } else { printf ("%s (Build %d)\n", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); } break; case VER_PLATFORM_WIN32_WINDOWS: if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) { printf ("Microsoft Windows 95 "); if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' ) printf("OSR2 " ); } if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) { printf ("Microsoft Windows 98 "); if ( osvi.szCSDVersion[1] == 'A' ) printf("SE " ); } if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) { printf ("Microsoft Windows Me "); } break; case VER_PLATFORM_WIN32s: printf ("Microsoft Win32s "); break; } return TRUE; }
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/osversioninfo_str.asp http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getting_the_system_version.asp 最新版 能判断Microsoft Windows Server 2003
看见 VB 老大 Zyl910 说了那么多~~ 不说了,听课吧... :)
Option Explicit Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _ (lpVersionInformation As OSVERSIONINFOEX) As Long Private Declare Function DllGetVersion Lib "Shlwapi.dll" (dwVersion As DllVersionInfo) As Long Private Type OSVERSIONINFOEX dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 wServicePackMajor As Integer wServicePackMinor As Integer wSuiteMask As Integer wProductType As Byte wReserved As Byte End Type Private Type DllVersionInfo cbSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long End Type ''used for platform id Const VER_PLATFORM_WIN32s = 0 ''win 3.x Const VER_PLATFORM_WIN32_WINDOWS = 1 ''win 9.x Const VER_PLATFORM_WIN32_NT = 2 ''win nt,2000,XP ''used for product type Const VER_NT_WORKSTATION = 1 Const VER_NT_SERVER = 3 ''used for suite mask Const VER_SUITE_DATACENTER = 128 Const VER_SUITE_ENTERPRISE = 2 Const VER_SUITE_PERSONAL = 512 Private Const INFINITE = &HFFFFFFFF Private Const NORMAL_PRIORITY_CLASS = &H20 Private Const WAIT_TIMEOUT = &H102& Public Function GetOSVer() As String Dim osv As OSVERSIONINFOEX osv.dwOSVersionInfoSize = Len(osv) If GetVersionEx(osv) = 1 Then Select Case osv.dwPlatformId Case Is = VER_PLATFORM_WIN32s GetOSVer = "Windows 3.x" Case Is = VER_PLATFORM_WIN32_WINDOWS Select Case osv.dwMinorVersion Case Is = 0 If InStr(UCase(osv.szCSDVersion), "C") Then GetOSVer = "Windows 95 OSR2" Else GetOSVer = "Windows 95" End If Case Is = 10 If InStr(UCase(osv.szCSDVersion), "A") Then GetOSVer = "Windows 98 SE" Else GetOSVer = "Windows 98" End If Case Is = 90 GetOSVer = "Windows Me" End Select Case Is = VER_PLATFORM_WIN32_NT Select Case osv.dwMajorVersion Case Is = 3 Select Case osv.dwMinorVersion Case Is = 0 GetOSVer = "Windows NT 3" Case Is = 1 GetOSVer = "Windows NT 3.1" Case Is = 5 GetOSVer = "Windows NT 3.5" Case Is = 51 GetOSVer = "Windows NT 3.51" End Select Case Is = 4 GetOSVer = "Windows NT 4" Case Is = 5 Select Case osv.dwMinorVersion Case Is = 0 ''win 2000 Select Case osv.wProductType Case Is = VER_NT_WORKSTATION GetOSVer = "Windows 2000 Professional" Case Is = VER_NT_SERVER Select Case osv.wSuiteMask Case Is = VER_SUITE_DATACENTER GetOSVer = "Windows 2000 DataCenter Server" Case Is = VER_SUITE_ENTERPRISE GetOSVer = "Windows 2000 Advanced Server" Case Else GetOSVer = "Windows 2000 Server" End Select End Select Case Is = 1 ''win XP or win .NET server Select Case osv.wProductType Case Is = VER_NT_WORKSTATION ''win XP If osv.wSuiteMask = VER_SUITE_PERSONAL Then GetOSVer = "Windows XP Home Edition" Else GetOSVer = "Windows XP Professional" End If Case Else If osv.wSuiteMask = VER_SUITE_ENTERPRISE Then GetOSVer = "Windows .NET Enterprise Server" Else GetOSVer = "Windows .NET Server" End If End Select End Select End Select End Select End If End Function
The GetVersionEx function obtains extended information about the version of the operating system that is currently running.BOOL GetVersionEx(
LPOSVERSIONINFO lpVersionInformation // pointer to version
// information structure
);
Parameters
lpVersionInformation
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 the OSVERSIONINFO data structure to sizeof(OSVERSIONINFO). Windows NT 5.0 and later: This member can be a pointer to an OSVERSIONINFOEX structure. Set the dwOSVersionInfoSize member to sizeof(OSVERSIONINFOEX) to identify the type of structure. Return Values
If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, callGetLastError. The function fails if you specify an invalid value for the dwOSVersionInfoSize member of the OSVERSIONINFO or OSVERSIONINFOEX structure. Res
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 98, use the following test:GetVersionEx (&osvi);
bIsWindows98orLater =
(osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
( (osvi.dwMajorVersion > 4) ||
( (osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion > 0) ) );
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.To determine the best way to test for a feature, refer to the documentation for the feature of interest. The following list discusses some common techniques for feature detection: You can test for the presence of the functions associated with a feature. To test for the presence of a function in a system DLL, call theLoadLibrary function to load the DLL. Then call theGetProcAddress function to determine whether the function of interest is present in the DLL. Use the pointer returned by GetProcAddress to call the function. Note that even if the function is present, it may be a stub that just returns an error code such as ERROR_CALL_NOT_IMPLEMENTED.
You can determine the presence of some features by using the GetSystemMetrics function. For example, you can detect multiple display monitors by calling GetSystemMetrics(SM_CMONITORS).
There are several versions of the redistributable DLLs that implement shell and common control features. For information about determining which versions are present on the system your application is running on, see the topicShell Versions.
Windows CE: The value of the dwPlatformID member of the OSVERSIONINFO structure will be VER_PLATFORM_WIN32_CE.QuickInfo
Windows NT: Requires version 3.5 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.
The following example uses the GetVersionEx function to display the version of the currently running operating system. Relying on version information is not the best way to test for a feature. Instead, refer to the documentation for the feature of interest. For more information on common techniques for feature detection, see Operating System Version. If you must require a particular operating system, be sure to use it as a minimum supported version, rather than design the test for the one operating system. This way, your detection code will continue to work on future versions of Windows. #include <windows.h>
#include <stdio.h>BOOL DisplaySystemVersion()
{
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx; // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
//
// If that fails, try using the OSVERSIONINFO structure. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
{
// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO. osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
return FALSE;
} switch (osvi.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT: // Test for the product. if ( osvi.dwMajorVersion <= 4 )
printf("Microsoft Windows NT "); if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
printf ("Microsoft Windows 2000 "); if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
printf ("Microsoft Windows XP "); // Test for product type. if( bOsVersionInfoEx )
{
if ( osvi.wProductType == VER_NT_WORKSTATION )
{
if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
printf ( "Personal " );
else
printf ( "Professional " );
} else if ( osvi.wProductType == VER_NT_SERVER )
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
printf ( "DataCenter Server " );
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
printf ( "Advanced Server " );
else
printf ( "Server " );
}
}
else
{
HKEY hKey;
char szProductType[80];
DWORD dwBufLen; RegOpenKeyEx( HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
0, KEY_QUERY_VALUE, &hKey );
RegQueryValueEx( hKey, "ProductType", NULL, NULL,
(LPBYTE) szProductType, &dwBufLen);
RegCloseKey( hKey );
if ( lstrcmpi( "WINNT", szProductType) == 0 )
printf( "Professional " );
if ( lstrcmpi( "LANMANNT", szProductType) == 0 )
printf( "Server " );
if ( lstrcmpi( "SERVERNT", szProductType) == 0 )
printf( "Advanced Server " );
} // Display version, service pack (if any), and build number. if ( osvi.dwMajorVersion <= 4 )
{
printf ("version %d.%d %s (Build %d)\n",
osvi.dwMajorVersion,
osvi.dwMinorVersion,
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
}
else
{
printf ("%s (Build %d)\n",
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
}
break; case VER_PLATFORM_WIN32_WINDOWS: if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
{
printf ("Microsoft Windows 95 ");
if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' )
printf("OSR2 " );
} if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
{
printf ("Microsoft Windows 98 ");
if ( osvi.szCSDVersion[1] == 'A' )
printf("SE " );
} if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
{
printf ("Microsoft Windows Me ");
}
break; case VER_PLATFORM_WIN32s: printf ("Microsoft Win32s ");
break;
}
return TRUE;
}
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getting_the_system_version.asp
最新版
能判断Microsoft Windows Server 2003
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFOEX) As Long
Private Declare Function DllGetVersion Lib "Shlwapi.dll" (dwVersion As DllVersionInfo) As Long
Private Type OSVERSIONINFOEX
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
wServicePackMajor As Integer
wServicePackMinor As Integer
wSuiteMask As Integer
wProductType As Byte
wReserved As Byte
End Type
Private Type DllVersionInfo
cbSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
End Type
''used for platform id
Const VER_PLATFORM_WIN32s = 0 ''win 3.x
Const VER_PLATFORM_WIN32_WINDOWS = 1 ''win 9.x
Const VER_PLATFORM_WIN32_NT = 2 ''win nt,2000,XP
''used for product type
Const VER_NT_WORKSTATION = 1
Const VER_NT_SERVER = 3
''used for suite mask
Const VER_SUITE_DATACENTER = 128
Const VER_SUITE_ENTERPRISE = 2
Const VER_SUITE_PERSONAL = 512
Private Const INFINITE = &HFFFFFFFF
Private Const NORMAL_PRIORITY_CLASS = &H20
Private Const WAIT_TIMEOUT = &H102&
Public Function GetOSVer() As String
Dim osv As OSVERSIONINFOEX
osv.dwOSVersionInfoSize = Len(osv)
If GetVersionEx(osv) = 1 Then
Select Case osv.dwPlatformId
Case Is = VER_PLATFORM_WIN32s
GetOSVer = "Windows 3.x"
Case Is = VER_PLATFORM_WIN32_WINDOWS
Select Case osv.dwMinorVersion
Case Is = 0
If InStr(UCase(osv.szCSDVersion), "C") Then
GetOSVer = "Windows 95 OSR2"
Else
GetOSVer = "Windows 95"
End If
Case Is = 10
If InStr(UCase(osv.szCSDVersion), "A") Then
GetOSVer = "Windows 98 SE"
Else
GetOSVer = "Windows 98"
End If
Case Is = 90
GetOSVer = "Windows Me"
End Select
Case Is = VER_PLATFORM_WIN32_NT
Select Case osv.dwMajorVersion
Case Is = 3
Select Case osv.dwMinorVersion
Case Is = 0
GetOSVer = "Windows NT 3"
Case Is = 1
GetOSVer = "Windows NT 3.1"
Case Is = 5
GetOSVer = "Windows NT 3.5"
Case Is = 51
GetOSVer = "Windows NT 3.51"
End Select
Case Is = 4
GetOSVer = "Windows NT 4"
Case Is = 5
Select Case osv.dwMinorVersion
Case Is = 0 ''win 2000
Select Case osv.wProductType
Case Is = VER_NT_WORKSTATION
GetOSVer = "Windows 2000 Professional"
Case Is = VER_NT_SERVER
Select Case osv.wSuiteMask
Case Is = VER_SUITE_DATACENTER
GetOSVer = "Windows 2000 DataCenter Server"
Case Is = VER_SUITE_ENTERPRISE
GetOSVer = "Windows 2000 Advanced Server"
Case Else
GetOSVer = "Windows 2000 Server"
End Select
End Select
Case Is = 1 ''win XP or win .NET server
Select Case osv.wProductType
Case Is = VER_NT_WORKSTATION ''win XP
If osv.wSuiteMask = VER_SUITE_PERSONAL Then
GetOSVer = "Windows XP Home Edition"
Else
GetOSVer = "Windows XP Professional"
End If
Case Else
If osv.wSuiteMask = VER_SUITE_ENTERPRISE Then
GetOSVer = "Windows .NET Enterprise Server"
Else
GetOSVer = "Windows .NET Server"
End If
End Select
End Select
End Select
End Select
End If
End Function