By Zarko Gajic
Windows Management Instrumentation (WMI) is the infrastructure for management data and operations on Windows-based operating systems.
You can use Delphi to communicate with WMI by using a variety of interfaces. All the WMI interfaces are based on the Component Object Model (COM).
The Caption property of the Win32_OperatingSystem?WMI class returns a short description of the object (a one-line string). The string includes the operating system version. For example, "Microsoft Windows XP Professional Version = 5.1.2500".
Here's how to read the Windows OS Name from Delphi:
uses ActiveX;
function GetWin32_OSNameVersion : 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
try
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
result := Format('%s %s',[colItem.Caption, colItem.Version]);
except
result := '?'
end;
end;
Note: there's also the Win32MajorVersion and Win32MinorVersion defined in the System.pas you can use to create a Get Windows version function - but still you would need to turn results from GetVersionEx into a string representation.
Further, search for WMI and Win32_OperatingSystem for much more usage ideas...