很多時候從網上找到的取作業系統版本的代碼都不是很準確,有些資訊也不全面。為此,在msdn上找到全面的資訊整理成代碼,共用!
unit WindowsSysVersion;
interface
uses
windows ;
{$IFDEF CONDITIONALEXPRESSIONS}
{$IF Defined(TOSVersionInfoEx)}
{$DEFINE TOSVERSIONINFOEX_DEFINED}
{$IFEND}
{$ENDIF}
{$IFNDEF TOSVERSIONINFOEX_DEFINED}
type
POSVersionInfoEx = ^TOSVersionInfoEx;
TOSVersionInfoEx = packed record
dwOSVersionInfoSize: DWORD;
dwMajorVersion: DWORD;
dwMinorVersion: DWORD;
dwBuildNumber: DWORD;
dwPlatformId: DWORD;
szCSDVersion: array[0..127] of AnsiChar;
wServicePackMajor: Word;
wServicePackMinor: Word;
wSuiteMask: Word;
wProductType: Byte;
wReserved: Byte;
end;
type
TWinVer = (WinNone, Win95, Win98, WinMe, Win2000, WinServer2000, WinXp, WinXp64, WinServer2003, WinHomeServer, WinServer2003R2, WinVista, WinServer2008, WinServer2008R2, Win7);
const
VER_SERVER_NT = $80000000;
{$EXTERNALSYM VER_SERVER_NT}
VER_WORKSTATION_NT = $40000000;
{$EXTERNALSYM VER_WORKSTATION_NT}
VER_SUITE_SMALLBUSINESS = $00000001;
{$EXTERNALSYM VER_SUITE_SMALLBUSINESS}
VER_SUITE_ENTERPRISE = $00000002;
{$EXTERNALSYM VER_SUITE_ENTERPRISE}
VER_SUITE_BACKOFFICE = $00000004;
{$EXTERNALSYM VER_SUITE_BACKOFFICE}
VER_SUITE_COMMUNICATIONS = $00000008;
{$EXTERNALSYM VER_SUITE_COMMUNICATIONS}
VER_SUITE_TERMINAL = $00000010;
{$EXTERNALSYM VER_SUITE_TERMINAL}
VER_SUITE_SMALLBUSINESS_RESTRICTED = $00000020;
{$EXTERNALSYM VER_SUITE_SMALLBUSINESS_RESTRICTED}
VER_SUITE_EMBEDDEDNT = $00000040;
{$EXTERNALSYM VER_SUITE_EMBEDDEDNT}
VER_SUITE_DATACENTER = $00000080;
{$EXTERNALSYM VER_SUITE_DATACENTER}
VER_SUITE_SINGLEUSERTS = $00000100;
{$EXTERNALSYM VER_SUITE_SINGLEUSERTS}
VER_SUITE_PERSONAL = $00000200;
{$EXTERNALSYM VER_SUITE_PERSONAL}
VER_SUITE_BLADE = $00000400;
{$EXTERNALSYM VER_SUITE_BLADE}
VER_SUITE_EMBEDDED_RESTRICTED = $00000800;
{$EXTERNALSYM VER_SUITE_EMBEDDED_RESTRICTED}
VER_SUITE_SECURITY_APPLIANCE = $00001000;
{$EXTERNALSYM VER_SUITE_SECURITY_APPLIANCE}
VER_SUITE_WH_SERVER = $00008000;
{$EXTERNALSYM VER_SUITE_WH_SERVER}
PROCESSOR_ARCHITECTURE_AMD64 = 9;
{$EXTERNALSYM PROCESSOR_ARCHITECTURE_AMD64}
SM_SERVERR2 = 89;
{$EXTERNALSYM SM_SERVERR2}
const
VER_NT_WORKSTATION = $0000001;
{$EXTERNALSYM VER_NT_WORKSTATION}
VER_NT_DOMAIN_CONTROLLER = $0000002;
{$EXTERNALSYM VER_NT_DOMAIN_CONTROLLER}
VER_NT_SERVER = $0000003;
{$EXTERNALSYM VER_NT_SERVER}
{$ENDIF} // TOSVERSIONINFOEX_DEFINED
//取作業系統資訊填充到結構
function GetOSVersionInfo(var Info: TOSVersionInfoEx): Boolean;
//windows系統類別型 0表示取不到 1表示非伺服器 2表示伺服器
function GetWindowsSystemType: integer;
//取windows系統版本資訊,主函數
function GetWindowsSystemVersion: Twinver;
implementation
{
OSVersionInfoEx.wProductType 類型說明
代碼 值 說明
---------------------------------------------------------------------------------------------------------------------------------------
VER_NT_DOMAIN_CONTROLLER 0x0000002 裝的是個網域服務器系統(win2000server,2003server,2008server)
VER_NT_SERVER 0x0000003 裝的是伺服器系統(win2000server,2003server,2008server)
VER_NT_WORKSTATION 0x0000001 非伺服器版本(Vista, XP Professional, XP Home Edition, 2000)
}
//取作業系統類型 0未取到或出錯 1表示非伺服器 2表示伺服器
function GetWindowsSystemType: integer;
var
info: TOSVersionInfoEx;
begin
result := 0;
if (GetOSVersionInfo(info) = false) then exit;
case info.wProductType of
VER_NT_WORKSTATION:
begin
Result := 1; //非伺服器
end;
VER_NT_SERVER:
begin
Result := 2; //伺服器版
end;
VER_NT_DOMAIN_CONTROLLER:
begin
Result := 2; //網域服務器
end;
end;
end;
//系統 版本號碼 其它條件
//-------------------------------------------------------------------------------------------------------
//Windows 7 6 1 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
//Windows Server 2008 R2 6 1 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
//Windows Server 2008 6 0 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
//Windows Vista 6 0 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
//Windows Server 2003 R2 5 2 GetSystemMetrics(SM_SERVERR2) != 0
//Windows Home Server 5 2 OSVERSIONINFOEX.wSuiteMask == VER_SUITE_WH_SERVER
//Windows Server 2003 5 2 GetSystemMetrics(SM_SERVERR2) == 0
//Windows XP x64 Edition 5 2 (OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION) && (SYSTEM_INFO.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
//Windows XP 5 1
//Windows 2000 5 0
//Windows Me 4.9
//Windows 98 4.1
//Windows 95 4.0
//取windows系統版本資訊
function GetWindowsSystemVersion: Twinver;
var
info: TOSVersionInfoEx;
sysInfo: Tsysteminfo;
begin
Result := WinNone;
windows.GetSystemInfo(sysInfo); //系統資訊
try
if (GetOSVersionInfo(info) = false) then exit;
case info.dwMajorVersion of //主要版本
4:
begin
case info.dwMinorVersion of //次版本
0: Result := Win95;
1: Result := Win98;
9: Result := WinMe;
end;
end;
5: begin
case info.dwMinorVersion of
0:
begin
if info.wProductType = VER_NT_WORKSTATION then
Result := Win2000 else Result := WinServer2000;
end;
1: Result := WinXp;
2:
begin
if ((info.wProductType = VER_NT_WORKSTATION) and (sysinfo.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64)) then //PROCESSOR_ARCHITECTURE_AMD64
Result := WinXp64;
//SM_SERVERR2
if GetSystemMetrics(SM_SERVERR2) = 0 then
Result := WinServer2003
else
Result := WinServer2003R2;
if info.wSuiteMask = VER_SUITE_WH_SERVER then
Result := WinHomeServer;
end;
end;
end;
6: begin
case info.dwMinorVersion of
0:
begin
if info.wProductType = VER_NT_WORKSTATION then
Result := WinVista else Result := WinServer2008;
end;
1:
begin
if info.wProductType = VER_NT_WORKSTATION then
Result := Win7 else Result := WinServer2008R2;
end;
end;
end;
end;
except
exit;
end;
end;
function GetOSVersionInfo(var Info: TOSVersionInfoEx): Boolean;
begin
FillChar(Info, SizeOf(TOSVersionInfoEx), 0);
Info.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);
Result := GetVersionEx(TOSVersionInfo(Addr(Info)^));
if (not Result) then
Info.dwOSVersionInfoSize := 0;
end;
end.