Solved

Get windows version

Posted on 2009-07-07
18
1,525 Views
Last Modified: 2012-05-07
Hi,

Previously I asked a similar question in:
see: http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_22069225.html?sfQueryTermInfo=1+getsystemtyp+server

I now would like to extend this function by adding Windows Server 2008.
At the below I found out that both Windows Vista an Windows Server 2008 have mayor-version = 6 and minor-version = 0.
see: http://msdn.microsoft.com/en-us/library/ms724832(VS.85).aspx

How can i determine which operatingsystem is actually used as detailed as possible?
Probably Windows 7 can also be detected?

Regards,
Stef
0
Comment
Question by:Delphiwizard
  • 9
  • 5
  • 3
  • +1
18 Comments
 
LVL 2

Expert Comment

by:SectorX4
ID: 24800787
They both have a different product type to differentiate them from each other.

http://www.codeguru.com/cpp/w-p/system/systeminformation/print.php/c8973

You should be able to use that to determine the type.
0
 

Author Comment

by:Delphiwizard
ID: 24800845
How can I get ProductType within Delphi?
Does anybody have a function that gives OS + Windows Edition?
0
 
LVL 6

Expert Comment

by:twocandles
ID: 24801402
Extracted from the link provided by SectorX4: "...on Windows Vista and Windows Server 2008 a new function is available in kernel32.dll, called GetProductInfo(). It retrieves the product type for the operating system on the local computer..."
0
 

Author Comment

by:Delphiwizard
ID: 24801455
But how do I use it from within Delphi?
0
 
LVL 6

Expert Comment

by:twocandles
ID: 24801508
Here you have an example: http://www.delphi3000.com/printarticle.asp?ArticleID=5147

Very important: you need at least Windows Vista for this Windows API call to work, but the code provided in the example takes care of that already
0
 

Author Comment

by:Delphiwizard
ID: 24801647
twocandles:
Your solution seems to cover only Windows Vista and not all other windows versions.

I really prefer one unit/function that covers all.
0
 
LVL 6

Expert Comment

by:twocandles
ID: 24801787
In fact the final solution would be an improvement of your first solution. You use your previous solution to check the Windows major version. If it's below 6 then you already got the result. If it's 6 then you dinamically load GetProductInfo() to check the Product Type and get the result.
0
 
LVL 2

Expert Comment

by:preppydude
ID: 24804252
Hi Delphiwizard.

See if the following works for you/is what you're looking for. If you want it extended further - or changed to not using strings but some kind of enum - please say so.
type

  TMyOSVersionInfo = record

    IsServerEdition:  Boolean;

    Is64BitSystem:    Boolean;

    sCompleteDesc:    String;

    sWindows:         String;

    sServicePack:     String;

    Major:            Cardinal;

    Minor:            Cardinal;

    Build:            Cardinal;

    Special:          Cardinal;

    ServicePackMajor: Cardinal;

    ServicePackMinor: Cardinal;

  end;
 

// ...
 

function GetOSInformation: TMyOSVersionInfo;

var

  SysInfo: TSystemInfo;

  VerInfo: TOSVersionInfoEx;

  bExtendedInfo: Boolean;

const

  PROCESSOR_ARCHITECTURE_AMD64             = $00000009;

  VER_NT_WORKSTATION                       = $00000001;

  VER_NT_DOMAIN_CONTROLLER                 = $00000002;

  VER_NT_SERVER                            = $00000003;

  VER_SUITE_BACKOFFICE                     = $00000004;

  VER_SUITE_BLADE                          = $00000400;

  VER_SUITE_COMPUTE_SERVER                 = $00004000;

  VER_SUITE_DATACENTER                     = $00000080;

  VER_SUITE_ENTERPRISE                     = $00000002;

  VER_SUITE_EMBEDDEDNT                     = $00000040;

  VER_SUITE_PERSONAL                       = $00000200;

  VER_SUITE_SINGLEUSERTS                   = $00000100;

  VER_SUITE_SMALLBUSINESS                  = $00000001;

  VER_SUITE_SMALLBUSINESS_RESTRICTED       = $00000020;

  VER_SUITE_STORAGE_SERVER                 = $00002000;

  VER_SUITE_TERMINAL                       = $00000010;

  VER_SUITE_WH_SERVER                      = $00008000;

begin

  Result.IsServerEdition  := False;

  Result.sWindows         := 'Unknown';

  Result.Special          := 0;

  Result.Major            := 0;

  Result.Minor            := 0;

  Result.Build            := 0;

  Result.ServicePackMajor := 0;

  Result.ServicePackMinor := 0;

  Result.IsServerEdition  := False;

  Result.Is64BitSystem    := (SizeOf(Pointer)=8);
 

  bExtendedInfo := True;

  VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);

  if not (GetVersionEx(VerInfo)) then

  begin

    bExtendedInfo := False;

    VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);

    if not (GetVersionEx(VerInfo)) then VerInfo.dwOSVersionInfoSize := 0;

  end;
 

  if not (VerInfo.dwOSVersionInfoSize=0) then

  begin

    Result.Major := VerInfo.dwMajorVersion;

    Result.Minor := VerInfo.dwMinorVersion;

    Result.Build := VerInfo.dwBuildNumber;

    Result.IsServerEdition := ((VerInfo.dwPlatformId=VER_PLATFORM_WIN32_NT) and not (VerInfo.wProductType=VER_NT_WORKSTATION));

    if (bExtendedInfo) then

    begin

      Result.ServicePackMajor := VerInfo.wServicePackMajor;

      Result.ServicePackMinor := VerInfo.wServicePackMinor;

    end;

    GetSystemInfo(SysInfo);
 

    case VerInfo.dwPlatformId of

      VER_PLATFORM_WIN32s:

        Result.sWindows := 'Windows 3.1';

      VER_PLATFORM_WIN32_WINDOWS:

      begin

        if (Result.Major=4) and (Result.Minor=0) then

          if (VerInfo.szCSDVersion[1]='C') or (VerInfo.szCSDVersion[1]='B') then

            Result.sWindows := 'Windows 95 (Release 2)'

          else

            Result.sWindows := 'Windows 95'

        else if (Result.Major=4) and (Result.Minor=10) and (VerInfo.szCSDVersion[1]='A') then

          Result.sWindows := 'Windows 98 SE'

        else if (Result.Major=4) and (Result.Minor=10) then

          Result.sWindows := 'Windows 98'

        else if (Result.Major=4) and (Result.Minor=90) then

          Result.sWindows := 'Windows ME';

      end;

      VER_PLATFORM_WIN32_NT:

      begin

        if (VerInfo.dwMajorVersion=6) and (VerInfo.dwMinorVersion=0) and (VerInfo.wProductType=VER_NT_WORKSTATION) then

          Result.sWindows := 'Windows Vista'

        else if (VerInfo.dwMajorVersion=6) and (VerInfo.dwMinorVersion=0) and not (VerInfo.wProductType=VER_NT_WORKSTATION) then

          Result.sWindows := 'Windows 2008 Server'

        else if (VerInfo.dwMajorVersion=6) and (VerInfo.dwMinorVersion=1) and (VerInfo.wProductType=VER_NT_WORKSTATION) then

          Result.sWindows := 'Windows 7'

        else if (VerInfo.dwMajorVersion=6) and (VerInfo.dwMinorVersion=1) and not (VerInfo.wProductType=VER_NT_WORKSTATION) then

          Result.sWindows := 'Windows 2008 Server (Release 2)'

        else if (VerInfo.dwMajorVersion=5) and (VerInfo.dwMinorVersion=2) then

        begin

          if (VerInfo.wSuiteMask=VER_SUITE_WH_SERVER) then

            Result.sWindows   := 'Windows Home Server'

          else if (VerInfo.wProductType=VER_NT_WORKSTATION) and (SysInfo.wProcessorArchitecture=PROCESSOR_ARCHITECTURE_AMD64) then

            Result.sWindows       := 'Windows XP Professional x64'

          else

          begin

            if not (GetSystemMetrics(89)=0) then

              Result.sWindows := 'Windows 2003 Server'

            else

              Result.sWindows := 'Windows 2003 Server (Release 2)';

          end;

        end

        else if (VerInfo.dwMajorVersion=5) and (VerInfo.dwMinorVersion=1) and not (VerInfo.wProductType=VER_NT_WORKSTATION) then

          Result.sWindows := 'Windows XP';
 

        if (bExtendedInfo) then

        begin

          if (VerInfo.wProductType=VER_NT_WORKSTATION) and not (SysInfo.wProcessorArchitecture=PROCESSOR_ARCHITECTURE_AMD64) then

          begin

            case VerInfo.dwMajorVersion of

              4: Result.sWindows := 'Windows NT 4.0 Workstation';

              5:

              begin

                if (VerInfo.wSuiteMask and VER_SUITE_PERSONAL<>0) and (VerInfo.dwMinorVersion=1) then

                  Result.sWindows := 'Windows XP Home'

                else

                  if (VerInfo.dwMinorVersion=1) then

                    Result.sWindows := 'Windows XP Professional'

                  else

                    Result.sWindows := 'Windows 2000 Professional';

              end;

              6: if (VerInfo.wSuiteMask and VER_SUITE_PERSONAL<>0) then Result.sWindows := 'Windows Vista Home';

            end;

          end

          else if (VerInfo.wProductType=VER_NT_SERVER) or (VerInfo.wProductType=VER_NT_DOMAIN_CONTROLLER) then

          begin

            if (VerInfo.dwMajorVersion=6) and (VerInfo.dwMinorVersion=0) then

            begin

              if (VerInfo.wSuiteMask and VER_SUITE_DATACENTER<>0) then

                Result.sWindows := 'Windows 2008 Server Datacenter'

              else if (VerInfo.wSuiteMask and VER_SUITE_ENTERPRISE<>0) then

                Result.sWindows := 'Windows 2008 Server Enterprise'

              else

                Result.sWindows := 'Windows 2008 Server'

            end

            else if (VerInfo.dwMajorVersion=5) and (VerInfo.dwMinorVersion=2) then

            begin

              if (VerInfo.wSuiteMask and VER_SUITE_DATACENTER<>0) then

                Result.sWindows := 'Windows 2003 Server Datacenter'

              else if (VerInfo.wSuiteMask and VER_SUITE_ENTERPRISE<>0) then

                Result.sWindows := 'Windows 2003 Server Enterprise'

              else if (VerInfo.wSuiteMask and VER_SUITE_BLADE<>0) then

                Result.sWindows := 'Windows 2003 Server Web Edition'

              else

                Result.sWindows := 'Windows 2003 Server';

            end

            else if (VerInfo.dwMajorVersion=5) and (VerInfo.dwMinorVersion=0) then

            begin

              if (VerInfo.wSuiteMask and VER_SUITE_DATACENTER<>0) then

                Result.sWindows := 'Windows 2000 Server Datacenter'

              else if (VerInfo.wSuiteMask and VER_SUITE_ENTERPRISE<>0) then

                Result.sWindows := 'Windows 2000 Server Enterprise'

              else if (VerInfo.wSuiteMask and VER_SUITE_BLADE<>0) then

                Result.sWindows := 'Windows 2000 Server Web Edition'

              else

                Result.sWindows := 'Windows 2000 Server';

            end

            else

            begin

              if (VerInfo.wSuiteMask and VER_SUITE_DATACENTER<>0) then

                Result.sWindows := 'Windows NT 4.0 Server Datacenter'

              else if (VerInfo.wSuiteMask and VER_SUITE_ENTERPRISE<>0) then

                Result.sWindows := 'Windows NT 4.0 Server Enterprise'

              else if (VerInfo.wSuiteMask and VER_SUITE_BLADE<>0) then

                Result.sWindows := 'Windows NT 4.0 Server Web Edition'

              else

                Result.sWindows := 'Windows NT 4.0 Server';

            end;

          end;

        end;

      end;

      else

        Result.sWindows := Format('Unknown Platform ID (%d)', [VerInfo.dwPlatformId]);

    end;

  end;
 

  Result.sServicePack  := Format('%d.%d', [Result.ServicePackMajor, Result.ServicePackMinor]);

  Result.sCompleteDesc := Format('%s (Build: %d', [Result.sWindows, Result.Build]);

  if not (Result.ServicePackMajor=0) then

    Result.sCompleteDesc := Result.sCompleteDesc + Format(' - Service Pack: %s', [Result.sServicePack]);

  Result.sCompleteDesc := Result.sCompleteDesc + ')';

end;

Open in new window

0
 

Author Comment

by:Delphiwizard
ID: 24805435
preppydude:
Your solution looks very promissing. Please give me some time te test it.

What I do miss is the division of Windows 7 into separate Editions.
But maybe this isn't available yet?
0
Do email signature updates give you a headache?

Do you feel like all of your time is spent managing email signatures? Too busy to visit every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

 
LVL 2

Expert Comment

by:preppydude
ID: 24805450
I'll get to that when i get home again.
0
 
LVL 2

Expert Comment

by:preppydude
ID: 24806217
@Delphiwizard:
This one should recognize various editions on Windows 2008 Server, Windows Vista and Windows 7. I haven't been able to test it, though, as I don't have any of them.
function GetOSInformation: TMyOSVersionInfo;

var

  SysInfo: TSystemInfo;

  VerInfo: TOSVersionInfoEx;

  bExtendedInfo: Boolean;

  // Only available on Windows 2008, Vista, 7 (or better)

  GetProductInfo: function(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion, dwSpMinorVersion: DWORD; pdwReturnedProductType: PDWORD): BOOL; WINAPI;

  dwMajor:        DWORD;

  dwMinor:        DWORD;

  dwSpMajor:      DWORD;

  dwSpMinor:      DWORD;

  pdwProductType: PDWORD;

const

  PROCESSOR_ARCHITECTURE_AMD64             = $00000009;

  VER_NT_WORKSTATION                       = $00000001;

  VER_NT_DOMAIN_CONTROLLER                 = $00000002;

  VER_NT_SERVER                            = $00000003;

  VER_SUITE_BACKOFFICE                     = $00000004;

  VER_SUITE_BLADE                          = $00000400;

  VER_SUITE_COMPUTE_SERVER                 = $00004000;

  VER_SUITE_DATACENTER                     = $00000080;

  VER_SUITE_ENTERPRISE                     = $00000002;

  VER_SUITE_EMBEDDEDNT                     = $00000040;

  VER_SUITE_PERSONAL                       = $00000200;

  VER_SUITE_SINGLEUSERTS                   = $00000100;

  VER_SUITE_SMALLBUSINESS                  = $00000001;

  VER_SUITE_SMALLBUSINESS_RESTRICTED       = $00000020;

  VER_SUITE_STORAGE_SERVER                 = $00002000;

  VER_SUITE_TERMINAL                       = $00000010;

  VER_SUITE_WH_SERVER                      = $00008000;

begin

  Result.IsServerEdition  := False;

  Result.sWindows         := 'Unknown';

  Result.Special          := 0;

  Result.Major            := 0;

  Result.Minor            := 0;

  Result.Build            := 0;

  Result.ServicePackMajor := 0;

  Result.ServicePackMinor := 0;

  Result.IsServerEdition  := False;

  Result.Is64BitSystem    := (SizeOf(Pointer)=8);
 

  bExtendedInfo := True;

  VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);

  if not (GetVersionEx(VerInfo)) then

  begin

    bExtendedInfo := False;

    VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);

    if not (GetVersionEx(VerInfo)) then VerInfo.dwOSVersionInfoSize := 0;

  end;
 

  if not (VerInfo.dwOSVersionInfoSize=0) then

  begin

    Result.Major := VerInfo.dwMajorVersion;

    Result.Minor := VerInfo.dwMinorVersion;

    Result.Build := VerInfo.dwBuildNumber;

    Result.IsServerEdition := ((VerInfo.dwPlatformId=VER_PLATFORM_WIN32_NT) and not (VerInfo.wProductType=VER_NT_WORKSTATION));

    if (bExtendedInfo) then

    begin

      Result.ServicePackMajor := VerInfo.wServicePackMajor;

      Result.ServicePackMinor := VerInfo.wServicePackMinor;

    end;

    GetSystemInfo(SysInfo);
 

    case VerInfo.dwPlatformId of

      VER_PLATFORM_WIN32s:

        Result.sWindows := 'Windows 3.1';

      VER_PLATFORM_WIN32_WINDOWS:

      begin

        if (Result.Major=4) and (Result.Minor=0) then

          if (VerInfo.szCSDVersion[1]='C') or (VerInfo.szCSDVersion[1]='B') then

            Result.sWindows := 'Windows 95 (Release 2)'

          else

            Result.sWindows := 'Windows 95'

        else if (Result.Major=4) and (Result.Minor=10) and (VerInfo.szCSDVersion[1]='A') then

          Result.sWindows := 'Windows 98 SE'

        else if (Result.Major=4) and (Result.Minor=10) then

          Result.sWindows := 'Windows 98'

        else if (Result.Major=4) and (Result.Minor=90) then

          Result.sWindows := 'Windows ME';

      end;

      VER_PLATFORM_WIN32_NT:

      begin

        if (VerInfo.wProductType=VER_NT_WORKSTATION) then

        begin

          if (VerInfo.dwMajorVersion=6) then

          begin

            if (VerInfo.dwMinorVersion=1) then

              Result.sWindows := 'Windows 7'

            else if (VerInfo.dwMinorVersion=0) then

              Result.sWindows := 'Windows Vista';
 

            GetProductInfo := GetProcAddress(GetModuleHandle('kernel32.dll'), 'GetProductInfo');

            GetProductInfo(dwMajor, dwMinor, dwSpMajor, dwSpMinor, pdwProductType);
 

            case DWORD(pdwProductType) of

              1: Result.sWindows := Format('%s %s', [Result.sWindows, 'Ultimate']);

              2: Result.sWindows := Format('%s %s', [Result.sWindows, 'Home Basic']);

              3: Result.sWindows := Format('%s %s', [Result.sWindows, 'Premium']);

              4: Result.sWindows := Format('%s %s', [Result.sWindows, 'Enterprise']);

              5: Result.sWindows := Format('%s %s', [Result.sWindows, 'Home Basic N']);

              6: Result.sWindows := Format('%s %s', [Result.sWindows, 'Business']);

              11: Result.sWindows := Format('%s %s', [Result.sWindows, 'Starter']);

              16: Result.sWindows := Format('%s %s', [Result.sWindows, 'Business N']);

              26: Result.sWindows := Format('%s %s', [Result.sWindows, 'Premium N']);

              27: Result.sWindows := Format('%s %s', [Result.sWindows, 'Enterprise N']);

              28: Result.sWindows := Format('%s %s', [Result.sWindows, 'Ultimate N']);

              else Result.sWindows := Format('%s %s', [Result.sWindows, '(unknown edition)']);

            end;

          end

          else if (VerInfo.dwMajorVersion=5) then

          begin

            if (VerInfo.dwMinorVersion=2) and (SysInfo.wProcessorArchitecture=PROCESSOR_ARCHITECTURE_AMD64) then

              Result.sWindows := 'Windows XP Professional x64'

            else if (VerInfo.wSuiteMask and VER_SUITE_PERSONAL<>0) and (VerInfo.dwMinorVersion=1) then

              Result.sWindows := 'Windows XP Home'

            else

              if (VerInfo.dwMinorVersion=1) then

                Result.sWindows := 'Windows XP Professional'

              else

                Result.sWindows := 'Windows 2000 Professional';

          end

          else

            Result.sWindows := Format('Windows NT %d.%d', [VerInfo.dwMajorVersion, VerInfo.dwMinorVersion]);

        end else begin

          if (VerInfo.dwMajorVersion=6) and (VerInfo.dwMinorVersion=0) then

          begin

            Result.sWindows := 'Windows 2008';
 

            GetProductInfo := GetProcAddress(GetModuleHandle('kernel32.dll'), 'GetProductInfo');

            GetProductInfo(dwMajor, dwMinor, dwSpMajor, dwSpMinor, pdwProductType);
 

            case DWORD(pdwProductType) of

              7: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Standard']);

              8: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Datacenter']);

              10: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Enterprise']);

              12: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Datacenter']);

              13: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Standard']);

              14: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Enterprise']);

              15: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Enterprise IA64']);

              17: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Web']);

              else Result.sWindows := Result.sWindows + ' Server (unknown edition)';

            end;

          end

          else if (VerInfo.dwMajorVersion=5) and (VerInfo.dwMinorVersion=2) then

          begin

            if (VerInfo.wSuiteMask and VER_SUITE_DATACENTER<>0) then

              Result.sWindows := 'Windows 2003 Server Datacenter'

            else if (VerInfo.wSuiteMask and VER_SUITE_ENTERPRISE<>0) then

              Result.sWindows := 'Windows 2003 Server Enterprise'

            else if (VerInfo.wSuiteMask and VER_SUITE_BLADE<>0) then

              Result.sWindows := 'Windows 2003 Server Web Edition'

            else

              Result.sWindows := 'Windows 2003 Server';

          end

          else if (VerInfo.dwMajorVersion=5) and (VerInfo.dwMinorVersion=2) then

          begin

            if (VerInfo.wSuiteMask=VER_SUITE_WH_SERVER) then

              Result.sWindows := 'Windows Home Server'

            else

            begin

              if not (GetSystemMetrics(89)=0) then

                Result.sWindows := 'Windows 2003 Server'

              else

                Result.sWindows := 'Windows 2003 Server (Release 2)';

            end;

          end

          else if (VerInfo.dwMajorVersion=5) and (VerInfo.dwMinorVersion=0) then

          begin

            if (VerInfo.wSuiteMask and VER_SUITE_DATACENTER<>0) then

              Result.sWindows := 'Windows 2000 Server Datacenter'

            else if (VerInfo.wSuiteMask and VER_SUITE_ENTERPRISE<>0) then

              Result.sWindows := 'Windows 2000 Server Enterprise'

            else if (VerInfo.wSuiteMask and VER_SUITE_BLADE<>0) then

              Result.sWindows := 'Windows 2000 Server Web Edition'

            else

              Result.sWindows := 'Windows 2000 Server';

          end

          else

          begin

            if (VerInfo.wSuiteMask and VER_SUITE_DATACENTER<>0) then

              Result.sWindows := 'Windows NT 4.0 Server Datacenter'

            else if (VerInfo.wSuiteMask and VER_SUITE_ENTERPRISE<>0) then

              Result.sWindows := 'Windows NT 4.0 Server Enterprise'

            else if (VerInfo.wSuiteMask and VER_SUITE_BLADE<>0) then

              Result.sWindows := 'Windows NT 4.0 Server Web Edition'

            else

              Result.sWindows := 'Windows NT 4.0 Server';

          end;

        end;

      end;

      else

        Result.sWindows := Format('Unknown Platform ID (%d)', [VerInfo.dwPlatformId]);

    end;

  end;
 

  Result.sServicePack  := Format('%d.%d', [Result.ServicePackMajor, Result.ServicePackMinor]);

  Result.sCompleteDesc := Format('%s (Build: %d', [Result.sWindows, Result.Build]);

  if not (Result.ServicePackMajor=0) then Result.sCompleteDesc := Result.sCompleteDesc + Format(' - Service Pack: %s', [Result.sServicePack]);

  Result.sCompleteDesc := Result.sCompleteDesc + ')';

end;

Open in new window

0
 

Author Comment

by:Delphiwizard
ID: 24811197
peppydude:
I've tried your code, but it isn't working yet.
-> VerInfo: TOSVersionInfoEx;
isn't recognized.

If i change it to  
-> VerInfo: TOSVersionInfo;
it is recognized, but then
-> VerInfo.wServicePackMajor
-> VerInfo.wServicePackMinor
-> VerInfo.wProductType
-> VerInfo.wSuiteMask
are undeclared
0
 

Author Comment

by:Delphiwizard
ID: 24811384
Pervious problem is solved:
Adding the declaration in the code-part solves it.

Now I just need to know how to use it.
How do I get one or more values by f.e. clicking on a button?

procedure TForm1.Button1Click(Sender: TObject);
begin
  // get Result.sWindows
end;

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;

Open in new window

0
 

Author Comment

by:Delphiwizard
ID: 24811509
Another change in the code in order to get it working:
  if not (GetVersionEx(VerInfo)) then

  begin

    bExtendedInfo := False;

    VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);

    if not (GetVersionEx(VerInfo)) then

      VerInfo.dwOSVersionInfoSize := 0;

  end;
 

MUST BE:
 

  if not GetVersionEx(TOSVersionInfo(Addr(VerInfo)^)) then

  begin

    bExtendedInfo := False;

    VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);

    if not GetVersionEx(TOSVersionInfo(Addr(VerInfo)^)) then

      VerInfo.dwOSVersionInfoSize := 0;

  end;

Open in new window

0
 
LVL 2

Accepted Solution

by:
preppydude earned 500 total points
ID: 24812814
@Delphiwizard:
It seems weird that the code isn't working out for you. I've tested it on both my brother's and dad's. It worked just fine. Maybe I didn't give you the few changes I made, but, still ... I don't get it.

The OSVERSIONINFOEX struct should be in your Windows.pas file installed with Delphi. I've attached the struct and function coming with my Delphi 2009 release.

Anyways, the code attached has been tested on 4 different systems: Vista Ultimate, Vista Home, Vista Enterprise and Windows XP Professional. It showed both the suite version and installed service pack correctly.
// The struct used:

type

  TOSVersionInfoEx = record

    dwOSVersionInfoSize: DWORD;

    dwMajorVersion: DWORD;

    dwMinorVersion: DWORD;

    dwBuildNumber: DWORD;

    dwPlatformId: DWORD;

    szCSDVersion: array[0..127] of WideChar; { Maintenance UnicodeString for PSS usage }

    wServicePackMajor: WORD;

    wServicePackMinor: WORD;

    wSuiteMask: WORD;

    wProductType: BYTE;

    wReserved:BYTE;

  end;
 

// ... and the function (GetVersionEx):
 

function GetVersionEx(var lpVersionInformation: TOSVersionInfoEx): BOOL; external kernel32 name 'GetVersionExW';
 

// ... and finally the working function I've tested:
 

function GetOSInformation: TMyOSVersionInfo;

var

  SysInfo: TSystemInfo;

  VerInfo: TOSVersionInfoEx;

  bExtendedInfo: Boolean;

  // Only available on Windows 2008, Vista, 7 (or better)

  GetProductInfo: function(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion, dwSpMinorVersion: DWORD; pdwReturnedProductType: PDWORD): BOOL; WINAPI;

  dwMajor:        DWORD;

  dwMinor:        DWORD;

  dwSpMajor:      DWORD;

  dwSpMinor:      DWORD;

  dwProductType:  DWORD;

const

  PROCESSOR_ARCHITECTURE_AMD64             = $00000009;

  VER_NT_WORKSTATION                       = $00000001;

  VER_NT_DOMAIN_CONTROLLER                 = $00000002;

  VER_NT_SERVER                            = $00000003;

  VER_SUITE_BACKOFFICE                     = $00000004;

  VER_SUITE_BLADE                          = $00000400;

  VER_SUITE_COMPUTE_SERVER                 = $00004000;

  VER_SUITE_DATACENTER                     = $00000080;

  VER_SUITE_ENTERPRISE                     = $00000002;

  VER_SUITE_EMBEDDEDNT                     = $00000040;

  VER_SUITE_PERSONAL                       = $00000200;

  VER_SUITE_SINGLEUSERTS                   = $00000100;

  VER_SUITE_SMALLBUSINESS                  = $00000001;

  VER_SUITE_SMALLBUSINESS_RESTRICTED       = $00000020;

  VER_SUITE_STORAGE_SERVER                 = $00002000;

  VER_SUITE_TERMINAL                       = $00000010;

  VER_SUITE_WH_SERVER                      = $00008000;

begin

  Result.IsServerEdition  := False;

  Result.sWindows         := 'Unknown';

  Result.Special          := 0;

  Result.Major            := 0;

  Result.Minor            := 0;

  Result.Build            := 0;

  Result.ServicePackMajor := 0;

  Result.ServicePackMinor := 0;

  Result.IsServerEdition  := False;

  Result.Is64BitSystem    := (SizeOf(Pointer)=8);
 

  bExtendedInfo := True;

  VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);

  if not (GetVersionEx(VerInfo)) then

  begin

    bExtendedInfo := False;

    VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);

    if not (GetVersionEx(VerInfo)) then VerInfo.dwOSVersionInfoSize := 0;

  end;
 

  if not (VerInfo.dwOSVersionInfoSize=0) then

  begin

    Result.Major := VerInfo.dwMajorVersion;

    Result.Minor := VerInfo.dwMinorVersion;

    Result.Build := VerInfo.dwBuildNumber;

    Result.IsServerEdition := ((VerInfo.dwPlatformId=VER_PLATFORM_WIN32_NT) and not (VerInfo.wProductType=VER_NT_WORKSTATION));

    if (bExtendedInfo) then

    begin

      Result.ServicePackMajor := VerInfo.wServicePackMajor;

      Result.ServicePackMinor := VerInfo.wServicePackMinor;

    end;

    GetSystemInfo(SysInfo);
 

    case VerInfo.dwPlatformId of

      VER_PLATFORM_WIN32s:

        Result.sWindows := 'Windows 3.1';

      VER_PLATFORM_WIN32_WINDOWS:

      begin

        if (Result.Major=4) and (Result.Minor=0) then

          if (VerInfo.szCSDVersion[1]='C') or (VerInfo.szCSDVersion[1]='B') then

            Result.sWindows := 'Windows 95 (Release 2)'

          else

            Result.sWindows := 'Windows 95'

        else if (Result.Major=4) and (Result.Minor=10) and (VerInfo.szCSDVersion[1]='A') then

          Result.sWindows := 'Windows 98 SE'

        else if (Result.Major=4) and (Result.Minor=10) then

          Result.sWindows := 'Windows 98'

        else if (Result.Major=4) and (Result.Minor=90) then

          Result.sWindows := 'Windows ME';

      end;

      VER_PLATFORM_WIN32_NT:

      begin

        if (VerInfo.wProductType=VER_NT_WORKSTATION) then

        begin

          if (VerInfo.dwMajorVersion=6) then

          begin

            if (VerInfo.dwMinorVersion=1) then

              Result.sWindows := 'Windows 7'

            else if (VerInfo.dwMinorVersion=0) then

              Result.sWindows := 'Windows Vista';
 

            GetProductInfo := GetProcAddress(GetModuleHandle('kernel32.dll'), 'GetProductInfo');

            GetProductInfo(dwMajor, dwMinor, dwSpMajor, dwSpMinor, @dwProductType);
 

            case dwProductType of

              1: Result.sWindows := Format('%s %s', [Result.sWindows, 'Ultimate']);

              2: Result.sWindows := Format('%s %s', [Result.sWindows, 'Home Basic']);

              3: Result.sWindows := Format('%s %s', [Result.sWindows, 'Premium']);

              4: Result.sWindows := Format('%s %s', [Result.sWindows, 'Enterprise']);

              5: Result.sWindows := Format('%s %s', [Result.sWindows, 'Home Basic N']);

              6: Result.sWindows := Format('%s %s', [Result.sWindows, 'Business']);

              11: Result.sWindows := Format('%s %s', [Result.sWindows, 'Starter']);

              16: Result.sWindows := Format('%s %s', [Result.sWindows, 'Business N']);

              26: Result.sWindows := Format('%s %s', [Result.sWindows, 'Premium N']);

              27: Result.sWindows := Format('%s %s', [Result.sWindows, 'Enterprise N']);

              28: Result.sWindows := Format('%s %s', [Result.sWindows, 'Ultimate N']);

              else Result.sWindows := Format('%s %s', [Result.sWindows, '(unknown edition)']);

            end;

          end

          else if (VerInfo.dwMajorVersion=5) then

          begin

            if (VerInfo.dwMinorVersion=2) and (SysInfo.wProcessorArchitecture=PROCESSOR_ARCHITECTURE_AMD64) then

              Result.sWindows := 'Windows XP Professional x64'

            else if (bExtendedInfo) and (VerInfo.wSuiteMask and VER_SUITE_PERSONAL<>0) and (VerInfo.dwMinorVersion=1) then

              Result.sWindows := 'Windows XP Home'

            else

              if (VerInfo.dwMinorVersion=1) then

                Result.sWindows := 'Windows XP Professional'

              else

                Result.sWindows := 'Windows 2000 Professional';

          end

          else

            Result.sWindows := Format('Windows NT %d.%d', [VerInfo.dwMajorVersion, VerInfo.dwMinorVersion]);

        end else begin

          if (VerInfo.dwMajorVersion=6) and (VerInfo.dwMinorVersion=0) then

          begin

            Result.sWindows := 'Windows 2008';
 

            GetProductInfo := GetProcAddress(GetModuleHandle('kernel32.dll'), 'GetProductInfo');

            GetProductInfo(dwMajor, dwMinor, dwSpMajor, dwSpMinor, @dwProductType);
 

            case dwProductType of

              7: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Standard']);

              8: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Datacenter']);

              10: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Enterprise']);

              12: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Datacenter']);

              13: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Standard']);

              14: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Enterprise']);

              15: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Enterprise IA64']);

              17: Result.sWindows := Format('%s %s Server', [Result.sWindows, 'Web']);

              else Result.sWindows := Result.sWindows + ' Server (unknown edition)';

            end;

          end

          else if (VerInfo.dwMajorVersion=5) and (VerInfo.dwMinorVersion=2) then

          begin

            if (bExtendedInfo) and (VerInfo.wSuiteMask and VER_SUITE_DATACENTER<>0) then

              Result.sWindows := 'Windows 2003 Server Datacenter'

            else if (bExtendedInfo) and (VerInfo.wSuiteMask and VER_SUITE_ENTERPRISE<>0) then

              Result.sWindows := 'Windows 2003 Server Enterprise'

            else if (bExtendedInfo) and (VerInfo.wSuiteMask and VER_SUITE_BLADE<>0) then

              Result.sWindows := 'Windows 2003 Server Web Edition'

            else

              Result.sWindows := 'Windows 2003 Server';

          end

          else if (VerInfo.dwMajorVersion=5) and (VerInfo.dwMinorVersion=2) then

          begin

            if (bExtendedInfo) and (VerInfo.wSuiteMask=VER_SUITE_WH_SERVER) then

              Result.sWindows := 'Windows Home Server'

            else

            begin

              if not (GetSystemMetrics(89)=0) then

                Result.sWindows := 'Windows 2003 Server'

              else

                Result.sWindows := 'Windows 2003 Server (Release 2)';

            end;

          end

          else if (VerInfo.dwMajorVersion=5) and (VerInfo.dwMinorVersion=0) then

          begin

            if (bExtendedInfo) and (VerInfo.wSuiteMask and VER_SUITE_DATACENTER<>0) then

              Result.sWindows := 'Windows 2000 Server Datacenter'

            else if (bExtendedInfo) and (VerInfo.wSuiteMask and VER_SUITE_ENTERPRISE<>0) then

              Result.sWindows := 'Windows 2000 Server Enterprise'

            else if (bExtendedInfo) and (VerInfo.wSuiteMask and VER_SUITE_BLADE<>0) then

              Result.sWindows := 'Windows 2000 Server Web Edition'

            else

              Result.sWindows := 'Windows 2000 Server';

          end

          else

          begin

            if (bExtendedInfo) and (VerInfo.wSuiteMask and VER_SUITE_DATACENTER<>0) then

              Result.sWindows := 'Windows NT 4.0 Server Datacenter'

            else if (bExtendedInfo) and (VerInfo.wSuiteMask and VER_SUITE_ENTERPRISE<>0) then

              Result.sWindows := 'Windows NT 4.0 Server Enterprise'

            else if (bExtendedInfo) and (VerInfo.wSuiteMask and VER_SUITE_BLADE<>0) then

              Result.sWindows := 'Windows NT 4.0 Server Web Edition'

            else

              Result.sWindows := 'Windows NT 4.0 Server';

          end;

        end;

      end;

      else

        Result.sWindows := Format('Unknown Platform ID (%d)', [VerInfo.dwPlatformId]);

    end;

  end;
 

  Result.sServicePack  := Format('%d.%d', [Result.ServicePackMajor, Result.ServicePackMinor]);

  Result.sCompleteDesc := Format('%s (Build: %d', [Result.sWindows, Result.Build]);

  if not (Result.ServicePackMajor=0) then Result.sCompleteDesc := Result.sCompleteDesc + Format(' - Service Pack: %s', [Result.sServicePack]);

  Result.sCompleteDesc := Result.sCompleteDesc + ')';

end;

Open in new window

0
 
LVL 2

Assisted Solution

by:preppydude
preppydude earned 500 total points
ID: 24812832
Forgot to attach the code I'm using to retrieve the values...
procedure TMainForm.btnTestClick(Sender: TObject);

var

  VerInfo: TMyOSVersionInfo;

begin

  VerInfo           := GetOSInformation;

  txtWindows.Text   := VerInfo.sCompleteDesc;

  txtMajor.Text     := IntToStr(VerInfo.Major);

  txtMinor.Text     := IntToStr(VerInfo.Minor);

  txtBuild.Text     := IntToStr(VerInfo.Build);

  txtSpVersion.Text := VerInfo.sServicePack;

  txt64System.Text  := BoolToStr(VerInfo.Is64BitSystem, True);

  txtServerEdition.Text := BoolToStr(VerInfo.IsServerEdition, True);

end;

Open in new window

0
 

Author Closing Comment

by:Delphiwizard
ID: 31600958
Thank you very much.
0
 

Author Comment

by:Delphiwizard
ID: 33796389
The original code doesn't had to be changed, but I lacked the following line in my code:
function GetVersionEx(var lpVersionInformation: TOSVersionInfoEx): BOOL; external kernel32 name 'GetVersionExW';


// Original code:



  if not (GetVersionEx(VerInfo)) then 

  begin 

    bExtendedInfo := False; 

    VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx); 

    if not (GetVersionEx(VerInfo)) then 

      VerInfo.dwOSVersionInfoSize := 0; 

  end;

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

758 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

22 Experts available now in Live!

Get 1:1 Help Now