how to implement a function that will return the OS name? detect the environment where the program running on?
win2000/xp/vista/win7 etc...?
how to implement a function that will return the OS name? detect the environment where the program running on?
win2000/xp/vista/win7 etc...?
try this just expand the function http://www.swissdelphicenter.ch/torry/showcode.php?id=316
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';
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.
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.
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;
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:
Regards