PdhEnumObjectsW issue

Ruslan Sydorovych 86 Reputation points
2022-05-08T21:39:33.45+00:00

Hello!

I have issue with enumerating performance counter objects. My code worked well for a few years but recently I have found out it started to fail to get counter objects with the following issue:

PDH_CSTATUS_NO_MACHINE The path did not contain a computer name, and the function was unable to retrieve the local computer name.

 DWORD bufLength = 0;  
 const DWORD detailLevel = PERF_DETAIL_WIZARD;  
 PDH_STATUS objStatus = PdhEnumObjectsW(nullptr, nullptr, nullptr, &bufLength, detailLevel, TRUE);  
 qDebug() << ManageApp::getPdhStatusMsg(objStatus);  
 qDebug() << "bufLength: " << bufLength;  
 std::wstring namebuf(bufLength, '\0');  
 PDH_STATUS status = PdhEnumObjectsW(nullptr, nullptr, &namebuf[0], &bufLength, detailLevel, FALSE);  
 qDebug() << ManageApp::getPdhStatusMsg(status);  

I have tried to get the computer name and setting it in the PdhEnumObjectsW: PDH_STATUS objStatus = PdhEnumObjectsW(nullptr, machineName.toStdWString().c_str(), nullptr, &bufLength, detailLevel, TRUE);

    TCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1];  
    DWORD computerNameSize = sizeof(computerName);  
    GetComputerNameW(computerName, &computerNameSize);  
    QString machineName = QString("\\%1").arg(QString::fromStdWString(computerName));  
    qDebug() << "machineName: " << machineName;  

It still does not work. Any ideas how to fix this issue? Thank you.

Updated:
I have removed the update and switched back to 19044.1645 but it still displays this issue: PDH_CSTATUS_NO_MACHINE The path did not contain a computer name, and the function was unable to retrieve the local computer name.

From the docs (https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhconnectmachinea), I get following error description:
PDH_CSTATUS_NO_MACHINE

Unable to connect to the specified computer. Could be caused by the computer not being on, not supporting PDH, not being connected to the network, or having the permissions set on the registry that prevent remote connections or remote performance monitoring by the user.

So, I think it's somehow related to the registry permissions. Any ideas how to verify that all registry permissions for PDH are properly set on my machine? Thanks.

Updated 2:
I have checked out the registry permissions - Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib
All permissions are set properly. So, I do not think, it is related to the registry permissions. Any ideas what could cause such PDH_CSTATUS_NO_MACHINE issue? Thank you.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,430 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,544 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ruslan Sydorovych 86 Reputation points
    2022-05-09T22:08:25.94+00:00

    I have fixed it by rebuilding the performance counters.

    Instructions:

    1. Set "Disable Performance Counters" to 0 using this command: Reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PerfProc\Performance /v "Disable Performance Counters" /t REG_DWORD /d 0
      1. Rebuild all performance counters:
      %windir%\system32\lodctr /R %windir%\sysWOW64\lodctr /R

    When running the first command - %windir%\system32\lodctr /R, you will get:

    Error: Unable to rebuild performance counter setting from system
    backup store, error code is 2

    In such case, feel free run this command first - %windir%\sysWOW64\lodctr /R and then run %windir%\system32\lodctr /R. The second time after this command: %windir%\sysWOW64\lodctr /R it will complete successfully for %windir%\system32\lodctr /R:

    Info: Successfully rebuilt performance counter setting from system
    backup store

    1. Resync the counters with Windows Management Instrumentation (WMI): %windir%\system32\wbem\winmgmt.exe /resyncperf
    2. Stop and restart the Performance Logs and Alerts service with the following commands: net stop pla net start pla
      1. Stop and restart the Windows Management Instrumentation (WMI) service by using these commands:
      net stop winmgmt net start winmgmt

    So, the issue is resolved. Thanks.

    0 comments No comments