unit PDHThread;
interface
uses
Classes, Windows, SysUtils;
const
PDH_NO_DATA = $800007D5;
PDH_MEMORY_ALLOCATION_FAILURE = $C0000BBB;
PDH_INVALID_HANDLE = $C0000BBC;
PDH_INVALID_ARGUMENT = $C0000BBD;
PDH_FMT_RAW = $00000010;
PDH_FMT_ANSI = $00000020;
PDH_FMT_UNICODE = $00000040;
PDH_FMT_LONG = $00000100;
PDH_FMT_DOUBLE = $00000200;
PDH_FMT_LARGE = $00000400;
PDH_FMT_NOSCALE = $00001000;
PDH_FMT_1000 = $00002000;
PDH_FMT_NODATA = $00004000;
PDH_FMT_NOCAP100 = $00008000;
type
PQUERY = ^HQUERY;
HQUERY = THandle;
PCOUNTER = ^HCOUNTER;
HCOUNTER = THandle;
PDH_STATUS = Longint;
PPDH_FMT_COUNTERVALUE = ^TPDH_FMT_COUNTERVALUE;
_PDH_FMT_COUNTERVALUE =
record
CStatus : DWORD;
longValue : Longint;
doubleValue : double;
largeValue : LONGLONG;
AnsiStringValue : LPCSTR;
WideStringValue : LPCWSTR;
end;
TPDH_FMT_COUNTERVALUE = _PDH_FMT_COUNTERVALUE;
PDH_FMT_COUNTERVALUE = _PDH_FMT_COUNTERVALUE;
type
TPDHInfo =
record
Counter : HCounter;
PDHLoad : PDH_FMT_COUNTERVALUE;
end;
type
TPDHThread =
class(TThread)
private
HQ : HQuery;
PDHInfo :
Array[1..4]
of TPDHInfo;
hPDH : THandle;
procedure UpdatePDH;
protected
procedure Execute;
override;
public
end;
Var
PdhOpenQuery :
function(pReserved: Pointer; dwUserData: DWORD; phQuery: PQUERY): PDH_STATUS;
stdcall;
PdhCloseQuery :
function(ahQuery: HQUERY): PDH_STATUS;
stdcall;
PdhAddCounter :
function(ahQuery: HQUERY; szFullCounterPath: PChar; dwUserData: DWORD; phCounter: PCOUNTER ): PDH_STATUS;
stdcall;
PdhRemoveCounter :
function( ahCounter: HCOUNTER ): PDH_STATUS;
stdcall;
PdhCollectQueryData :
function( ahQuery: HQUERY ): PDH_STATUS;
stdcall;
PdhValidatePath :
function( szFullCounterPath: PChar ): PDH_STATUS;
stdcall;
PdhGetFormattedCounterValue :
function( ahCounter: HCOUNTER; dwFormat: DWORD; lpdwType: LPDWORD; pValue: PPDH_FMT_COUNTERVALUE): PDH_STATUS;
stdcall;
implementation
uses Main;
function GetNumberOfProcessors: Integer;
var
SystemInfo: TSystemInfo;
begin
GetSystemInfo(SystemInfo);
Result:=SystemInfo.dwNumberOfProcessors;
end;
Function LoadPDH : THandle;
Var
H : THandle;
Begin
H := LoadLibrary('
PDH.DLL');
If H <> 0
then
Begin
PdhOpenQuery := GetProcAddress(H, '
PdhOpenQuery');
PdhCloseQuery := GetProcAddress(H, '
PdhCloseQuery');
PdhAddCounter := GetProcAddress(H, '
PdhAddCounterA');
PdhRemoveCounter := GetProcAddress(H, '
PdhRemoveCounter');
PdhCollectQueryData := GetProcAddress(H, '
PdhCollectQueryData');
PdhValidatePath := GetProcAddress(H, '
PdhValidatePath');
PdhGetFormattedCounterValue := GetProcAddress(H, '
PdhGetFormattedCounterValue');
End;
Result := H;
End;
procedure TPDHThread.Execute;
var
X, MaxX : Integer;
dwctrType : DWord;
begin
hPDH := LoadPDH;
If hPDH <> 0
then
Begin
If PDHOpenQuery(
nil, 1 ,@HQ) = ERROR_SUCCESS
then
Begin
MaxX := GetNumberOfProcessors;
If MaxX > 4
then
MaxX := 4;
For X := 1
to MaxX
do
If PDHAddCounter(HQ, PChar('
\Prozessor('+InttoStr(X-1)+'
)\Prozessorzeit (%)'), 1, @PDHInfo[X].Counter) <> ERROR_SUCCESS
then
PDHAddCounter(HQ, PChar('
\Processor('+InttoStr(X-1)+'
)\% Processor Time'), 1, @PDHInfo[X].Counter);
while not Terminated
do
Begin
If PDHCollectQueryData(HQ) = ERROR_SUCCESS
then
For X := 1
to MaxX
do
PdhGetFormattedCounterValue(PDHInfo[X].Counter, PDH_FMT_DOUBLE, @dwctrType, @PDHInfo[X].PDHLoad);
Synchronize(UpdatePDH);
Sleep(1000);
End;
PdhCloseQuery(HQ);
FreeLibrary(hPDH);
End;
End;
end;
procedure TPDHThread.UpdatePDH;
begin
MainForm.UpdatePDH(PDHInfo);
end;
end.