Thema: Delphi GetProcessIoCounters

Einzelnen Beitrag anzeigen

Nachaz

Registriert seit: 5. Feb 2004
Ort: Essen
18 Beiträge
 
Delphi XE Architect
 
#1

GetProcessIoCounters

  Alt 5. Feb 2004, 20:07
Hallo!

Ich habe ein kleines Problem: Ich will die Windows-Funktion GetProcessIoCounters aus Delphi 6 aufrufen. Die steht aber leider nicht vordefiniert in der Windows.pas (auch nicht in irgend einer anderen). Also hab' ich mir 'ne Funktion gebastelt: (Hier als Mini-Unit zum einbinden für Interessierte)

Delphi-Quellcode:
unit UNIFunctions;

interface
uses Windows, SysUtils, UNIConst, Forms;
type
  TIoCounters = record
    ReadOperationCount : LONGLONG;
    WriteOperationCount : LONGLONG;
    OtherOperationCount : LONGLONG;
    ReadTransferCount : LONGLONG;
    WriteTransferCount : LONGLONG;
    OtherTransferCount : LONGLONG;
  end;
  PIoCounters = ^TIoCounters;

Function GetProcessIoCounters(hProc : THandle) : PIoCounters;

implementation

Function GetProcessIoCounters(hProc : THandle) : PIoCounters;
var
  pIoc : PIoCounters;
  hDLL : THandle;
  _GetProcessIoCounters : Function(hProc : THandle; var IoCounters : PIoCounters) : BOOL;
begin
  hDll := LoadLibrary('Kernel32.dll');

  if hDll <> 0 then
    begin
       _GetProcessIoCounters := GetProcAddress(hDLL,'GetProcessIoCounters');

       if @_GetProcessIoCounters <> nil then
         begin
// new(pIoc);
           if _GetProcessIoCounters(hProc, pIoc) then
             begin
// dispose(@_GetProcessIoCounters);
               result := pioc
             end
           else
             result := nil;

// Dispose(pIoc);
         end
       else
         result := nil;
      FreeLibrary(hDLL);
    end
  else
    result := nil
end;

end.
Der Aufruf des ganzen sieht so aus: (lvProc ist ein TListView auf dem Formular, ListItems inkl. Subitems sind schon da)
Delphi-Quellcode:
procedure TfrmProcessInfo.refresh;
var
  hProc : THANDLE;
  pIoc : PIoCounters;
begin
  hProc := OpenProcess(PROCESS_ALL_ACCESS,false,pid);

  if hProc <> 0 then
    begin

      pIoc := GetProcessIoCounters(hProc);

      if pIoc <> nil then
        begin
          lvProc.Items.Item[3].SubItems.strings[0] := inttostr(pIoc^.ReadOperationCount);
          lvProc.Items.Item[4].SubItems.strings[0] := inttostr(pIoc^.WriteOperationCount);
          lvProc.Items.Item[5].SubItems.strings[0] := inttostr(pIoc^.OtherOperationCount);
          lvProc.Items.Item[6].SubItems.strings[0] := inttostr(pIoc^.ReadTransferCount);
          lvProc.Items.Item[7].SubItems.strings[0] := inttostr(pIoc^.WriteTransferCount);
          lvProc.Items.Item[8].SubItems.strings[0] := inttostr(pIoc^.OtherTransferCount);
        end
      else
        begin
          lvProc.Items.Item[3].SubItems.strings[0] := 'Error!';
          lvProc.Items.Item[4].SubItems.strings[0] := 'Error!';
          lvProc.Items.Item[5].SubItems.strings[0] := 'Error!';
          lvProc.Items.Item[6].SubItems.strings[0] := 'Error!';
          lvProc.Items.Item[7].SubItems.strings[0] := 'Error!';
          lvProc.Items.Item[8].SubItems.strings[0] := 'Error!';
        end;

    end
end;
Wenn man das ganze jetzt debugt (Denglisch?!?), sieht man, daß Delphi beim "end;" der Funktion GetProcessIoCounters abschmiert - und zwar irgendwo "zwischen" dem Code, also im CPU-Fenster! Zugriffsverletzung beim lesen von $irgendwas (Hexzahl sieht für mich ungewöhnlich hoch aus).

Wer hat so etwas vielleicht schon einmal erlebt und kann mir helfen?


Dank im Voraus,


Sven
Sven
  Mit Zitat antworten Zitat