如何确定操作系统

时间:2010-08-10 09:22:30

标签: delphi operating-system

如何实现一个返回操作系统名称的函数?检测运行程序的环境?

win2000 / xp / vista / win7等......?

7 个答案:

答案 0 :(得分:20)

这样的事情:

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;

由于GetVersionExSysUtils.pas子句中有InitPlatformID,因此无需致电initialization。因此,全局常量Win32MajorVersionWin32MinorVersion(以及朋友)将已填充。

答案 1 :(得分:11)

作为Win32 API的替代方案,您可以使用WMI Win32_OperatingSystem类。

您可以编写一个这样的简单函数来检索操作窗口版本名称。

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.

答案 2 :(得分:4)

我在我的程序中使用它:

{$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;

答案 3 :(得分:4)

如果您想获得有关de Operating System的更多信息,可以使用WMI(RRUZ的代码将其用作名称)。有关详细信息,您可以使用GLibWMI的组件 COperatingSystemInfo (来自Sourceforge或Author WebPage(我的);-D)。
所有的图书馆都是免费的,并且可以使用酸性代码。对于OperatingSystem,您可以获得以下属性:

alt text http://a.imageshack.us/img594/1660/coperatinsysteminfo.png

此致

答案 4 :(得分:1)

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操作系统检测。

答案 5 :(得分:0)

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;

答案 6 :(得分:0)

Delphi XE2以来,您可以使用tbody td记录。请注意,它是一个全局记录you cannot create TOSVersion variables

例如:

$('#calendar').fullCalendar({
    eventAfterAllRender: function (view) {

        var row, cell, date;

        // First iterate over each calendar row
        $('.fc-content-skeleton').each(function(i) {
            row = $(this);

            // Now iterate over each header cell within this row
            $('thead td', row).each(function(k) {
                cell = $(this);

                // Get the date attribute from the current thead td
                date = cell.data('date');

                // Find the matching tbody td, at the same index 'k'
                // as our current thead td, and add the data attribute.
                $('tbody td', row).eq(k).data('date', date);

                // Since the data attribute won't be visible in the source, 
                // temporarily add the actual date to the cell to confirm
                // it is really working.  Remove this once you can see it works.
                $('tbody td', row).eq(k).html(date);
            });
        });
    }
});

您可以使用的记录属性很少:var formsubmission = <?php echo base_url() ?> + 'index.php/AJAX/get_item'; TOSVersionlblVersion.Caption := TOSVersion.ToString; MinorMajor一起提供,您可以使用它们来确保操作系统是Windows。

要将系统的当前版本与已定义的版本进行比较,请使用Build版本。