14

I will like to find some result after this query, but in the beginning of the foreach loop, the error "invalid class" occur.

string wmiQuery = string.Format("SELECT * FROM Win32_Process");
var searcher = new ManagementObjectSearcher(wmiQuery);
var wmiResults = searcher.Get();

foreach (ManagementObject retObject in wmiResults)
 {
   Console.WriteLine("[{0}]\tName: {1}", retObject["ProcessID"], retObject["Name"]);
 }

I use window 7 64, and i wonder if Win32_Process exists. I also use wmi code creator download it from http://www.microsoft.com/downloads/en/details.aspx?familyid=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en but i dont find any Win32_Process.

Somebody has an idea ?

Guillaume V
  • 931
  • 2
  • 10
  • 27

4 Answers4

22

I solve my problem. It seem that my WMI was corrupt. After testing WMI with this step:

  1. Click Start, click Run, type wmimgmt.msc, and then click OK.
  2. Right-click WMI Control (Local), and then click Properties.

I saw Win32_Process was a invalid class I follow this step for repair my WMI, and it work

1) In the start menu type "cmd"

2) Type "net stop winmgmt" and press Enter

3) Open a Windows Explorer and locate the path to C:\ windows\system32\WBEM\ folder and rename the Repository folder to something else like RepositoryOLD (right click and choose 'Rename Folder').

4) restart the computer

5) In the start menu type "cmd"

6) Type "net stop winmgmt" and press enter

7) Type "winmgmt /resetRepository" and restart the computer.

Guillaume V
  • 931
  • 2
  • 10
  • 27
  • 1
    Thanks a lot! I needed to use an administrator console because the stop command had to kill some additional services, except that it worked like a charm! – Florian Straub Sep 28 '16 at 12:54
  • Thanks! - For some reason every time I stopped winmgmt it started up again automatically so it wouldn't let me rename Repository - I had to quit out of Windows (hold shift while powering down from Sign-In screen) and then restart with just Command Prompt to be able to rename the file. After that your solution worked perfectly (with Admin console as well though) – komodosp Nov 03 '22 at 23:34
2

As dominus suggested, use the Process class:

...
Process[] processes = Process.GetProcesses();

foreach (Process process in processes)
    Console.WriteLine(process.ProcessName);
...
Igor Turman
  • 2,165
  • 1
  • 22
  • 25
  • 1
    not a bad idea but eventually I want the CommandLine column, like this : "SELECT ProcessId, CommandLine FROM Win32_Process WHEN ..." According to [link](http://stackoverflow.com/questions/504208/how-to-read-command-line-arguments-of-another-process-in-c) the best way to have commandline is with WMI. – Guillaume V Aug 03 '11 at 14:10
  • Except that you can't kill the process this way. You'd need to use WMI. – NapkinBob Nov 19 '17 at 19:15
0

In my opinion the exception does not sufficiently state the problem. The code below works. If you for example omit "\cimv2" in the scope the exception occurs. If you for example erroneously select from "Win32_Processes" the exception also occurs. Hence at least make sure the scope is correct and the query is correct.

ManagementScope scope = new ManagementScope(@"\\localhost\root\cimv2");
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Process");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
foreach (ManagementBaseObject eachObject in searcher.Get())
{
    Console.WriteLine("Value: {0}", eachObject);
}
0

Just to add as this is first thing that pops up on google search and having issue myself here C# wmi query exception invalid class using ManagmentObjectSearcher

Not all WMI providers have a 32 bit and 64 bit version. You may get invalid class if running a 32 bit application on 64 bit machine. I know that is not the case in this particular situation since Win32_Process has a 32 bit and 64 bit provider but figured it might be useful for some arriving here.

JMIII
  • 320
  • 2
  • 16