Reputation: 195
I'm attempting to run the following code:
ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_Processor");
// This line throws the exception
ManagementObjectCollection moc = mos.Get();
and I get the following excpetion:
System.Management.ManagementException: Invalid class
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
at LicenseCheckThingy.Form1.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
For reference, the system I'm running on is a Windows XP SP 3 machine with .net 3.5 installed.
The user on this machine is configured as an administrator, howerver is not using the "Administrator" account.
I created a sample project with basicaly just this code in it as a proof of error. I know from adding simple show messages around the two lines that the line mos.Get() is the one that throws the error and exception text would seem to support that by virtue of the "ManagementObjectEnumerator.MoveNext()" in the stack trace. Anyway, I'm at a loss as for what to even look for on the machine.
Note, I've run this same code on over 50+ other machines (majority of them vista or windows 7) without issue, so it seems like it would be something specific to this box. Suggestion/thought on what I could try?
More information: So I've run the following code on the machine, this causes the same exceptions, but on the for loop declaration
MessageBox.Show("pre setup"); // displays correctly
ManagementScope scope = new ManagementScope(@"\\" + Environment.MachineName + @"\root\cimv2");
//connect to the machine
scope.Connect();
MessageBox.Show("scope setup"); // displays correctly
//use a SelectQuery to tell what we're searching in
SelectQuery searchQuery = new SelectQuery("SELECT * FROM Win32_Processor");
//set the search up
ManagementObjectSearcher searcherObj = new ManagementObjectSearcher(scope, searchQuery);
MessageBox.Show("search object setup"); // displays correctly
//get the results into a collection
ManagementObjectCollection obj = searcherObj.Get();
MessageBox.Show("got ManagementObjectCollection"); // displays correctly
// next statement appears to cause Invalid class exception
foreach ( ManagementObject mo in obj )
{
try
{
MessageBox.Show("looking for device id, cpu0"); // never shows up
if ( string.Equals((string) mo["DeviceID"], "CPU0", StringComparison.InvariantCultureIgnoreCase) )
{
MessageBox.Show("processor ID: " + mo["ProcessorID"].ToString()); // never shows up
break;
}
}
catch ( Exception ex )
{
MessageBox.Show("Exception fetching processor id: " + ex.ToString()); // doesn't show
}
}
Other help??
Wasn't able to get this one figured out, it seems as thought the web site that Johnv2020 recommended that there is a bug or issue in windows XP SP3 which does not allow me to get the processor ID. For reference, the processor is acutally an i5 650 on this machine, and I've "worked around" this issue by wrapping the problem in a try catch and ignoring the processor ID in this case. Special thanks to Johnv2020 for his help!
Upvotes: 2
Views: 16096
Reputation: 849
An Invalid class exception generally occurs on WMI query when the WMI is broken. To verify it, do the following:
Look at this link to repair the WMI on windows 10: https://www.thewindowsclub.com/how-to-repair-or-rebuild-the-wmi-repository-on-windows-10
Upvotes: 0
Reputation: 2136
Works ok for me, can you try the code below & see what happens
//set the scope of this search
ManagementScope scope = new ManagementScope(@"\\" + Environment.MachineName + @"\root\cimv2");
//connect to the machine
scope.Connect();
Upvotes: 0