const
WMI_HOST_COMPUTER = '.';
WMI_SYSTEM_NAMESPACE = 'root\CIMV2';
WMI_CLASS_NIC = 'Win32_Process';
WMI_ATTRIB_CAPTION = 'Name';
WMI_ATTRIB_PATH = 'ExecutablePath';
WMI_ATTRIB_PROCID = 'ProcessID';
WMI_ATTRIB_PARENT_PROCID = 'ParentProcessId';
WMI_ATTRIB_SESSIONID = 'SessionID';
WMI_ATTRIB_THREAD_CNT = 'ThreadCount';
WMI_ATTRIB_PRIORITY = 'Priority';
function WMIEnumProcesses(
Handle: THandle): TPIArray;
var
Locator : ISWbemLocator;
Services : ISWbemServices;
ObjectDefinition: ISWbemObject;
ObjectSet : SWbemObjectSet;
ObjectInstances: IEnumVariant;
WMIObject : ISWbemObject;
PropertySet : ISWbemPropertySet;
WMIProperty : ISWbemProperty;
TempObj : OleVariant;
ObjValue : Cardinal;
i : Integer;
resourcestring
rsWMIError = '
WMI-Fehler';
begin
i := 0;
[color=red]Locator := CoSWbemLocator.Create;[/color]
try
try
[color=red]Services := Locator.ConnectServer(WMI_HOST_COMPUTER, WMI_SYSTEM_NAMESPACE,
'', '', '', '', 0, nil);[/color]
if Services <> nil then
begin
[color=red]ObjectDefinition := Services.Get(WMI_CLASS_NIC,
wbemFlagUseAmendedQualifiers, nil);[/color]
[color=red]ObjectSet := ObjectDefinition.Instances_(0, nil);[/color]
[color=red]ObjectInstances := (ObjectSet._NewEnum) as IEnumVariant;[/color]
while (ObjectInstances.Next(1, TempObj, ObjValue) = S_OK) do
begin
[color=red]WMIObject := IUnknown(TempObj) as SWBemObject;[/color]
[color=red]PropertySet := WMIObject.Properties_;[/color]
setlength(result, length(result) + 1);
[color=green]// welche Eigenschaft will ich abfragen[/color]
WMIProperty := PropertySet.Item(WMI_ATTRIB_CAPTION, 0);
if not VarIsNull(WMIProperty.Get_Value) then
[color=green]// Eigenschaft abfragen[/color]
result[i].Name := WMIProperty.Get_Value;
WMIProperty := PropertySet.Item(WMI_ATTRIB_PATH, 0);
if not VarIsNull(WMIProperty.Get_Value) then
result[i].Path := WMIProperty.Get_Value;
WMIProperty := PropertySet.Item(WMI_ATTRIB_PROCID, 0);
if not VarIsNull(WMIProperty.Get_Value) then
result[i].ProcID := WMIProperty.Get_Value;
WMIProperty := PropertySet.Item(WMI_ATTRIB_PARENT_PROCID, 0);
if not VarIsNull(WMIProperty.Get_Value) then
result[i].ParentProcID := WMIProperty.Get_Value;
WMIProperty := PropertySet.Item(WMI_ATTRIB_SESSIONID, 0);
if not VarIsNull(WMIProperty.Get_Value) then
result[i].SessionID := WMIProperty.Get_Value;
WMIProperty := PropertySet.Item(WMI_ATTRIB_THREAD_CNT, 0);
if not VarIsNull(WMIProperty.Get_Value) then
result[i].ThreadCount := WMIProperty.Get_Value;
WMIProperty := PropertySet.Item(WMI_ATTRIB_PRIORITY, 0);
if not VarIsNull(WMIProperty.Get_Value) then
result[i].Priority := WMIProperty.Get_Value;
Inc(i);
end;
end;
finally
Locator := nil;
Services := nil;
end;
except
on e:
Exception do
Messagebox(
Handle, PChar(e.Message), PChar(rsWMIError), MB_ICONSTOP);
end;
end;