Solved

How to use GetVersionEx?

Posted on 2002-05-05
5
1,095 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
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 33

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Path  to current project in Delphi. 2 80
Magic Software info 18 132
how to change, disabled button color FMX ? 1 33
Breakpoint doesn't stop in my variable 3 28
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

803 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