Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#1

[WMI] Quellcode erklären

  Alt 15. Jan 2005, 05:23
Könnte mir mal bitte jemand folgenden Quellcode kommentieren. Insbesondere die markierten Zeilen. Ich habe bestehenden Quellcode umgeschrieben und erweitert, weiß aber gar nicht so recht, was ich mache und warum. Aus dem PSDK werde ich nicht so recht schlau. Eventuell reicht mir auch eine Seite, wo das alles mal erklärt ist.
Code:
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;
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat