Mastodon
Programmierung

Get the OS-Version

How to get the OS-Version?

Solution by GRS:
// —— Code starts ——

[code lang=”delphi”]unit osvr1632;

interface

procedure OSVersion(var WinMaj, WinMin: word);

implementation

uses {$IFDEF VER80} WinProcs {$ELSE} Windows {$ENDIF} ;

procedure OSVersion( var WinMaj, WinMin: word);
{$IFDEF VER80}
var osv: longint;
begin
osv := GetVersion;
WinMaj := osv and $FF;
WinMin := (osv shr and $FF;
if WinMin=95 then
begin
WinMaj := 4;
WinMin := 0;
end;
{$ELSE}
var OSVI: TOSVersionInfo;
n: dword;
begin
n := SizeOf(TOSVersionInfo);
ZeroMemory(@OSVI,n);
OSVI.dwOSVersionInfoSize := n;
GetVersionEx(OSVI);
if OSVI.dwPlatformId=VER_PLATFORM_WIN32_NT then
begin
WinMaj := OSVI.dwMajorVersion;
WinMin := OSVI.dwMinorVersion;
end
else
begin
WinMaj := (OSVI.dwBuildNumber shr 24) and $FF;
WinMin := (OSVI.dwBuildNumber shr 16) and $FF;
end;
{$ENDIF}
end;

end.
[/code]
// ——- Code ends ——-

If compiled with D5:
You’ll get the expected results: winMaj=4 and winMin=whatever when under
Win9x, ME or NT, and you’ll get winMaj=5 when under Win2K.

The use of the dwBuildNumber part of the OSVersionInfo structure in the
32-bit part of the procedure overcomes the recently discovered “WinME
becoming Win98” bug if the exe is called setup.exe. If you’re not
bothered
by that bug then you can change the 2 appropriate lines to:

WinMaj := OSVI.dwMajorVersion;
WinMin := OSVI.dwMinorVersion;

[tags]Delphi, System[/tags]

0 Kommentare zu “Get the OS-Version

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.