Get CPU Usage Using PDH API
-
Hello. I am trying to get cpu usage in delphi, using
Performace Data Helper API
. At the moment I know just two methods of getting it. One using Registry Interface and second PDH Interface and latter is easier than former. I am trying to use following code but it produces PDH_INVALID_ARGUMENT meaning either argument is missing or is invalid.hStatus := PdhOpenQuery(Nil, 0, hQuery);
if hStatus <> ERROR_SUCCESS then // hStatus = PDH_INVALID_ARGUMENT
begin
GetErrorMsg(hStatus, sErrorMsg); // sErrorMsg = 'argument is invalid or missing'
Exit;
end;SO WHAT COULD BE WRONG? Thanks for any pointer. May be irrelevant, but to give an idea that pdh variables are intialized with what values.
implementaion
varPdhOpenQuery : function( pReserved: Pointer;
dwUserData: DWORD; phQuery: PDH_HQUERY ): PDH_STATUS; stdcall;
// similarly the rest of the pdh functions hereDllHandle : THandle;
hStatus : PDH_STATUS;procedure TClass.Create()
beginDllHandle := LoadLibrary('pdh.dll');
if DllHandle <> 0 then
begin
PdhOpenQuery := GetProcAddress( DllHandle, 'PdhOpenQuery' );
// similarly the rest of the pdh functions here
end; -
Hello. I am trying to get cpu usage in delphi, using
Performace Data Helper API
. At the moment I know just two methods of getting it. One using Registry Interface and second PDH Interface and latter is easier than former. I am trying to use following code but it produces PDH_INVALID_ARGUMENT meaning either argument is missing or is invalid.hStatus := PdhOpenQuery(Nil, 0, hQuery);
if hStatus <> ERROR_SUCCESS then // hStatus = PDH_INVALID_ARGUMENT
begin
GetErrorMsg(hStatus, sErrorMsg); // sErrorMsg = 'argument is invalid or missing'
Exit;
end;SO WHAT COULD BE WRONG? Thanks for any pointer. May be irrelevant, but to give an idea that pdh variables are intialized with what values.
implementaion
varPdhOpenQuery : function( pReserved: Pointer;
dwUserData: DWORD; phQuery: PDH_HQUERY ): PDH_STATUS; stdcall;
// similarly the rest of the pdh functions hereDllHandle : THandle;
hStatus : PDH_STATUS;procedure TClass.Create()
beginDllHandle := LoadLibrary('pdh.dll');
if DllHandle <> 0 then
begin
PdhOpenQuery := GetProcAddress( DllHandle, 'PdhOpenQuery' );
// similarly the rest of the pdh functions here
end;Not sure your compiler is able to delay load dynamic library[^] which is another option. I haven't got chance to use this syntax though... I would use the following to dynamically load the function.
type
//definition is based on http://gr32ex.googlecode.com/svn/trunk/GR32Ex/Examples/DesktopSnow/GPSysHook/api/JwaPdh.pas
PDH_HQUERY = THANDLE;
PDH_STATUS = DWORD;
FuncPdhOpenQuery = function(szDataSource: LPCTSTR; dwUserData: DWORD_PTR;
var phQuery: PDH_HQUERY): PDH_STATUS; stdcall;//and your function implementation would be:
procedure LoadPDHQuery;
var
PdhOpenQuery: FuncPdhOpenQuery;
DllHandle: HModule;
QueryHandle: PDH_HQUERY;
Res: PDH_STATUS;
begin
try
DllHandle:= LoadLibrary('pdh.dll');
if DllHandle > 0 then
begin
@PdhOpenQuery:= GetProcAddress(DllHandle, 'PdhOpenQueryW');//notice it's either W or A as stated in MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/aa372652(v=vs.85).aspx)
if (Assigned(PdhOpenQuery)) then
begin
Res:= PdhOpenQuery(nil, 0, QueryHandle);ShowMessage(IntToStr(Res)); end; end;
finally
if (DllHandle > 0)
FreeLibrary(DllHandle);
end;
end; -
Hello. I am trying to get cpu usage in delphi, using
Performace Data Helper API
. At the moment I know just two methods of getting it. One using Registry Interface and second PDH Interface and latter is easier than former. I am trying to use following code but it produces PDH_INVALID_ARGUMENT meaning either argument is missing or is invalid.hStatus := PdhOpenQuery(Nil, 0, hQuery);
if hStatus <> ERROR_SUCCESS then // hStatus = PDH_INVALID_ARGUMENT
begin
GetErrorMsg(hStatus, sErrorMsg); // sErrorMsg = 'argument is invalid or missing'
Exit;
end;SO WHAT COULD BE WRONG? Thanks for any pointer. May be irrelevant, but to give an idea that pdh variables are intialized with what values.
implementaion
varPdhOpenQuery : function( pReserved: Pointer;
dwUserData: DWORD; phQuery: PDH_HQUERY ): PDH_STATUS; stdcall;
// similarly the rest of the pdh functions hereDllHandle : THandle;
hStatus : PDH_STATUS;procedure TClass.Create()
beginDllHandle := LoadLibrary('pdh.dll');
if DllHandle <> 0 then
begin
PdhOpenQuery := GetProcAddress( DllHandle, 'PdhOpenQuery' );
// similarly the rest of the pdh functions here
end;It don't know where is the error it works here. And there is a third method, you did not mention: WMI.