Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ManagementException - Invalid Class

I am having an issue querying WMI that has me completely baffled. The Application I am building utilizes WMI counters that are installed as part of the VMware View Agent into a VDI desktop. The counters provide information about the remote display protocol PCoIP.

I have working code to query the counters:

ManagementObjectSearcher searcher = new ManagementObjectSearcher();

ObjectQuery generalQuery = new ObjectQuery("SELECT * FROM Win32_PerfRawData_TeradiciPerf_PCoIPSessionGeneralStatistics");

// Many other queries...

searcher.Query = generalQuery;
foreach (ManagementObject obj in searcher.Get())
{
  // Total session duration
  ulong sessionDurationSec = Convert.ToUInt64(obj["SessionDurationSeconds"]);
  // Get other items...
}

This code is from an existing, working .NET 3.5 application. I have now created a new .NET 4.0 application, and I am using the exact same code - which fails every time with an "Invalid Class" ManagementException.

I have verified application permissions are correct, I have also tried querying other items like "Win32_Process" in the new .NET 4.0 application and it works without issue.

The puzzling thing is, the counters appear and work fine in perfmon and wmic:

http://hirstius.com/media/stackoverflow/perfmon.png

But from within WMI CIM Studio, or WMI Code Creator - nothing:

http://hirstius.com/media/stackoverflow/WMI_code_creator.png

In every way I can think of the applications are the same - same permissions int he app.manifest, exact same code being invoked in the same way (via a Timer). The only difference is the .NET 3.5 vs 4.0 - but it's clear the counters are there as more than one application can see them, yet an equal number cannot.

Does anyone know of any possible difference between .NET 3.5 and 4.0 that would cause this? Or why, possibly, the counters would be visible to certain application and not others? Is there some environmental setting I am missing that is required for .NET 4.0? Currently I do not know where to go to troubleshoot this further.

like image 456
Rex Remus Avatar asked Feb 11 '13 01:02

Rex Remus


People also ask

How do you fix an invalid class?

This is a known issue when Windows performance counters are unavailable or disabled. The primary culprit is a registry key that explicitly disables them. To fix this error, open HKLM\SYSTEM\ CurrentControlSet\Services\ PerfProc\Performance\ in regedit. Check for “Disable Performance Counters” value in the right pane.


1 Answers

As stated in the comments by Hans Passant the answer to this question is:

change the EXE project's platform target setting from x86 to AnyCPU

which is confirmed by the OP:

I would guess that means the counters are 64bit only? It would also explain why certain apps could or could not see them - based upon which platform they were targeted for.

like image 141
2 revs Avatar answered Oct 13 '22 13:10

2 revs