PdhEnumObjectsW issue

Viewed 52

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

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 set it in the PdhEnumObjectsW() call:

PDH_STATUS objStatus = PdhEnumObjectsW(nullptr, machineName.toStdWString().c_str(), nullptr, &bufLength, detailLevel, TRUE);
WCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1];
DWORD computerNameSize = _countof(computerName);
bool isComputerName = GetComputerNameExW(ComputerNameDnsFullyQualified, computerName, &computerNameSize);
qDebug() << isComputerName;
QString machineName = "";

I have removed the Windows 10 update and switched back to 19044.1645, but it still displays the PDH_CSTATUS_NO_MACHINE error. Also, I have checked it in a VM - Windows 10 build 19043, Windows 11 and Windows 11 Insider Preview, so it works well there.

From the docs, 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 registry permissions. Any ideas how to verify that all registry permissions for PDH are properly set on my machine?

I have checked out the registry permissions for Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib, and 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?

1 Answers

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

  2. 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

  3. 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.

Related