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

PeterdeBAsked:
Who is Participating?
 
Geert GruwezConnect With a Mentor Oracle dbaCommented:
and what's wrong with this answer from your previous q ?
function IsWOW64: Boolean;
type
  TIsWow64Process = function(Handle: THandle; var Res: BOOL): BOOL; stdcall;
var
  IsWow64Result: BOOL;              // result from IsWow64Process
  IsWow64Process: TIsWow64Process;  // IsWow64Process fn reference
begin
  Result := False;
  // Try to load required function from kernel32
  IsWow64Process := GetProcAddress(GetModuleHandle('kernel32'), 'IsWow64Process');
  if Assigned(IsWow64Process) then
  begin
    // Function is implemented: call it
    if IsWow64Process(GetCurrentProcess, IsWow64Result) then
    // Return result of function
      Result := IsWow64Result;
  end;
end;

function CheckWindowsOs: String;
const
  { operating system constants }
  cOsUnknown = -1;
  cOsWin95   = 0;
  cOsWin98   = 1;
  cOsWin98SE = 2;
  cOsWinME   = 3;
  cOsWinNT   = 4;
  cOsWin2000 = 5;
  cOsWinXP   = 6;
  cOsWinVista = 7;
  cOsWin2008 = 8;
  cOsWin2008R2 = 9;
  cOsWin7 = 10;
  VER_NT_WORKSTATION = 1;

  OperatingNames: array[-1..10] of string =
    ('Unknown', 'Windows 95', 'Windows 98', 'Windows 98SE', 'Windows ME',
      'Windows NT4', 'Windows 2000', 'Windows XP', 'Windows Vista',
      'Windows 2008', 'Windows 2008 R2', 'Windows 7');
var
  osVerInfo : TOSVersionInfoEx;
  ver: integer;
begin
  ver := cOsUnknown;
  osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);
  if GetVersionEx(osVerInfo) then
  begin
    case osVerInfo.dwPlatformId of
      VER_PLATFORM_WIN32_NT :
        case osVerInfo.dwMajorVersion of
          0..4: ver := cOsWinNT;
          5:
          begin
            ver := cOsWin2000;
            if osVerInfo.dwMinorVersion = 1 then
              ver := cOsWinXP;
          end;
          6:
          begin
            ver := cOsWinVista;
            case osVerInfo.dwMinorVersion of
              0: if osVerInfo.wProductType <> VER_NT_WORKSTATION then
                ver := cOsWin2008;
              1:
              begin
                ver := cOsWin2008R2;
                if osVerInfo.wProductType = VER_NT_WORKSTATION then
                  ver := cOsWin7;
              end;
            end;
          end;
        end;
      VER_PLATFORM_WIN32_WINDOWS:
        case osVerInfo.dwMajorVersion of
          4:
          begin
            ver := cOsWin95;
            case osVerInfo.dwMinorVersion of
              10:
              begin
                ver := cOsWin98;
                if osverInfo.szCSDVersion[1] = 'A' then
                  ver := cOsWin98SE;
              end;
              90: ver := cOsWinME;
            end;
          end;
        end;
    end;
  end;
  Result := OperatingNames[ver];
  if IsWOW64 then
    Result := Result + ' 64bit';
end;

Open in new window

0
 
PeterdeBAuthor Commented:
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

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


UOSInfo.pas

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
BdLmCommented:


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


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



0
 
BdLmConnect With a Mentor Commented:
OK, and here a tested solution, using WIN7 I got the correct result   :;-)

unit Unit_EEWINVERSION;
//   tested unit for experts exchange
interface

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

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

     function IsWOW64: Boolean;
     function CheckWindowsOs: String;

implementation



function IsWOW64: Boolean;
type  TIsWow64Process = function(Handle: THandle; var Res: Boolean): Boolean; stdcall;
var
  IsWow64Result: Boolean;              // result from IsWow64Process
  IsWow64Process: TIsWow64Process;     // IsWow64Process fn reference
begin
  Result := False;
  // Try to load required function from kernel32
  IsWow64Process := GetProcAddress(GetModuleHandle('kernel32'), 'IsWow64Process');
  if Assigned(IsWow64Process) then
  begin
    // Function is implemented: call it
    if IsWow64Process(GetCurrentProcess, IsWow64Result) then
    // Return result of function
      Result := IsWow64Result;
  end;
end;


function CheckWindowsOs: String;
const
  { operating system constants }
  cOsUnknown = -1;
  cOsWin95   = 0;
  cOsWin98   = 1;
  cOsWin98SE = 2;
  cOsWinME   = 3;
  cOsWinNT   = 4;
  cOsWin2000 = 5;
  cOsWinXP   = 6;
  cOsWinVista = 7;
  cOsWin2008 = 8;
  cOsWin2008R2 = 9;
  cOsWin7 = 10;
  VER_NT_WORKSTATION = 1;

  OperatingNames: array[-1..10] of string =
    ('Unknown', 'Windows 95', 'Windows 98', 'Windows 98SE', 'Windows ME',
      'Windows NT4', 'Windows 2000', 'Windows XP', 'Windows Vista',
      'Windows 2008', 'Windows 2008 R2', 'Windows 7');
var
  osVerInfo : TOSVersionInfoEX;
  POSVI     : POSVersionInfo;
  IsExtended: Boolean;

  ver: integer;
begin



 FillChar(osVerInfo, SizeOf(osVerInfo), 0);
  // Get pointer to structure of non-extended type
  // (GetVersionEx requires non-extended structure: we need
  // this pointer to get it to accept our extended structure!!)
  {$TYPEDADDRESS OFF}
  POSVI := @osVerInfo;
  {$TYPEDADDRESS ON}
  // We first try to get extended information
  osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);

  IsExtended := GetVersionEx(POSVI^);




  ver := cOsUnknown;
  osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);


  if IsExtended then
  begin
    case osVerInfo.dwPlatformId of
      VER_PLATFORM_WIN32_NT :
        case osVerInfo.dwMajorVersion of
          0..4: ver := cOsWinNT;
          5:
          begin
            ver := cOsWin2000;
            if osVerInfo.dwMinorVersion = 1 then
              ver := cOsWinXP;
          end;
          6:
          begin
            ver := cOsWinVista;
            case osVerInfo.dwMinorVersion of
              0: if osVerInfo.wProductType <> VER_NT_WORKSTATION then
                ver := cOsWin2008;
              1:
              begin
                ver := cOsWin2008R2;
                if osVerInfo.wProductType = VER_NT_WORKSTATION then
                  ver := cOsWin7;
              end;
            end;
          end;
        end;
      VER_PLATFORM_WIN32_WINDOWS:
        case osVerInfo.dwMajorVersion of
          4:
          begin
            ver := cOsWin95;
            case osVerInfo.dwMinorVersion of
              10:
              begin
                ver := cOsWin98;
                if osverInfo.szCSDVersion[1] = 'A' then
                  ver := cOsWin98SE;
              end;
              90: ver := cOsWinME;
            end;
          end;
        end;
    end;
  end;

  Result := OperatingNames[ver];
  if IsWOW64 then
    Result := Result + ' 64bit';


end;


end.

Open in new window

0
 
PeterdeBAuthor Commented:
@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

0
 
Geert GruwezOracle dbaCommented:
I tried this with delphi 2010.
What version of delphi do have ?
0
 
PeterdeBAuthor Commented:
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...
0
 
BdLmCommented:
@ 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 ?
0
 
PeterdeBAuthor Commented:
Most of all many thanks for your help and quick answers from the both of you
0
 
PeterdeBAuthor Commented:
@Geert > your answer is correct for delphi 2010 therefore you get half the points
0
 
PeterdeBAuthor Commented:
Just compiled the code in 2010 with no adaptions at all, works like a charm thanks
0
All Courses

From novice to tech pro — start learning today.