Solved

Add windows 2008 detection to the checkwindowsos code

Posted on 2010-08-13
12
660 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:PeterdeB
  • 6
  • 4
  • 2
12 Comments
 
LVL 36

Accepted Solution

by:
Geert Gruwez earned 250 total points
ID: 33434486
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
 

Author Comment

by:PeterdeB
ID: 33434787
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
 
LVL 8

Expert Comment

by:BdLm
ID: 33435339
if you can't compile , I think you neede to include the unit 


UOSInfo.pas

Open in new window

0
 
LVL 8

Expert Comment

by:BdLm
ID: 33435397


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


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



0
 
LVL 8

Assisted Solution

by:BdLm
BdLm earned 250 total points
ID: 33435433
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
 

Author Comment

by:PeterdeB
ID: 33435697
@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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33436319
I tried this with delphi 2010.
What version of delphi do have ?
0
 

Author Comment

by:PeterdeB
ID: 33438549
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
 
LVL 8

Expert Comment

by:BdLm
ID: 33439695
@ 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
 

Author Comment

by:PeterdeB
ID: 33439938
Most of all many thanks for your help and quick answers from the both of you
0
 

Author Closing Comment

by:PeterdeB
ID: 33441548
@Geert > your answer is correct for delphi 2010 therefore you get half the points
0
 

Author Comment

by:PeterdeB
ID: 33458267
Just compiled the code in 2010 with no adaptions at all, works like a charm thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now