2018年8月10日 星期五

[C++] Get PdhAddCounter by different international languages

In PdhAddCounter need adds the specified counter to the query.

The response -1073738824 means PDH_CSTATUS_NO_OBJECT=0xC0000BB8, using a non-english OS.

The system uses counters to collect performance data. Each counter is uniquely identified through its name and its path, or location. The syntax of a counter path is.

	PDH_STATUS status = ERROR_SUCCESS;
	WCHAR * PDHCounterName = new WCHAR[MAX_PATH];
	std::wmemset(PDHCounterName, 0, sizeof(PDHCounterName));
	
	std::wstring ProcessExampleName = L"ExampleProcess";
	std::wstring ObjectName = GetPerformanceCounterNamebyIndex(PerformanceObjectName);
	std::wstring CounterName = GetPerformanceCounterNamebyIndex(PerformanceCounterName);
		
	HQUERY hQuery = NULL;
	status = PdhOpenQuery(NULL, 0, &hQuery);

	if (ERROR_SUCCESS != status)
	{
		wprintf(L"PdhOpenQuery failed with 0x%x", status);
	}
	//\Process(ExampleProcess)\% Processor Time (English)
	//\Prozess(ExampleProcess)\% Prozessor zeit (Deutsch Maybe this)
	wsprintf(PDHCounterName, TEXT("\\%s(%s)\\%s"), ObjectName.c_str(), ProcessExampleName.c_str(), CounterName.c_str());
	
	HCOUNTER hCounter = NULL;
	status = PdhAddCounter(hQuery, PDHCounterName, 0, &hCounter);

	if (ERROR_SUCCESS != status)
	{
		wprintf(L"PdhAddCounter failed with 0x%x\n", status);
		reval = false;
	}

The index value that you specify must match one of the index values associated with the objects or counters that were loaded on the computer. The index/name value pairs are stored in the Counters registry value in the following registry location.

In Register Key Path: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009

PerformanceObjectName 230 //is Process
PerformanceCounterName 6 //is Processor Time

By the GetPerformanceCounterNamebyIndex:
std::wstring CpuUsageInfo::GetPerformanceCounterNamebyIndex(int index)
{
	PDH_STATUS status = ERROR_SUCCESS;
	WCHAR szObjectName[MAX_PATH] = L"";
	DWORD dwSize = sizeof(szObjectName);
	OptationFunction_Class object;

	if (status = PdhLookupPerfNameByIndex(NULL, index, szObjectName, &dwSize))
	{
		wprintf(L"PdhLookupPerfNameByIndex failed with 0x%x, LastError %ld\n", status, GetLastError());
	}
	return szObjectName;
}

沒有留言:

張貼留言