简体   繁体   中英

How to get the Windows version is Vista and up versus XP on Delphi?

Is there any way to know which verion of Windows we are working on?

I need to set image to TBitButton in Windows XP and no image in Windows7. It should be done automatically.

Check the SysUtils.Win32MajorVersion (in Delphi 7, you'll need to add SysUtils to your uses clause if it's not there already - later versions add it automatically). The easiest way is to assign the Glyph as usual in the IDE, and clear it if you're running on Vista or higher:

if SysUtils.Win32MajorVersion >= 6 then // Windows Vista or higher
  BitBtn1.Glyph := nil;

For more info on detecting specific Windows editions and versions, see this post . It hasn't been updated for the latest Windows versions and editions, but it'll get you started. You can also search SO for [delphi] GetVersionEx to see other examples.

This is actually a little project of mine - a drop-in component which provides info of the operating system - even preview it in design-time...

unit JDOSInfo;

interface

uses
  Classes, Windows, SysUtils, StrUtils, Forms, Registry;

type
  TJDOSInfo = class(TComponent)
  private
    fReg: TRegistry;
    fKey: String;
    fMinor: Integer;
    fMajor: Integer;
    fBuild: Integer;
    fPlatform: Integer;
    fIsServer: Bool;
    fIs64bit: Bool;
    fProductName: String;
    function GetProductName: String;
    procedure SetProductName(Value: String);
    procedure SetMajor(Value: Integer);
    procedure SetMinor(Value: Integer);
    procedure SetBuild(Value: Integer);
    procedure SetPlatform(Value: Integer);
    procedure SetIs64Bit(const Value: Bool);
    procedure SetIsServer(const Value: Bool);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Major: Integer read fMajor write SetMajor;
    property Minor: Integer read fMinor write SetMinor;
    property Build: Integer read fBuild write SetBuild;
    property Platf: Integer read fPlatform write SetPlatform;
    property ProductName: String read GetProductName write SetProductName;
    property IsServer: Bool read fIsServer write SetIsServer;
    property Is64Bit: Bool read fIs64bit write SetIs64Bit;
  end;

function IsWOW64: Boolean; 
function GetOSInfo: TOSVersionInfo;


procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('JD Custom', [TJDOSInfo]);
end;

function GetOSInfo: TOSVersionInfo;
begin
  FillChar(Result, SizeOf(Result), 0);
  Result.dwOSVersionInfoSize := SizeOf(Result);
  if not GetVersionEx(Result) then
    raise Exception.Create('Error calling GetVersionEx');
end;

function IsWOW64: Boolean;
type
  TIsWow64Process = function( // Type of IsWow64Process API fn
    Handle: THandle;
    var Res: BOOL): BOOL; stdcall;
var
  IsWow64Result: BOOL;              // result from IsWow64Process
  IsWow64Process: TIsWow64Process;  // IsWow64Process fn reference
begin
  // Try to load required function from kernel32
  IsWow64Process:= GetProcAddress(GetModuleHandle('kernel32'),'IsWow64Process');
  if Assigned(IsWow64Process) then
  begin
    // Function is implemented: call it
    if not IsWow64Process(GetCurrentProcess, IsWow64Result) then
      raise Exception.Create('Bad process handle');
    // Return result of function
    Result := IsWow64Result;
  end else
    // Function not implemented: can't be running on Wow64
    Result:= False;
end;

constructor TJDOSInfo.Create(AOwner: TComponent);
var
  Info: TOSVersionInfo;
  Str: String;
begin
  inherited Create(AOwner);
  fReg:= TRegistry.Create(KEY_READ);
  fReg.RootKey:= HKEY_LOCAL_MACHINE;
  fKey:= 'Software\Microsoft\Windows NT\CurrentVersion';  
  fReg.OpenKey(fKey, False);
  Info:= GetOSInfo;
  fMajor:= Info.dwMajorVersion;
  fMinor:= Info.dwMinorVersion;
  fBuild:= Info.dwBuildNumber;
  fIsServer:= False;
  fIs64bit:= False;
  fPlatform:= Info.dwPlatformId;
  if fMajor >= 5 then begin
    //After 2000
    if fReg.ValueExists('ProductName') then
      Str:= fReg.ReadString('ProductName')
    else begin
      Str:= 'Unknown OS: '+IntToStr(fMajor)+'.'+IntToStr(fMinor)+'.'+
        IntToStr(fBuild)+'.'+IntToStr(fPlatform);
    end;      
    if fReg.ValueExists('InstallationType') then begin
      if UpperCase(fReg.ReadString('InstallationType')) = 'SERVER' then
        fIsServer:= True;
    end;
    fIs64bit:= IsWOW64;
    if fIs64bit then
      Str:= Str + ' 64 Bit';
  end else begin
    //Before 2000
    case fMajor of
      4: begin
        case fMinor of
          0: Str:= 'Windows 95';
          10: Str:= 'Windows 98';
          90: Str:= 'Windows ME';
        end;
      end;
      else begin
        Str:= 'Older than 95';
      end;
    end;
  end;
  Self.fProductName:= Str;
end;

destructor TJDOSInfo.Destroy;
begin
  if assigned(fReg) then begin
    if fReg.Active then
      fReg.CloseKey;
    fReg.Free;
  end;
  inherited Destroy;
end;

function TJDOSInfo.GetProductName: String;
begin
  Result:= Self.fProductName;
end;

procedure TJDOSInfo.SetProductName(Value: String);
begin
  //Do Nothing Here!
end;

procedure TJDOSInfo.SetMinor(Value: Integer); 
begin
  //Do Nothing Here!
end;

procedure TJDOSInfo.SetMajor(Value: Integer); 
begin
  //Do Nothing Here!
end;

procedure TJDOSInfo.SetBuild(Value: Integer);  
begin
  //Do Nothing Here!
end;

procedure TJDOSInfo.SetPlatform(Value: Integer); 
begin
  //Do Nothing Here!
end;

procedure TJDOSInfo.SetIs64Bit(const Value: Bool);
begin
  //Do Nothing Here!
end;

procedure TJDOSInfo.SetIsServer(const Value: Bool);
begin
  //Do Nothing Here!
end;

end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM