Link to home
Start Free TrialLog in
Avatar of petershaw9
petershaw9

asked on

How to use GetVersionEx?

Hi,
I need to get os platform info. So I use GetVersionEx.
Firstly, I try the following;

procedure TForm1.Button1Click(Sender: TObject);
type
  _OSVersionInfoA = record
    dwOSVersionInfoSize,
    dwMajorVersion,
    dwMinorVersion,
    dwBuildNumber,
    dwPlatformId : DWORD;
    szCSDVersion : array[1..127 ] of Ansichar;
  end;

var verInfo : _OSVersionInfoA;
begin
  VerInfo.dwOSVersionInfoSize := SizeOf(VerInfo);
  GetVersionEx(VerInfo);
end;

It cannot compile. said type uncompatable.
Finally I uses the other way as followed, it works.

var OSI : TOSVersionInfo;
begin
  OSI.dwOSVersionInfoSize := SizeOf(OSI);
  GetVersionEx(OSI);
end;


but I don't know why first one did not work?
  _OSVersionInfoA = record
    dwOSVersionInfoSize,
    dwMajorVersion,
    dwMinorVersion,
    dwBuildNumber,
    dwPlatformId : DWORD;
    szCSDVersion : array[1..127 ] of Ansichar;
  end;
is same as TOSVersionInfo. why one work, another not?


peter

Avatar of Alone
Alone

If you want to get what system (9x or NT-like) your program is running use SysUtils variable Win32Platform instead. It presents in SysUtils unit from Delphi 5 and higher.

WBR...
If you want to get what system (9x or NT-like) your program is running use SysUtils variable Win32Platform instead. It presents in SysUtils unit from Delphi 5 and higher.

WBR...
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi PeterShaw9

I have been working with this stuff too - I've written a small unit, which uses GetVersionEx and identifies the platformId and the Windows version.

I hope this is helpfull to you.

LoneA


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
CONST
  // Windows Version ID's
  WINDOWS_UNKNOWN  = 0;    // Unknown
  WINDOWS_NT_3_51  = 351;  // Windows NT 3.51
  WINDOWS_NT_4     = 4;    // Windows NT 4;
  WINDOWS_ME       = 490;  // Windows Millenium
  WINDOWS_95       = 95;   // Windows 95
  WINDOWS_98       = 98;   // Windows 98
  WINDOWS_2000     = 500;  // Windows 2000
  WINDOWS_XP       = 510;  // Windows XP


FUNCTION GetOS (VAR Major  : DWORD;
                VAR Minor  : DWORD;
                VAR PlatformID : DWORD) : Smallint;
VAR
  MyVersionInfo : OSVERSIONINFO;
  WindowsVer : Smallint;
BEGIN
  { Get version-no using GETVERSIONex.  }
  MyVersionInfo.dwOSVersionInfoSize := sizeof(OSVERSIONINFO);

  WindowsVer := WINDOWS_UNKNOWN;
  Major      := WINDOWS_UNKNOWN;
  Minor      := WINDOWS_UNKNOWN;
  PlatformID := WINDOWS_UNKNOWN;

  if GetVersionEx (MyVersionInfo) THEN
  BEGIN
   { dwMajorVersion
     Identifies the major version number of the operating system as follows. Operating System Value
     dwMinorVersion
     Identifies the minor version number of the operating system as follows. Operating System Value }
    CASE MyVersionInfo.dwMajorVersion OF
      3 : BEGIN { Windows NT 3.51 }
            if MyVersionInfo.dwMinorVersion = 51 THEN
              WindowsVer := WINDOWS_NT_3_51
            ELSE
              WindowsVer :=  WINDOWS_UNKNOWN;
          END;

      4 : BEGIN { Windows 95, Windows 98, Windows Me, Windows NT 4.0 }
            CASE MyVersionInfo.dwMinorVersion OF
              0 : BEGIN
                    if MyVersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT THEN
                    BEGIN
                      // Det er en NT - detekt service-pack
                      WindowsVer := WINDOWS_NT_4;
                    END
                    ELSE
                    BEGIN
                      // Det er en 95
                      WindowsVer := WINDOWS_95;
                    END;
                  END;
              10 : BEGIN
                     // Det er en 98 - detekt service-pack
                      WindowsVer := WINDOWS_98;
                   END;
              90 : WindowsVer := WINDOWS_ME;
            END;
          END;
      5 : BEGIN { Windows 2000, Whistler  }
            CASE MyVersionInfo.dwMinorVersion OF
              0 : WindowsVer := WINDOWS_2000;
              1 : WindowsVer := WINDOWS_XP;
            END;
          END;
    END;

    Major := MyVersionInfo.dwMajorVersion;
    Minor := MyVersionInfo.dwMinorVersion;
    PlatformID := MyVersionInfo.dwPlatformId;
  END;

  Result := WindowsVer;
END;


end.
Avatar of petershaw9

ASKER

Hi all the answers are very helpful. Thank u very much.