c++ PdhAddCounter on 64bit os

Started by
1 comment, last by frob 11 years, 7 months ago
We have our own custom server software on windows server 2003 and it makes use of PdhAddCounter with "\\Processor(_Total)\\% Processor Time". Yesterday we got a new server with windows 2008 and its now 64bit. Our code has way to much to change and recompile as 64bit and were not by far the most experienced at going from 32bit code to 64bit. One the new server and some other machines it seems like everything works fine except for this code it always gets PDH_INVALID_DATA and if i ignore that and just try to get the value its always zero. Does anybody know how this can be fixed in the 32bit system instead of trying to change everything into 64bit not even knowing if that will fix the problem.
	HQUERY hQuery;
	HCOUNTER hCounter;
	
	PDH_FMT_COUNTERVALUE FmtValue;
	
	PdhOpenQuery(NULL, 0, &hQuery);
	
	PdhAddCounter(hQuery, "\\Processor(_Total)\\% Processor Time", 0, &hCounter);
	
	PdhCollectQueryData(hQuery);
	
	//PDH_FMT_DOUBLE
	//PDH_FMT_LARGE
	PDH_STATUS hold=PdhGetFormattedCounterValue(hCounter, PDH_FMT_LARGE, NULL, &FmtValue);
	if (hold != ERROR_SUCCESS){
		
		if (hold == PDH_MORE_DATA){
			std::cout << "PDH_MORE_DATA\n";
		}
		if (hold == PDH_INVALID_ARGUMENT){
			std::cout << "PDH_INVALID_ARGUMENT\n";
		}
		if (hold == PDH_INVALID_HANDLE){
			std::cout << "PDH_INVALID_HANDLE\n";
		}
		if (hold == PDH_INVALID_DATA){

			std::cout << "PDH_INVALID_DATA\n";
		}
	}
	PdhCloseQuery(hQuery);
	unsigned long long data = FmtValue.largeValue;
Advertisement
Did you solved this problem? I found the solution...

This is related with the OS language in 64 bits

Sample code:

PDH_STATUS status;
CONST PWSTR COUNTER_PATH = L"\\Processor(_Total)\\% processor time"; //C:\Windows\system32>typeperf -q Processor
CONST PWSTR COUNTER_PATH_PT = L"\\processador(_Total)\\% de tempo do processador"; //C:\Windows\system32>typeperf -q processador

// Initialize the flag indicating whether this object can read the system cpu usage or not.
m_canReadCpu = true;
// Create a query object to poll cpu usage.
status = PdhOpenQuery(NULL, 0, &m_queryHandle);
if(status != ERROR_SUCCESS)
m_canReadCpu = false;
// Set query object to poll all cpus in the system.
status = PdhAddCounter(m_queryHandle, COUNTER_PATH, 0, &m_counterHandle); // Win7 ENG
if(status != ERROR_SUCCESS)
status = PdhAddCounter(m_queryHandle, COUNTER_PATH_PT, 0, &m_counterHandle);// Win7 PT (64 bits only!)
// TODO... do the same for other languages...
if(status != ERROR_SUCCESS)
m_canReadCpu = false;
Please look at the date of posts when replying.

This post was two and a half years old.

Closing the necro thread.

This topic is closed to new replies.

Advertisement