Solved

How to use GetVersionEx?

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

20 Experts available now in Live!

Get 1:1 Help Now