Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Delphi
  4. Get CPU Usage Using PDH API

Get CPU Usage Using PDH API

Scheduled Pinned Locked Moved Delphi
jsondelphiwindows-adminhelpquestion
3 Posts 3 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Django_Untaken
    wrote on last edited by
    #1

    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
    var

    PdhOpenQuery : function( pReserved: Pointer;
    

    dwUserData: DWORD; phQuery: PDH_HQUERY ): PDH_STATUS; stdcall;
    // similarly the rest of the pdh functions here

    DllHandle : THandle;
    hStatus : PDH_STATUS;

    procedure TClass.Create()
    begin

    DllHandle := LoadLibrary('pdh.dll');
    if DllHandle <> 0 then
    begin
    PdhOpenQuery := GetProcAddress( DllHandle, 'PdhOpenQuery' );
    // similarly the rest of the pdh functions here
    end;

    S J 2 Replies Last reply
    0
    • D Django_Untaken

      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
      var

      PdhOpenQuery : function( pReserved: Pointer;
      

      dwUserData: DWORD; phQuery: PDH_HQUERY ): PDH_STATUS; stdcall;
      // similarly the rest of the pdh functions here

      DllHandle : THandle;
      hStatus : PDH_STATUS;

      procedure TClass.Create()
      begin

      DllHandle := LoadLibrary('pdh.dll');
      if DllHandle <> 0 then
      begin
      PdhOpenQuery := GetProcAddress( DllHandle, 'PdhOpenQuery' );
      // similarly the rest of the pdh functions here
      end;

      S Offline
      S Offline
      smags13
      wrote on last edited by
      #2

      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;

      1 Reply Last reply
      0
      • D Django_Untaken

        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
        var

        PdhOpenQuery : function( pReserved: Pointer;
        

        dwUserData: DWORD; phQuery: PDH_HQUERY ): PDH_STATUS; stdcall;
        // similarly the rest of the pdh functions here

        DllHandle : THandle;
        hStatus : PDH_STATUS;

        procedure TClass.Create()
        begin

        DllHandle := LoadLibrary('pdh.dll');
        if DllHandle <> 0 then
        begin
        PdhOpenQuery := GetProcAddress( DllHandle, 'PdhOpenQuery' );
        // similarly the rest of the pdh functions here
        end;

        J Offline
        J Offline
        Jose A Pascoa
        wrote on last edited by
        #3

        It don't know where is the error it works here. And there is a third method, you did not mention: WMI.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups