Solved

How to use GetVersionEx?

Posted on 2002-05-05
5
1,118 Views
Last Modified: 2008-02-01
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
Comment
Question by:petershaw9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Expert Comment

by:Alone
ID: 6990491
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
 
LVL 1

Expert Comment

by:Alone
ID: 6990492
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
 
LVL 34

Accepted Solution

by:
Slick812 earned 50 total points
ID: 6990865
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
 
LVL 1

Expert Comment

by:LoneA
ID: 6993072
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
 

Author Comment

by:petershaw9
ID: 7002915
Hi all the answers are very helpful. Thank u very much.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 358
Strange behavior when a form is closed 6 69
FMX and jaudiotracker playing memory stream 29 98
Firemonkey Queue list 1 31
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…

733 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