uses
WbemScripting_TLB,
ActiveX;
type
TWMIOSInfo =
record
OSVersionString:
string;
CSDVersion:
string;
OSManufacturer:
string;
end;
const
WMI_SYSTEM_NAMESPACE = '
root\CIMV2';
WMI_CLASS_OS = '
Win32_OperatingSystem';
WMI_ATTRIB_OSCAPTION = '
Caption';
WMI_ATTRIB_CSDVERSION = '
CSDVersion';
WMI_ATTRIB_OSMAN = '
Manufacturer';
function WMIGetOSInfo(
const Computer, user, Password:
string): TWMIOSInfo;
var
Locator : ISWbemLocator;
Services : ISWbemServices;
ObjectDefinition: ISWbemObject;
ObjectSet : SWbemObjectSet;
ObjectInstances: IEnumVariant;
WMIObject : ISWbemObject;
PropertySet : ISWbemPropertySet;
WMIProperty : ISWbemProperty;
TempObj : OleVariant;
ObjValue : Cardinal;
begin
Locator := CoSWbemLocator.CreateRemote(Computer);
try
try
Services := Locator.ConnectServer(Computer, WMI_SYSTEM_NAMESPACE, User,
Password, '
', '
', 0,
nil);
if Services <>
nil then
begin
ObjectDefinition := Services.Get(WMI_CLASS_OS,
wbemFlagUseAmendedQualifiers,
nil);
ObjectSet := ObjectDefinition.Instances_(0,
nil);
ObjectInstances := (ObjectSet._NewEnum)
as IEnumVariant;
if ObjectInstances.Next(1, TempObj, ObjValue) = S_OK
then
begin
WMIObject := IUnknown(TempObj)
as SWBemObject;
PropertySet := WMIObject.Properties_;
WMIProperty := PropertySet.Item(WMI_ATTRIB_OSCAPTION, 0);
if not VarIsNull(WMIProperty.Get_Value)
then
result.OSVersionString := trim(WMIProperty.Get_Value);
WMIProperty := PropertySet.Item(WMI_ATTRIB_CSDVERSION, 0);
if not VarIsNull(WMIProperty.Get_Value)
then
result.CSDVersion := trim(WMIProperty.Get_Value);
WMIProperty := PropertySet.Item(WMI_ATTRIB_OSMAN, 0);
if not VarIsNull(WMIProperty.Get_Value)
then
result.OSManufacturer := trim(WMIProperty.Get_Value);
end;
end;
finally
Locator :=
nil;
Services :=
nil;
end;
except
on e:
Exception do
e.Create(e.
Message);
end;
end;