uses
WbemScripting_TLB,
ActiveX;
type
TProcesInformation =
packed record
Name:
string;
Path:
string;
ProcID: DWORD;
ParentProcID: DWORD;
SessionID: DWORD;
ThreadCount: DWORD;
Priority: DWORD;
end;
type
TPIArray =
array of TProcesInformation;
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(Computer, User, Password:
String): 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;
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_NIC,
wbemFlagUseAmendedQualifiers,
nil);
ObjectSet := ObjectDefinition.Instances_(0,
nil);
ObjectInstances := (ObjectSet._NewEnum)
as IEnumVariant;
while (ObjectInstances.Next(1, TempObj, ObjValue) = S_OK)
do
begin
WMIObject := IUnknown(TempObj)
as SWBemObject;
PropertySet := WMIObject.Properties_;
setlength(result, length(result) + 1);
WMIProperty := PropertySet.Item(WMI_ATTRIB_CAPTION, 0);
if not VarIsNull(WMIProperty.Get_Value)
then
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
raise e.Create(e.
message);
end;
end;