• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 733
  • Last Modified:

Detect current operating system and show friendly name instead of integer value

Hi

I have combined code found here, to detect the current operating system uptil windows 7, it works but it returns the integer value like on windows 7= 8

i want it to show something like = Windows 7

And i want to use this code if possible how do i proceed?

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;
  cOsWinXP   = 6;
  cOsWinVista = 7;
  cOsWin7 = 8;
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 < 1) then
                  result := (IntToStr(cOsWinVista))
              else
                  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;



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


end.

Open in new window

0
PeterdeB
Asked:
PeterdeB
  • 11
  • 6
  • 2
1 Solution
 
Geert GruwezOracle dbaCommented:
windows 7 and Windows 2008

majorver = 6, minorver = 1


are you only interested in 32 bit ?
0
 
aflarinCommented:
procedure TForm1.Button1Click(Sender: TObject);
const
  WinOSNames: array[-1..8] of string =
    ( 'Unknown', 'Windows 95', 'Windows 98', 'Windows 98SE', 'Windows ME',
      'Windows NT4', 'Windows 2000', 'Windows XP', 'Windows Vista', 'Windows 7' );
begin
  ShowMessage(WinOSNames[CheckWindowsOs]);
end;
0
 
aflarinCommented:
oops, CheckWindowsOs returns string?
ok, then:

procedure TForm1.Button1Click(Sender: TObject);
const
  WinOSNames: array[-1..8] of string =
    ( 'Unknown', 'Windows 95', 'Windows 98', 'Windows 98SE', 'Windows ME',
      'Windows NT4', 'Windows 2000', 'Windows XP', 'Windows Vista', 'Windows 7' );
begin
  ShowMessage(WinOSNames[ StrToInt(CheckWindowsOs) ]);
end;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Geert GruwezOracle dbaCommented:
0
 
Geert GruwezOracle dbaCommented:
unit Unit4;

interface

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

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

var
  Form4: TForm4;

implementation

{$R *.dfm}

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;
  cOsWin7 = 8;
  cOsWin2008 = 9;
  cOsWin2008R2 = 10;
  VER_NT_WORKSTATION = 1;
var
  osVerInfo : TOSVersionInfoEx;
  majorVer, minorVer : Integer;
begin
  result := (IntToStr(cOsUnknown));
  osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);
  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 >= 6 then
              begin
                if minorver = 0 then
                begin
                  if osverInfo.wProductType = VER_NT_WORKSTATION then
                    result := IntToStr(cOsWinVista)
                  else
                    result := IntToStr(cOsWin2008);
                end else if minorver = 1 then
                begin
                  if osverInfo.wProductType = VER_NT_WORKSTATION then
                    result := IntToStr(cOsWin2008R2)
                  else
                    result := IntToStr(cOsWin7);
                end;
              end;
          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;



procedure TForm4.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Add(CheckWindowsOs);
end;

end.
0
 
PeterdeBAuthor Commented:
@Geert Gruwe thank you very much for your replies, I will build them into the program as well so it is even more complete, and also thanks for your prompt replies
0
 
Geert GruwezOracle dbaCommented:
oops, typo
unit Unit4;

interface

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

type
  TForm4 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    function CheckWindowsOs: String;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

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;
  cOsWin7 = 8;
  cOsWin2008 = 9;
  cOsWin2008R2 = 10;
  VER_NT_WORKSTATION = 1;
var
  osVerInfo : TOSVersionInfoEx;
  majorVer, minorVer : Integer;
begin
  result := (IntToStr(cOsUnknown));
  osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);
  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 >= 6 then
              begin
                //Memo1.Lines.Add('major: ' + IntToStr(majorver));
                //Memo1.Lines.Add('minor: ' + IntToStr(minorver));
                //Memo1.Lines.Add('producttype: ' + IntToStr(osverInfo.wProductType));
                if minorver = 0 then
                begin
                  if osverInfo.wProductType = VER_NT_WORKSTATION then
                    result := IntToStr(cOsWinVista)
                  else
                    result := IntToStr(cOsWin2008);
                end else if minorver = 1 then
                begin
                  if osverInfo.wProductType <> VER_NT_WORKSTATION then
                    result := IntToStr(cOsWin2008R2)
                  else
                    result := IntToStr(cOsWin7);
                end;
              end;
          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;



procedure TForm4.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Add(CheckWindowsOs);
end;

end.

Open in new window

0
 
PeterdeBAuthor Commented:
Thanks for your very fast response, I had been working with the inttostr function but did not succeed, you have solved it instantly, again thanks!
0
 
Geert GruwezOracle dbaCommented:
i checked it on a windows 2008 R2 server

i got windows 7 os, lol

changed

76:              end else if minorver = 1 then
77:                begin
78:                  if osverInfo.wProductType <> VER_NT_WORKSTATION then
79:                    result := IntToStr(cOsWin2008R2)
80:                  else
81:                    result := IntToStr(cOsWin7);
82:                end;
0
 
Geert GruwezOracle dbaCommented:
??? glitch in accepting answers ???
0
 
Geert GruwezOracle dbaCommented:
so you just wanted that string ?

do you mind at least accepting this also as a solution ?
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];
end;

Open in new window

0
 
Geert GruwezOracle dbaCommented:
this also adds 64-bit detection
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
 
Geert GruwezOracle dbaCommented:
do you mind re evaluating the comment you accepted
like accepting multiple answers ?
0
 
PeterdeBAuthor Commented:
Hi

The reason for it, is that my question was = how to display a friendly name like = windows 7
And aflarin gave the correct answer it fixed my problem entirely.

Then i decided to try and add windows 2008 code, I tried to built the code posted in this thread but it did not work, so i started a new question, giving him or anyone else the chance to assist  since i did not think it was fair, to continue this thread with additional questions since the main question had been answered.


 ID: 33428938
ID: 33429075, both do not compile, again also a reason for starting a new question for me.

Personally i dont mind splitting the points at all, but should i then choose one of those two as assist, while they do not work? I did not even think it was allowed to ask additional different questions than the one i have started a thread with, so this is another reason i started a new question

So tell me what to do, in order to make everyone happy

grtz
0
 
PeterdeBAuthor Commented:
Admin


My mistake, I did not mention which version I was using in Delphi, I only have Delphi 7, while Geert Gruwer is using Delphi 2010, so when I thought the code was not correct, it was correct.

Please participants, respond with what you find is the best way to fix this

And accept my apology for not being more clear in regard to the version of Delphi

Grtz
0
 
Geert GruwezOracle dbaCommented:
hey PeterdeB ...
sorry for that,

it was that i put that much effort in a question
i tried to give a bit more than asked, i didn't know it was for delphi 7
i see that bdlm allready posted the solution in declaring the unknown type in your other question

you can allways ask more questions if they are of the same subject
more clarification, more detail, etc
you can even totally thrown around the question
sometimes things come to light in a question, which makes people reverse the logic to solve a problem

this can happen based on a wrong assumption, like me assuming the TOSVersionInfoEx existed in delphi 7
0
 
PeterdeBAuthor Commented:
Ok thank you, so are you ok with this question being closed then, or should i open it?
0
 
Geert GruwezOracle dbaCommented:
you can leave it closed.

i was a little frustrated that putting effort in a lot of questions lately didn't result in points ...
yours was just one in a lot, i guess i was a little frustrated at this, i shouldn't have continued and stepped away.

I did however get a thanx :)  

The good thing is spending attention on accepting answers too.
I have had to learn this myself the hard way too.
It is just a sign of respect to spend time on accepting answers
and explaining why or why not if there is a doubt.
The experts don't allways like it, but they repect it when clarified.
 
I appreciate you putting the effort in clarifying your answer.
That's enough for me.
0
 
PeterdeBAuthor Commented:
Ok thank you
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 11
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now