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

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

0
petershaw9
Asked:
petershaw9
1 Solution
 
AloneCommented:
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...
0
 
AloneCommented:
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...
0
 
Slick812Commented:
hello petershaw9, in the Windows.pas the GetVersionEx( ) is defined as

function GetVersionEx(var lpVersionInformation: TOSVersionInfo): BOOL; stdcall;

the parameter var is a TOSVersionInfo type, your local _OSVersionInfoA type is not defined as equivalent to the TOSVersionInfo type
so the compiler will not accept that type as a TOSVersionInfo. You can Type cast your local _OSVersionInfoA as a TOSVersionInfo. If you want to use your own types you may have to declare your function

function GetVersionExA; external 'kernel32.dll' name 'GetVersionExA';

function GetVersionExA(var lpVersionInformation: TWhateverType): BOOL; stdcall;


- - - - - - - - - - - - - - - -

procedure TForm1.sbut_GetVerClick(Sender: TObject);
type
  TOSVerInfo = record
    dwOSVersionInfoSize: DWORD;
    dwMajorVersion: DWORD;
    dwMinorVersion: DWORD;
    dwBuildNumber: DWORD;
    dwPlatformId: DWORD;
    szCSDVersion: array[0..127] of AnsiChar;
   end;
var
MyOSver: TOSVerInfo;
begin
MyOSver.dwOSVersionInfoSize := SizeOf(TOSVerInfo);
GetVersionEx(TOSVersionInfo(MyOSver));
{typecast your local record type to the type
defined in the function definition}
ShowMessage('Major Ver is '+IntToStr(MyOSver.dwMajorVersion));
end;
0
 
LoneACommented:
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.
0
 
petershaw9Author Commented:
Hi all the answers are very helpful. Thank u very much.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now