delphi 如何确定操作系统

vsaztqbk  于 2022-11-04  发布在  其他
关注(0)|答案(7)|浏览(291)

如何实现一个返回操作系统名称的函数?2检测程序运行的环境?
win2000/xp/vista/win7等等?

jtjikinw

jtjikinw1#

大概是这样的:

function osver: string;
begin
  result := 'Unknown (Windows ' + IntToStr(Win32MajorVersion) + '.' + IntToStr(Win32MinorVersion) + ')';
  case Win32MajorVersion of
    4:
      case Win32MinorVersion of
        0: result := 'Windows 95';
        10: result := 'Windows 98';
        90: result := 'Windows ME';
      end;
    5:
      case Win32MinorVersion of
        0: result := 'Windows 2000';
        1: result := 'Windows XP';
      end;
    6:
      case Win32MinorVersion of
        0: result := 'Windows Vista';
        1: result := 'Windows 7';
        2: result := 'Windows 8';
        3: result := 'Windows 8.1';
      end;
    10:
      case Win32MinorVersion of
        0: result := 'Windows 10';
      end;
  end;
end;

实际上没有必要调用GetVersionEx,因为SysUtils.pas在它的initialization子句中有InitPlatformID,因此全局常量Win32MajorVersionWin32MinorVersion(以及友元)已经被填充了。

ne5o7dgx

ne5o7dgx2#

作为Win32 API的替代方法,您可以使用WMI Win32_OperatingSystem类。
您可以编写一个类似于下面的简单函数来检索操作Windows版本名称。

function  GetWin32_OSName:string;
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;

  function GetWMIObject(const objectName: String): IDispatch;
  var
    chEaten: Integer;
    BindCtx: IBindCtx;
    Moniker: IMoniker;
  begin
    OleCheck(CreateBindCtx(0, bindCtx));
    OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
    OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
  end;

begin
  objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
  colItems      := objWMIService.ExecQuery('SELECT Caption FROM Win32_OperatingSystem','WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;
  if oEnum.Next(1, colItem, iValue) = 0 then
  Result:=colItem.Caption; //The caption property return an  string  wich includes the operating system version. For example, "Microsoft Windows XP Professional Version = 5.1.2500". 
end;

此外,您还可以使用Win32_OperatingSystem类的其他属性检索有关Windows版本的更多信息。
检查此代码

program GetWMI_Win32_OperatingSystem;

{$APPTYPE CONSOLE}

uses
  SysUtils
  ,ActiveX
  ,ComObj
  ,Variants;

Procedure  GetWin32_OperatingSystem;
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;

  function GetWMIObject(const objectName: String): IDispatch;
  var
    chEaten: Integer;
    BindCtx: IBindCtx;
    Moniker: IMoniker;
  begin
    OleCheck(CreateBindCtx(0, bindCtx));
    OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
    OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
  end;

begin
  objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
  colItems      := objWMIService.ExecQuery('SELECT * FROM Win32_OperatingSystem','WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;
  if oEnum.Next(1, colItem, iValue) = 0 then
  begin
    Writeln('Caption        '+colItem.Caption);
    Writeln('Version        '+colItem.Version);
    Writeln('BuildNumber    '+colItem.BuildNumber);
    Writeln('BuildType      '+colItem.BuildType);
    Writeln('CodeSet        '+colItem.CodeSet);
    Writeln('CountryCode    '+colItem.CountryCode);
    Writeln('BootDevice     '+colItem.BootDevice);
    Writeln;
  end;
end;

function  GetWin32_OSName:string;
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;

  function GetWMIObject(const objectName: String): IDispatch;
  var
    chEaten: Integer;
    BindCtx: IBindCtx;
    Moniker: IMoniker;
  begin
    OleCheck(CreateBindCtx(0, bindCtx));
    OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
    OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
  end;

begin
  objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
  colItems      := objWMIService.ExecQuery('SELECT Caption FROM Win32_OperatingSystem','WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;
  if oEnum.Next(1, colItem, iValue) = 0 then
  Result:=colItem.Caption;
end;

begin
 try
    CoInitialize(nil);
    try
      GetWin32_OperatingSystem;
      //Writeln(GetWin32_OSName);
      Readln;
    finally
    CoUninitialize;
    end;
 except
    on E:Exception do
    Begin
        Writeln(E.Classname, ': ', E.Message);
        Readln;
    End;
  end;
end.
nxagd54h

nxagd54h3#

我在我的程序中使用了以下代码:

{$J+}
TYPE
  TOperatingSystemClass = (osUntested,osUnknown,osWin95,osWin98,osWinME,osWinNT,osWin2000,osWinXP,osWinXPSP1,osWinXPSP2,osWinXPSP3,osWin2003,osWinVista,osWinVistaSP1,osWinVistaSP2,osWinVistaSP3,osWin7,osWin7SP1,osWin7SP2,osWin7SP3,osHigher);

FUNCTION OperatingSystemClass : TOperatingSystemClass;
  CONST
    OSClass     : TOperatingSystemClass = osUntested;

  VAR
    Info        : OSVERSIONINFOEX;
    OldInfo : OSVERSIONINFO ABSOLUTE Info;

  BEGIN
    IF OSClass=osUntested THEN BEGIN
      FillChar(Info,SizeOf(Info),0);
      Info.dwOSVersionInfoSize:=SizeOf(Info); OSClass:=osUnknown;
      IF NOT GetVersionEx(OldInfo) THEN BEGIN
        FillChar(OldInfo,SizeOf(OldInfo),0);
        OldInfo.dwOSVersionInfoSize:=SizeOf(OldInfo);
        GetVersionEx(OldInfo)
      END;
      CASE Info.dwPlatformId OF
        VER_PLATFORM_WIN32_WINDOWS      : CASE Info.dwMajorVersion OF
                                              3 : OSClass:=osWin95;
                                              4 : CASE Info.dwMinorVersion OF
                                                     0 : OSClass:=osWin95;
                                                    10 : OSClass:=osWin98
                                                  ELSE // OTHERWISE //
                                                    OSClass:=osWinME
                                                  END
                                          END;
        VER_PLATFORM_WIN32_NT           : CASE Info.dwMajorVersion OF
                                              5 : CASE Info.dwMinorVersion OF
                                                    0 : OSClass:=osWin2000;
                                                    1 : OSClass:=osWinXP;
                                                    2 : OSClass:=osWin2003
                                                  END;
                                              6 : IF Info.dwMinorVersion=0 THEN
                                                    OSClass:=osWinVista
                                                  ELSE IF Info.dwMinorVersion=1 THEN
                                                    OSClass:=osWin7
                                                  ELSE
                                                    OSClass:=osHigher
                                            END
      END;
      IF (OSClass IN [osWinXP,osWinVista,osWin7]) AND (Info.wServicePackMajor>0) THEN
        INC(OSClass,MAX(Info.wServicePackMajor,3))
    END;
    Result:=OSClass
  END;

其中

type
  OSVERSIONINFOEX = packed record
    dwOSVersionInfoSize: DWORD;
    dwMajorVersion: DWORD;
    dwMinorVersion: DWORD;
    dwBuildNumber: DWORD;
    dwPlatformId: DWORD;
    szCSDVersion: Array [0..127 ] of Char;
    wServicePackMajor: WORD;
    wServicePackMinor: WORD;
    wSuiteMask: WORD;
    wProductType: BYTE;
    wReserved: BYTE;
  End;
  TOSVersionInfoEx = OSVERSIONINFOEX;
  POSVersionInfoEx = ^TOSVersionInfoEx;
gzszwxb4

gzszwxb44#

如果你想获得更多关于操作系统的信息,你可以使用WMI(RRUZ的代码,用它来命名)。更多的信息,你可以使用GLibWMI的组件COperatingSystemInfo(来自Sourceforge或Author WebPage(mine);-D)的规定。
所有的库都是免费的,源代码可用。对于操作系统,您可以获得以下属性:
alt text http://a.imageshack.us/img594/1660/coperatinsysteminfo.png
此致

gkn4icbw

gkn4icbw5#

由于Delphi XE2,您可以使用TOSVersion记录。请注意,它是一个全局记录,并且是you cannot create TOSVersion objects
例如:

lblVersion.Caption := TOSVersion.ToString;

您可以使用的记录属性很少:MinorMajorBuildArchitecturePlatform一起提供,您可以使用它们来确保操作系统是Windows。
要将系统的当前版本与定义的版本进行比较,请使用Check版本。

zd287kbt

zd287kbt6#

JCL包含以下枚举类型:

TWindowsVersion =
   (wvUnknown, wvWin95, wvWin95OSR2, wvWin98, wvWin98SE, wvWinME,
    wvWinNT31, wvWinNT35, wvWinNT351, wvWinNT4, wvWin2000, wvWinXP,
    wvWin2003, wvWinXP64, wvWin2003R2, wvWinVista, wvWinServer2008,
    wvWin7, wvWinServer2008R2);

它包含此函数和其他函数:

function GetWindowsVersion: TWindowsVersion;

Documentation here
更好的是,当Windows的新版本发布时,有人会更新JCL,而您不必自己扩展此代码。此外,在这个问题的所有答案中,除了通常的Windows最终用户版本之外,只有JCL处理Windows Server操作系统检测。

v8wbuo2f

v8wbuo2f7#

function OS_IsWindowsXP: Boolean;
begin
 Result:= (Win32MajorVersion= 5) AND (Win32MinorVersion= 1);
end;

function OS_IsWindowsXPUp: Boolean;
begin
 Result:= ((Win32MajorVersion = 5) AND (Win32MinorVersion>= 1))
        OR (Win32MajorVersion>= 6);
end;

function OS_IsWindowsVista: Boolean;
begin
 Result:= (Win32MajorVersion= 6) AND (Win32MinorVersion= 0);
end;

function OS_IsWindowsVistaUp: Boolean;
begin
 Result:= (Win32MajorVersion>= 6);
end;

function OS_IsWindows7: Boolean;
begin
 Result:= (Win32MajorVersion= 6) AND (Win32MinorVersion= 1);
end;

function OS_IsWindows7Up: Boolean;
begin
 Result:= ((Win32MajorVersion = 6) AND (Win32MinorVersion>= 1))   { Any version 6 equal and above Win7 }
        OR (Win32MajorVersion>= 7);                               { Any version above 6 }
end;

function OS_IsWindows8: Boolean;
begin
 Result:= (Win32MajorVersion = 6) AND (Win32MinorVersion= 2);
end;

function OS_IsWindows8Up: Boolean;
begin
 Result:= ((Win32MajorVersion = 6) AND (Win32MinorVersion>= 2))
        OR (Win32MajorVersion>= 7);
end;

相关问题