Reputation: 3486
how to implement a function that will return the OS name? detect the environment where the program running on?
win2000/xp/vista/win7 etc...?
Upvotes: 12
Views: 9932
Reputation: 2194
Since Delphi XE2 you can use TOSVersion
record. Note, that it is a global record and you cannot create TOSVersion objects.
For example:
lblVersion.Caption := TOSVersion.ToString;
You have few record properties available to use: Minor
, Major
, Build
, Architecture
provided together with Platform
which you can use to make sure that the operating system is Windows.
To compare the current version of the system with defined one, use Check
version.
Upvotes: 2
Reputation: 109002
Something like this:
function osver: string;
begin
result := 'Unknown (Windows ' + IntToStr(Win32MajorVersion) + '.' + IntToStr(Win32MinorVersion) + ')';
case Win32MajorVersion of
4:
case Win32MinorVersion of
0: result := 'Windows 95';
10: result := 'Windows 98';
90: result := 'Windows ME';
end;
5:
case Win32MinorVersion of
0: result := 'Windows 2000';
1: result := 'Windows XP';
end;
6:
case Win32MinorVersion of
0: result := 'Windows Vista';
1: result := 'Windows 7';
2: result := 'Windows 8';
3: result := 'Windows 8.1';
end;
10:
case Win32MinorVersion of
0: result := 'Windows 10';
end;
end;
end;
There is really no need to call GetVersionEx
because SysUtils.pas
has InitPlatformID
in its initialization
clause. Hence the global constants Win32MajorVersion
and Win32MinorVersion
(and friends) will be populated already.
Upvotes: 20
Reputation: 21194
function OS_IsWindowsXP: Boolean;
begin
Result:= (Win32MajorVersion= 5) AND (Win32MinorVersion= 1);
end;
function OS_IsWindowsXPUp: Boolean;
begin
Result:= ((Win32MajorVersion = 5) AND (Win32MinorVersion>= 1))
OR (Win32MajorVersion>= 6);
end;
function OS_IsWindowsVista: Boolean;
begin
Result:= (Win32MajorVersion= 6) AND (Win32MinorVersion= 0);
end;
function OS_IsWindowsVistaUp: Boolean;
begin
Result:= (Win32MajorVersion>= 6);
end;
function OS_IsWindows7: Boolean;
begin
Result:= (Win32MajorVersion= 6) AND (Win32MinorVersion= 1);
end;
function OS_IsWindows7Up: Boolean;
begin
Result:= ((Win32MajorVersion = 6) AND (Win32MinorVersion>= 1)) { Any version 6 equal and above Win7 }
OR (Win32MajorVersion>= 7); { Any version above 6 }
end;
function OS_IsWindows8: Boolean;
begin
Result:= (Win32MajorVersion = 6) AND (Win32MinorVersion= 2);
end;
function OS_IsWindows8Up: Boolean;
begin
Result:= ((Win32MajorVersion = 6) AND (Win32MinorVersion>= 2))
OR (Win32MajorVersion>= 7);
end;
Upvotes: 0
Reputation: 69002
The JCL contains this enumerated type:
TWindowsVersion =
(wvUnknown, wvWin95, wvWin95OSR2, wvWin98, wvWin98SE, wvWinME,
wvWinNT31, wvWinNT35, wvWinNT351, wvWinNT4, wvWin2000, wvWinXP,
wvWin2003, wvWinXP64, wvWin2003R2, wvWinVista, wvWinServer2008,
wvWin7, wvWinServer2008R2);
It contains this function, and others:
function GetWindowsVersion: TWindowsVersion;
What's even better, when a new version of Windows comes out, someone will update the JCL and you won't have to extend this code yourself. Also, of all the answers to this question, only the JCL handles Windows Server operating system detection, in addition to the usual windows end-user versions.
Upvotes: 1
Reputation: 6848
If you want obtain more information about de Operating System, you can use WMI (the code of RRUZ use it for the name). For more information you can use the component COperatingSystemInfo of GLibWMI (from Sourceforge or Author WebPage (mine) ;-D ).
All the library is free and sourve code avaible. For OperatingSystem you can obtain this properties:
alt text http://a.imageshack.us/img594/1660/coperatinsysteminfo.png
Regards
Upvotes: 4
Reputation: 8243
I use this in my programs:
{$J+}
TYPE
TOperatingSystemClass = (osUntested,osUnknown,osWin95,osWin98,osWinME,osWinNT,osWin2000,osWinXP,osWinXPSP1,osWinXPSP2,osWinXPSP3,osWin2003,osWinVista,osWinVistaSP1,osWinVistaSP2,osWinVistaSP3,osWin7,osWin7SP1,osWin7SP2,osWin7SP3,osHigher);
FUNCTION OperatingSystemClass : TOperatingSystemClass;
CONST
OSClass : TOperatingSystemClass = osUntested;
VAR
Info : OSVERSIONINFOEX;
OldInfo : OSVERSIONINFO ABSOLUTE Info;
BEGIN
IF OSClass=osUntested THEN BEGIN
FillChar(Info,SizeOf(Info),0);
Info.dwOSVersionInfoSize:=SizeOf(Info); OSClass:=osUnknown;
IF NOT GetVersionEx(OldInfo) THEN BEGIN
FillChar(OldInfo,SizeOf(OldInfo),0);
OldInfo.dwOSVersionInfoSize:=SizeOf(OldInfo);
GetVersionEx(OldInfo)
END;
CASE Info.dwPlatformId OF
VER_PLATFORM_WIN32_WINDOWS : CASE Info.dwMajorVersion OF
3 : OSClass:=osWin95;
4 : CASE Info.dwMinorVersion OF
0 : OSClass:=osWin95;
10 : OSClass:=osWin98
ELSE // OTHERWISE //
OSClass:=osWinME
END
END;
VER_PLATFORM_WIN32_NT : CASE Info.dwMajorVersion OF
5 : CASE Info.dwMinorVersion OF
0 : OSClass:=osWin2000;
1 : OSClass:=osWinXP;
2 : OSClass:=osWin2003
END;
6 : IF Info.dwMinorVersion=0 THEN
OSClass:=osWinVista
ELSE IF Info.dwMinorVersion=1 THEN
OSClass:=osWin7
ELSE
OSClass:=osHigher
END
END;
IF (OSClass IN [osWinXP,osWinVista,osWin7]) AND (Info.wServicePackMajor>0) THEN
INC(OSClass,MAX(Info.wServicePackMajor,3))
END;
Result:=OSClass
END;
where
type
OSVERSIONINFOEX = packed record
dwOSVersionInfoSize: DWORD;
dwMajorVersion: DWORD;
dwMinorVersion: DWORD;
dwBuildNumber: DWORD;
dwPlatformId: DWORD;
szCSDVersion: Array [0..127 ] of Char;
wServicePackMajor: WORD;
wServicePackMinor: WORD;
wSuiteMask: WORD;
wProductType: BYTE;
wReserved: BYTE;
End;
TOSVersionInfoEx = OSVERSIONINFOEX;
POSVersionInfoEx = ^TOSVersionInfoEx;
Upvotes: 4
Reputation: 136431
As alternative to the Win32 API, you can use the WMI Win32_OperatingSystem
Class.
you can write a simple function like this to retrieve the operating windows version name.
function GetWin32_OSName:string;
var
objWMIService : OLEVariant;
colItems : OLEVariant;
colItem : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
function GetWMIObject(const objectName: String): IDispatch;
var
chEaten: Integer;
BindCtx: IBindCtx;
Moniker: IMoniker;
begin
OleCheck(CreateBindCtx(0, bindCtx));
OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
end;
begin
objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
colItems := objWMIService.ExecQuery('SELECT Caption FROM Win32_OperatingSystem','WQL',0);
oEnum := IUnknown(colItems._NewEnum) as IEnumVariant;
if oEnum.Next(1, colItem, iValue) = 0 then
Result:=colItem.Caption; //The caption property return an string wich includes the operating system version. For example, "Microsoft Windows XP Professional Version = 5.1.2500".
end;
Addionally you can retrieve more info about the Windows version using the others properties of the Win32_OperatingSystem Class.
Check this code
program GetWMI_Win32_OperatingSystem;
{$APPTYPE CONSOLE}
uses
SysUtils
,ActiveX
,ComObj
,Variants;
Procedure GetWin32_OperatingSystem;
var
objWMIService : OLEVariant;
colItems : OLEVariant;
colItem : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
function GetWMIObject(const objectName: String): IDispatch;
var
chEaten: Integer;
BindCtx: IBindCtx;
Moniker: IMoniker;
begin
OleCheck(CreateBindCtx(0, bindCtx));
OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
end;
begin
objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
colItems := objWMIService.ExecQuery('SELECT * FROM Win32_OperatingSystem','WQL',0);
oEnum := IUnknown(colItems._NewEnum) as IEnumVariant;
if oEnum.Next(1, colItem, iValue) = 0 then
begin
Writeln('Caption '+colItem.Caption);
Writeln('Version '+colItem.Version);
Writeln('BuildNumber '+colItem.BuildNumber);
Writeln('BuildType '+colItem.BuildType);
Writeln('CodeSet '+colItem.CodeSet);
Writeln('CountryCode '+colItem.CountryCode);
Writeln('BootDevice '+colItem.BootDevice);
Writeln;
end;
end;
function GetWin32_OSName:string;
var
objWMIService : OLEVariant;
colItems : OLEVariant;
colItem : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
function GetWMIObject(const objectName: String): IDispatch;
var
chEaten: Integer;
BindCtx: IBindCtx;
Moniker: IMoniker;
begin
OleCheck(CreateBindCtx(0, bindCtx));
OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
end;
begin
objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
colItems := objWMIService.ExecQuery('SELECT Caption FROM Win32_OperatingSystem','WQL',0);
oEnum := IUnknown(colItems._NewEnum) as IEnumVariant;
if oEnum.Next(1, colItem, iValue) = 0 then
Result:=colItem.Caption;
end;
begin
try
CoInitialize(nil);
try
GetWin32_OperatingSystem;
//Writeln(GetWin32_OSName);
Readln;
finally
CoUninitialize;
end;
except
on E:Exception do
Begin
Writeln(E.Classname, ': ', E.Message);
Readln;
End;
end;
end.
Upvotes: 11