CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Join Date
    Feb 2004
    Location
    RB(The Republic of Belarus)
    Posts
    12

    Processor Information

    How can I Get (in '%') CPU performance? Task Manager(WinNt) shows such information.

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    NtQuerySystemInformation....

    Note, that 'NtQuerySystemInformation()' does only work on systems NT or higher...

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Sent by PM:

    Are you sure that NtQuerySystemInforamtion returns cpu perfomance(in '%') even fo each process? Tell me please in what structure such information is contained. I didn't see anything about it...

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Take a look at the following thread...

  5. #5
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    WMI will provide that information.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  6. #6
    Join Date
    Feb 2004
    Location
    RB(The Republic of Belarus)
    Posts
    12
    Tell me plz where I can find Info how to work whith WMI or may be You'll help me..

  7. #7
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Look at Windows Management Instrumentation (Windows Management Instrumentation SDK: Platform SDK). Specifically, look at Monitoring Performance Data. You can use the Win32_PerfRawData_PerfProc_Process class but if you are only using 2000/XP or only using XP, there are other classes that are easier for you and the system.

    In the Platform SDK samples there are a few WMI samples. One of them (I think called simple) has the basics of what you need to use WMI. It will take a little time to learn about WMI, but once you know how to use it, the knowledge can be used for many other things too.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  8. #8
    Join Date
    Feb 2004
    Location
    RB(The Republic of Belarus)
    Posts
    12
    Tell me what's wrong.
    I use PDH.
    PDH_STATUS pdhResult = 0;
    TCHAR szCounterPath[1024];
    DWORD dwPathSize = 1024;
    PDH_COUNTER_PATH_ELEMENTS pe;
    HQUERY hQuery;
    HQUERY hCounter;
    pdhResult = PdhOpenQuery( NULL, 0, &hQuery );
    pe.szMachineName = 0l;
    pe.szObjectName = "Processor";
    pe.szInstanceName = "_Total";
    pe.szParentInstance = NULL;
    pe.dwInstanceIndex = 0;
    pe.szCounterName = "% Processor Time";
    pdhResult = PdhMakeCounterPath(&pe, szCounterPath, &dwPathSize, 0);
    pdhResult = PdhAddCounter(hQuery, szCounterPath, 0, &hCounter);
    And here PdhAddCounter returns PDH_CSTATUS_NO_OBJECT.
    What I have done incorrectly?

  9. #9
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    I don't know; I have not used the Performance Data Helper.

    Did you also check pdhResult after calling PdhMakeCounterPath? Are you sure the values you are using in pe are valid names?

    Have you looked at relevant sample programs? It is essentially certain that there are sample uses of the Performance Data Helper provided by Microsoft somewhere, either in the Platform SDK or in a KB article or both. Also there might be a CodeGuru article.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  10. #10
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Are sure the code you posted is the code your using? Just asking cause it works fine on my XP machine. Can you start the performance monitor and get a value for the processor time? Is it a different name under NT? I don't recall it being...but...how about nulling out the machinename also?

    There are examples on MSDN...

    I changed you code a bit, just to grab the first processors stats, see if it works, the double query is necassary to get the correct CPU % ie: fill all the values...for the query set.

    Code:
    PDH_STATUS pdhResult = 0;
    TCHAR szCounterPath[1024];
    DWORD dwPathSize = 1024;
    PDH_COUNTER_PATH_ELEMENTS pe = {0};
    HQUERY hQuery;
    HQUERY hCounter;
    pdhResult = PdhOpenQuery( NULL, 0, &hQuery );
    pe.szObjectName = "Processor(0)";
    pe.szCounterName = "% Processor Time";
    pdhResult = PdhMakeCounterPath(&pe, szCounterPath, &dwPathSize, 0);
    pdhResult = PdhAddCounter(hQuery, szCounterPath, 0, &hCounter);
    
    pdhResult = PdhCollectQueryData(hQuery);
    Sleep(2000);
    pdhResult = PdhCollectQueryData(hQuery);
    
    PDH_FMT_COUNTERVALUE stFormattedValue = {0};
    pdhResult = PdhGetFormattedCounterValue( hCounter
                                           , PDH_FMT_LONG
                                           , NULL
                                           , &stFormattedValue
                                           );

  11. #11
    Join Date
    Sep 1999
    Location
    Germany, Hessen
    Posts
    226
    Just to be sure:

    Are PdhOpenQuery() and PdhMakeCounterPath() returning ERROR_SUCCESS ???

    Or are you working with a unicode build ? Try to encapsulate the sz... members with _T(...)

  12. #12
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Merged threads]

  13. #13
    Join Date
    Feb 2004
    Location
    RB(The Republic of Belarus)
    Posts
    12
    Of course I checked all return values of previous functions. I have a problem whith PdhAddCounter().

  14. #14
    Join Date
    Feb 2004
    Location
    RB(The Republic of Belarus)
    Posts
    12
    And I work under Win2000.

    May be somebody can show me working code whith correctly filled structure PDH_COUNTER_PATH_ELEMENTS?
    I... don't know where I mistake...

  15. #15
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by Punga
    And I work under Win2000.

    May be somebody can show me working code whith correctly filled structure PDH_COUNTER_PATH_ELEMENTS?
    I... don't know where I mistake...
    I do not have access to my W2k machines at the moment, but I highly doubt the above code fails under w2k. Again, START perfmon, what is the counter name (there is a point behind my madness here such as lanquage)? Can you display the cpu % in perfmon.
    Last edited by Mick; April 12th, 2004 at 04:32 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured