Solved

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

Posted on 2010-08-13
20
670 Views
Last Modified: 2012-05-10
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
Comment
Question by:PeterdeB
  • 11
  • 6
  • 2
20 Comments
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33428526
windows 7 and Windows 2008

majorver = 6, minorver = 1


are you only interested in 32 bit ?
0
 
LVL 13

Expert Comment

by:aflarin
ID: 33428528
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
 
LVL 13

Accepted Solution

by:
aflarin earned 500 total points
ID: 33428546
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
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33428570
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33428680
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
 

Author Comment

by:PeterdeB
ID: 33428716
@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
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33428725
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
 

Author Closing Comment

by:PeterdeB
ID: 33428734
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
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33428750
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33428763
??? glitch in accepting answers ???
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33428938
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
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33429075
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
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33433817
do you mind re evaluating the comment you accepted
like accepting multiple answers ?
0
 

Author Comment

by:PeterdeB
ID: 33436389
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
 

Author Comment

by:PeterdeB
ID: 33439168
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
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33443777
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
 

Author Comment

by:PeterdeB
ID: 33453581
Ok thank you, so are you ok with this question being closed then, or should i open it?
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33454275
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
 

Author Comment

by:PeterdeB
ID: 33454507
Ok thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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…

759 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

17 Experts available now in Live!

Get 1:1 Help Now