Link to home
Start Free TrialLog in
Avatar of PeterdeB
PeterdeBFlag for Netherlands

asked on

Add windows 2008 detection to the checkwindowsos code

Hi

The program now detects almost every version except for windows 2008, and i cannot add it so that it works. Have tried to incorporate code from a previous question here, but i only got it so far that it will compile without errors, but i dont think it will detect windows 2008, any help please?

Regards
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    mmo1: TMemo;
    mmo2: TMemo;
    mmo3: TMemo;
    mmo4: TMemo;
    lbl1: TLabel;
    lbl2: TLabel;
    lbl3: TLabel;
    lbl4: TLabel;
    hlstsbr1: TStatusBar;
    Button1: TButton;

    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


function CheckWindowsOs: String;
const
  { operating system constants }
  cOsUnknown = -1;
  cOsWin95   = 0;
  cOsWin98   = 1;
  cOsWin98SE = 2;
  cOsWinME   = 3;
  cOsWinNT   = 4;
  cOsWin2000 = 5;
  cOsWin2003 = 6;
  cOsWinXP   = 7;
  cOsWinVista = 8;
  cOsWin7 = 9;
  cOsWin2008 = 10;
  VER_NT_WORKSTATION = 1;
var
  osVerInfo : TOSVersionInfo;
  majorVer, minorVer : Integer;
begin
  result := (IntToStr(cOsUnknown));
  osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
  if GetVersionEx(osVerInfo) then
    begin
      majorVer := osVerInfo.dwMajorVersion;
      minorVer := osVerInfo.dwMinorVersion;
      case osVerInfo.dwPlatformId of

        VER_PLATFORM_WIN32_NT :
          begin
            if majorVer <= 4 then
              result := (IntToStr(cOsWinNT))
            else
              if (majorVer = 5) AND (minorVer= 0) then
                result := (IntToStr(cOsWin2000))
              else
                if (majorVer = 5) AND (minorVer = 1) then
                  result := (IntToStr(cOsWinXP))
               else
              if (majorVer = 5) AND (minorVer= 2) then
                result := (IntToStr(cOsWin2003))
              else
               If (majorVer = 6) AND   (minorVer = 0) then
                  result := (IntToStr(cOsWinVista))
              else
               If (majorVer = 6) AND   (minorVer = 1) then
                 result := (IntToStr(cOsWin7))

          end; {case }



      VER_PLATFORM_WIN32_WINDOWS :
        begin
          if (majorVer = 4) AND (minorVer = 0) then
            result := (IntToStr(cOsWin95))
          else
            if (majorVer = 4) AND (minorVer = 10) then
              begin
                if osVerInfo.szCSDVersion[1] = 'A' then
                  result := (IntToStr(cOsWin98SE))
                else
                   result := (IntToStr(cOsWin98));
                end
              else
                if (majorVer = 4) AND (minorVer = 90) then
                  result := (IntToStr(cOsWinME))
                else
                   result := (IntToStr(cOsUnknown));
        end;
      else
       result := (IntToStr(cOsUnknown));
    end;
  end
else
   result := (IntToStr(cOsUnknown));
end;


{###############################################################################
Currently it will halt right after start on any Windows operating system
and then show a message dialog which, after it has been closed, will shut down
the application, just to show you that the code works
{###############################################################################}
procedure TForm1.FormCreate(Sender: TObject);
 begin
    //case CheckWindowsOs of
    //0..5: if (MessageDlg('Incompatible Operating System! Well not really but this shows a possible warning if the system is not the target system you had in mind!', mtWarning, [mbOK], 0) = mrok) then
    //Application.Terminate; use this to stop the program

 end;
 


procedure TForm1.Button1Click(Sender: TObject);
const
  WinOSNames: array[-1..10] of string =
    ( 'Unknown', 'Windows 95', 'Windows 98', 'Windows 98SE', 'Windows ME',
      'Windows NT4', 'Windows 2000', 'Windows XP', 'Windows 2003', 'Windows Vista', 'Windows 7', 'Windows 2008' );
begin
      ShowMessage(WinOSNames[ StrToInt(CheckWindowsOs) ]);
end;


end.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeterdeB

ASKER

heeey thx for your swift answer, this is the message i can not fix myself yet

var
  osVerInfo : TOSVersionInfoEx;

undeclared identifier TOSVersionInfoEx

Open in new window

if you can't compile , I think you neede to include the unit 


UOSInfo.pas

Open in new window



http://www.delphidabbler.com/articles?article=23&part=3 


the problem come from the fact you are paasing  TOSVersionInfoEx  instead of  TOSVersionInfo



SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@bdLM works like a charm, but what should i do now with the points, since youre using code that was introduced by Geert Gruwen?

@Geert Gruwen, now this question is open to a divide of points, gimme advice please thanks

Add the unit Unit_EEWINVERSION.pas posted by bdLM two posts earlier to the uses clausule

uses
Unit_EEWINVERSION

And then use the code as shown below

showmessage(CheckWindowsOs());

=============================
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Unit_EEWINVERSION, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(CheckWindowsOs());
end;

end.

Open in new window

I tried this with delphi 2010.
What version of delphi do have ?
Sorry Geert, I never even thought about mentioning the version of Delphi, I only have and use Delphi7, this explains the compiling errors, forgive me for my ignorance...
@ PeterdeB :   OK , my part was been to adaption of the solution to D7, any small honor while splitting the points is fine for me :-)



@geert : also OK  for you ?
Most of all many thanks for your help and quick answers from the both of you
@Geert > your answer is correct for delphi 2010 therefore you get half the points
Just compiled the code in 2010 with no adaptions at all, works like a charm thanks