Delphi判断操作系统版本(包括vista等)

Delphi判断操作系统版本(包括vista等)


Windows操作系统大全。包括最新的Longhorn,Vista

type
POSVersionInfoEx = ^TOSVersionInfoEx;
OSVERSIONINFOEXA = 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;

OSVERSIONINFOEXW = record
dwOSVersionInfoSize: DWORD;
dwMajorVersion: DWORD;
dwMinorVersion: DWORD;
dwBuildNumber: DWORD;
dwPlatformId: DWORD;
szCSDVersion: Array[0..127] of WideChar;
wServicePackMajor: WORD;
wServicePackMinor: WORD;
wSuiteMask: WORD;
wProductType: BYTE;
wReserved: BYTE;
end;
OSVERSIONINFOEX = OSVERSIONINFOEXA;
TOSVersionInfoEx = OSVERSIONINFOEX;

const

{ 系统版本 }
VER_NT_WORKSTATION = $00000001;
VER_NT_DOMAIN_CONTROLLER = $00000002;
VER_NT_SERVER = $00000003;

VER_SERVER_NT = $80000000;
VER_WORKSTATION_NT = $40000000;

VER_SUITE_SMALLBUSINESS = $00000001;
VER_SUITE_ENTERPRISE = $00000002;
VER_SUITE_BACKOFFICE = $00000004;
VER_SUITE_COMMUNICATIONS = $00000008;
VER_SUITE_TERMINAL = $00000010;
VER_SUITE_SMALLBUSINESS_RESTRICTED = $00000020;
VER_SUITE_DATACENTER = $00000080;
VER_SUITE_SINGLEUSERTS = $00000100;
VER_SUITE_PERSONAL = $00000200;
VER_SUITE_BLADE = $00000400;
VER_SUITE_STORAGE_SERVER = $00000200;

function TEmtInfo.GetWindowsVersion: string; //取系统版本号
var
osVerInfo : TOSVersionInfoEx;
ExVerExist: Boolean;
ProductType: string;
Begin
Result := 'Microsoft Windows';
ExVerExist := True;
osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);
if not GetVersionEx(POSVersionInfo(@osVerInfo)^) then
begin
osVerInfo.dwOSVersionInfoSize:=SizeOf(TOSVersionInfo);
GetVersionEx(POSVersionInfo(@osVerInfo)^);
ExVerExist := False;
end;
with osVerInfo do
begin
case dwPlatformId of
VER_PLATFORM_WIN32s : Result := Result + Format(' %d.%d',[dwMajorVersion, dwMinorVersion]);
VER_PLATFORM_WIN32_WINDOWS : { Windows 9x/ME }
begin
if (dwMajorVersion = 4) and (dwMinorVersion = 0) then
begin
Result:=Result+' 95';
if szCSDVersion[1] in ['B','C'] then
Result:=Result+' OSR2';
end
else if (dwMajorVersion = 4) and (dwMinorVersion = 10) then
begin
Result := Result + ' 98';
if (osVerInfo.szCSDVersion[1] = 'A') then
Result := Result + ' Second Edition';
end
else if (dwMajorVersion=4) and (dwMinorVersion = 90) Then
Result:=Result + ' Millenium Edition';
end;
VER_PLATFORM_WIN32_NT : { Windows NT/2000 }
begin
case dwMajorVersion of
3, 4: Result:=Result+Format(' NT %d.%d',[dwMajorVersion, dwMinorVersion]);
5: begin
if dwMinorVersion=0 then
Result := Result + ' 2000'
else if dwMinorVersion = 1 then
Result := Result + ' XP'
else if dwMinorVersion = 2 then
Result := Result + ' 2003 Server';
end;
//6: Windows Server "Longhorn" ,Windows Vista
end;
if ExVerExist then
begin
if wProductType = VER_NT_WORKSTATION then
begin
if dwMajorVersion = 4 then
Result := Result + ' Workstation'
else if wSuiteMask and VER_SUITE_PERSONAL <> 0 then
Result := Result + ' Home Edition'
else if dwMajorVersion = 6 then
Result := Result + ' Vista'
else
Result := Result + ' Professional';
end
else if wProductType = VER_NT_SERVER then
begin
if (dwMajorVersion = 5) and (dwMinorVersion = 2) then
begin
if wSuiteMask and VER_SUITE_DATACENTER <> 0 then
Result := Result + ' Datacenter Edition'
else if wSuiteMask and VER_SUITE_ENTERPRISE <> 0 then
Result := Result + ' Enterprise Edition'
else if wSuiteMask and VER_SUITE_BLADE <> 0 then
Result := Result + ' Web Edition'
else if wSuiteMask and VER_SUITE_STORAGE_SERVER <> 0 then
Result := Result + ' R2'
else
Result := Result + ' Standard Edition';
end
else if (dwMajorVersion = 5) and (dwMinorVersion = 0) then
begin
if wSuiteMask and VER_SUITE_DATACENTER <> 0 then
Result := Result + ' Datacenter Server'
else if wSuiteMask and VER_SUITE_ENTERPRISE <> 0 then
Result := Result + ' Advanced Server'
else
Result := Result + ' Server'
end
else if (dwMajorVersion = 6) and (dwMinorVersion = 0) then
begin
if wSuiteMask and VER_SUITE_DATACENTER <> 0 then
Result := Result + 'Server "Longhorn" Datacenter Edition'
else if wSuiteMask and VER_SUITE_ENTERPRISE <> 0 then
Result := Result + 'Server "Longhorn" Enterprise Edition'
end
else
begin
Result:=Result+' Server';
if wSuiteMask and VER_SUITE_ENTERPRISE <> 0 then
Result:=Result+' Enterprise Edition';
end;
end;
end
else
begin
with TRegistry.Create do
begin
try
RootKey:=HKEY_LOCAL_MACHINE;
if OpenKey('\SYSTEM\CurrentControlSet\Control\ProductOptions',False) then
begin
if ValueExists('ProductType') then
begin
ProductType:=ReadString('ProductType');
if SameText(ProductType,'WinNT') then
Result:=Result+' Workstation'
else if SameText(ProductType,'LanManNT') then
Result:=Result+' Server'
else if SameText(ProductType,'ServerNT') then
Result:=Result+' Advance Server';
end;
CloseKey;
end;
finally
Free;
end;
end;
end;
Result:=Result+' '+szCSDVersion;
if (dwMajorVersion=4) and SameText(szCSDVersion,'Service Pack 6') then
begin
with TRegistry.Create do
begin
try
RootKey:=HKEY_LOCAL_MACHINE;
if OpenKey('\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix\Q246009',False) then
begin
Result:=Result+'a';
CloseKey;
end;
finally
Free;
end;
end;
end;
end;
end;
Result := Format(Result + ' (%d.%d Build %d)',[
dwMajorVersion,
dwMinorVersion,
dwBuildNumber and $FFFF]);
end
end;

 

 

unit WinVerUtils;
{
#===============================================================================

# Name:        WinVerUtils.pas
# Author:      Aleksander Oven
# Created:     2007-02-25
# Last Change: 2007-02-25
# Version:     1.0

# Description:

  All about the version of the Windows OS.
  Reference: http://msdn2.microsoft.com/en-us/library/ms724451.aspx

# Warnings and/or special considerations:

  Source code in this file is free for personal and commercial use.

#===============================================================================
}
interface

type
  TWindowsVersion = (
    wvNotRecognized, wvWindows95, wvWindows95OSR2, wvWindows98, wvWindows98SE,
    wvWindowsME, wvWindowsNT, wvWindowsNT35, wvWindowsNT40, wvWindows2000,
    wvWindowsXP, wvWindowsXPSP2, wvWindowsXP64, wvWindowsServer2003,
    wvWindowsVista, wvWindowsServerLonghorn
  );

function GetWindowsVersion: TWindowsVersion;
function GetWindowsName: AnsiString;
function GetWindowsVersionString: AnsiString;

implementation

uses
  Windows, SysUtils;

type
  TOSVersionInfoExA = 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;

const
  cWindowsVersions: array [TWindowsVersion] of AnsiString = (
    'Not recognized', 'Windows 95', 'Windows 95 OSR 2', 'Windows 98',
    'Windows 98 Second Edition', 'Windows Millenium', 'Windows NT',
    'Windows NT 3.5', 'Windows NT 4.0', 'Windows 2000', 'Windows XP',
    'Windows XP Service Pack 2', 'Windows XP x64', 'Windows Server 2003',
    'Windows Vista', 'Windows Server Longhorn'
  );

function GetVersionExA(lpVersionInformation: Pointer): BOOL; stdcall;
  external kernel32 name 'GetVersionExA';

function GetWindowsVersion: TWindowsVersion;
const
  VER_NT_WORKSTATION = $01;
var
  VI: TOSVersionInfoA;
  VIEx: TOSVersionInfoExA;
begin
  Result := wvNotRecognized;

  VI.dwOSVersionInfoSize := SizeOf(TOSVersionInfoA);
  if not GetVersionExA(@VI) then
    Exit;

  case VI.dwPlatformID of
    VER_PLATFORM_WIN32_WINDOWS:
      begin
        case VI.dwMinorVersion of
          0:
            begin
              if (VI.szCSDVersion[1] = 'B') then
                Result := wvWindows95OSR2
              else
                Result := wvWindows95;
            end;
          10:
            begin
              if (VI.szCSDVersion[1] = 'A') then
                Result := wvWindows98SE
              else
                Result := wvWindows98;
            end;
          90:
            begin
              if (VI.dwBuildNumber = $045A0BB8) then
                Result := wvWindowsME;
            end;
        end;
      end;
    VER_PLATFORM_WIN32_NT:
      begin
        case VI.dwMajorVersion of
          3: Result := wvWindowsNT35;
          4: Result := wvWindowsNT40;
        else
          VIEx.dwOSVersionInfoSize := SizeOf(TOSVersionInfoExA);
          if not GetVersionExA(@VIEx) then
            VIEx.dwOSVersionInfoSize := 0;

          case VI.dwMajorVersion of
            5:
              begin
                case VI.dwMinorVersion of
                  0: Result := wvWindows2000;
                  1:
                    begin
                      if (Pos('Service Pack 2', AnsiString(VI.szCSDVersion)) > 0) then
                        Result := wvWindowsXPSP2
                      else
                        Result := wvWindowsXP;
                    end;
                  2:
                    begin
                      Result := wvWindowsXP64;

                      if (VIEx.dwOSVersionInfoSize > 0) and
                        (VIEx.wProductType <> VER_NT_WORKSTATION)
                      then
                        Result := wvWindowsServer2003;
                    end
                else
                  Result := wvWindowsNT;
                end;
              end;
            6:
              begin
                Result := wvWindowsVista;

                if (VIEx.dwOSVersionInfoSize > 0) and
                  (VIEx.wProductType <> VER_NT_WORKSTATION)
                then
                  Result := wvWindowsServerLonghorn;
              end;
          end;
        end;
      end;
  end;
end;

function GetWindowsName: AnsiString;
begin
  Result := cWindowsVersions[GetWindowsVersion];
end;

function GetWindowsVersionString: AnsiString;
var
  VI: TOSVersionInfoA;
begin
  VI.dwOSVersionInfoSize := SizeOf(TOSVersionInfoA);
  if GetVersionExA(@VI) then
    with VI do
      Result := Trim(
        Format(
          '%d.%d build %d %s',
          [dwMajorVersion, dwMinorVersion, dwBuildNumber, szCSDVersion]
        )
      )
  else
    Result := '';
end;

end.


//声明:
GetVersionEx(
  var lpVersionInformation: TOSVersionInfo {参数是一个结构}
): BOOL;						

//结构 TOSVersionInfo 是 _OSVERSIONINFOA 的重命名
_OSVERSIONINFOA = record
  dwOSVersionInfoSize: DWORD;              {结构大小}
  dwMajorVersion: DWORD;                   {主编号}
  dwMinorVersion: DWORD;                   {次编号}
  dwBuildNumber: DWORD;                    {建立编号}
  dwPlatformId: DWORD;                     {作业平台}
  szCSDVersion: array[0..127] of AnsiChar; {更多说明}
end;

{另外参数4(作业平台)可能的取值是:
  VER_PLATFORM_WIN32s = 0;
  VER_PLATFORM_WIN32_WINDOWS = 1;
  VER_PLATFORM_WIN32_NT = 2;
  VER_PLATFORM_WIN32_CE = 3;
  
  我没装 Vista.
}

//举例:
var
  OsInfo: TOSVersionInfo;
const
  s = '主编号: %d' + sLineBreak +
      '次编号: %d' + sLineBreak +
      '建立编号: %d' + sLineBreak +
      '作业平台: %d' + sLineBreak +
      '更多说明: %s';
begin
  OsInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo); {先根据大小要内存}

  GetVersionEx(OsInfo);
  ShowMessage(Format(s, [OsInfo.dwMajorVersion,
                         OsInfo.dwMinorVersion,
                         OsInfo.dwBuildNumber,
                         OsInfo.dwPlatformId,
                         OsInfo.szCSDVersion]));
end;

posted @ 2011-11-05 12:52  findumars  Views(1487)  Comments(0Edit  收藏  举报