ManagementObjectSearcher Get() Method throws an exception

13,210

Solution 1

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();

Solution 2

An Invalid class exception generally occurs on WMI query when the WMI is broken. To verify it, do the following:

  1. Start-->Run-->Wmimgmt.msc and press enter
  2. In the left most pane of the window that opens, Right click on WMI Control(local) and select properties.
  3. If the properties windows displays any error message, it confirms that WMI is broken on windows.

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

Share:
13,210
Beau
Author by

Beau

Updated on June 04, 2022

Comments

  • Beau
    Beau almost 2 years

    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!

  • Beau
    Beau almost 13 years
    Both command run to completion, no exceptions of any sort on the machine.. ?
  • Johnv2020
    Johnv2020 almost 13 years
    That's very strange. What does the following return
  • Johnv2020
    Johnv2020 almost 13 years
    ManagementScope scope = new ManagementScope(@"\\" + Environment.MachineName + @"\root\cimv2"); //connect to the machine scope.Connect(); //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); //get the results into a collection using (ManagementObjectCollection obj = searcherObj.Get()) {
  • Beau
    Beau almost 13 years
    That's very interesting, it seemed to run without error as well... I'm not very familiar with this WMI would this, in essense, return the same information as my object searcher? it it just localized to the current machine in this case then? is it trying to go to the network for some reason in this case?
  • Beau
    Beau almost 13 years
    I've taken this idea and tried to get the data I needed and without success, see the code snipit I've added above...
  • Johnv2020
    Johnv2020 almost 13 years
    Hi - check out this link seems to indicate that its a problem with the CPU type & SP3 - news.softpedia.com/news/…