0

C++, Visual Studio 2019, Windows 10, SDK 10.0.22621.0

As part of my app's log file, I have collected a few bits of information about the user's computer.

I would start the query with:

static PDH_HQUERY cpuQuery;
static PDH_HCOUNTER cpuTotal;
PDH_STATUS status;
status = PdhOpenQuery(NULL, NULL, &cpuQuery);

and then get the bits of information starting with:

status = PdhAddCounter(cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal);
if (status != ERROR_SUCCESS) {
    csData += _T("GetCPURAMStatsinThread - status Add Counter Processor Time Error 2 and return *********\n");
    log_write(csData);
    return -1;
}

I just noticed that I am now getting the error from PdhAddCounter as:

0xC0000BB8 (PDH_CSTATUS_NO_OBJECT) The specified object is not found on the system.

The only thing that I can think of that has changed since this used to work was that I updated to SDK 10.0.22621.0. I believe that it worked with 10.0.17763.0.

I have not been paying attention to these lines in the log file, but when a customer had a problem that had to do with how many cores his CPU had, and how many virtual processors it had, then that is when I realized that these lines have been erroring out.

I have a laptop that had Windows 7, but I upgraded it to Windows 10, and ran the app on that, and it did not error out. So, does this mean an issue with the Windows 10 update, or the SDK update?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
ehardin
  • 47
  • 6
  • 1
    I would guess the customer is running in a non-english locale: See https://stackoverflow.com/questions/7186776/are-instance-names-for-system-performance-counters-localized – Tony Lee Nov 07 '22 at 21:20
  • 2
    Updating an SDK does n'ot usually change the behavior of existing functions. – Remy Lebeau Nov 07 '22 at 21:40
  • @Tony Lee Once I saw the missing data in my customer's log file I looked at mine and saw it was missing as well. I tried the PdhAddEnglishCounter() on my machine but got the same error. I used to work but at some point it stopped. It works on my laptop that was Win7 upgraded to Win10 but not on my desktop that is Win10 from the start. Remy Lebeau suggests that SDKs normally don't change behavior of existing functions and I would think this is correct. That only leaves Win10 updates that I have that the laptop doesn't have. No telling. I just can't think of anything else. – ehardin Nov 08 '22 at 14:57
  • @ehardin - the error is your counter path is no good - it doesn't reference an object. You can use perfmon to verify the element/instance names of that counter. Or the documentation from MS tells you how to browse the counters. If the code sample from MS doesn't work, I don't know what to tell you. – Tony Lee Nov 08 '22 at 16:22
  • @Tony Lee I checked the Windows update history of both my desktop and the laptop and both seem to be up to date. I'll try to browse the counters but I wonder why they would change. "\\Processor(_Total)\\% Processor Time" used to work. Oh well. I'll take a look. – ehardin Nov 08 '22 at 16:43
  • @Tony Lee I ran the dialog to browse the counters using the sample code from https://learn.microsoft.com/en-us/windows/win32/perfctrs/browsing-performance-counters – ehardin Nov 08 '22 at 17:09
  • The only thing I noticed was that it listed Processor Information rather than just Processor and &Processor Time rather than having the space after the %. I tried the 4 variations of changing these 2 variables and kept getting the same error. No telling.... – ehardin Nov 08 '22 at 17:12
  • @Tony Lee I didn't continue with the example code cited above. Looking at the buffer after making my selection in the dialog I get NULL unless I select . There is a _Total choice but, if I select that I get an empty buffer but if I selecte I get the buffer L"\\Processor Information(*)\\% Processor Time" and that works when I plug that string directly into PdhAddEnglishCounter() Very strange but at least it that I have a fix. – ehardin Nov 08 '22 at 17:49

1 Answers1

0

Per my comments above with @Tony Lee I used the MS sample code to browse the counters on my local computer. There was a Processor Information selection vs my original Processor under which there was a Processor Time selection. In the choice box below that there was an all instances choice and a _Total choice. When I selected the _Total choice the buffer in the sample code stayed as NULL but if I selected the all instances the buffer filled with:

L"\Processor Information(*)\% Processor Time"

Plugging that string into PdhAddEnglishCounter() worked... Edit it also worked with PdhAddCounter()

Using Processor instead of Processor Information and (_Total) Instead of (*) used to work in Windows 10. No telling why things have changed at least on some computer.

Ed

EDIT Important note. First is that the new code above also works on the laptop on which the original code worked. Second note is that I just realized that the desktop on which the original code failed is Windows 10 Home whereas the laptop on which the original code worked is Windows 10 Pro. That maybe the difference. Regardless, the new code works on both Home and Pro.

EDIT 2 The new code also ran fine on Windows 11 Home. I also see that my customer in whose log file I noticed the error line was on Windows 11 Home. That would insinuate that the Pro version still works with the legacy (see next comment by Tony Lee) Processor while the Home versions do not work with the legacy Processor but only with the new Processor Information

ehardin
  • 47
  • 6
  • It seems Processor is legacy, only there for backward compatibility, and finally removed. Processor Information is the new hotness since Server 2008. https://techcommunity.microsoft.com/t5/running-sap-applications-on-the/windows-2008-r2-performance-monitor-8211-processor-information/ba-p/367007 – Tony Lee Nov 08 '22 at 18:27
  • That explains a lot. Or at least it explains whey **Processor** wouldn't work but not why the **_Total** didn't work. On well...all's well that ends well. – ehardin Nov 08 '22 at 19:58