Discussion:
WMI and C# Illegal class error
(too old to reply)
Henry
2007-04-08 17:34:22 UTC
Permalink
Below is the code I am trying to use in a console application, each time I
run it I get an Management error Illegal Class error. I have traced the
error to the \"Win32_Process\" line, apparently the wmi path is incorrect.
Tried to run the app on both xp and vista with the same error. Could
someone suggest how I might correct this wmi path variable???

Thanks

Henry


+++++++++++++++++++++++++++++++++++++++++++++++++++++

public static void WriteToEventLog()
{
WqlEventQuery DemoQuery = new
WqlEventQuery("_InstanceCreationEvent", new TimeSpan(0, 0, 1),
"TargetInstance isa \"Win32_Process\" ");

ManagementEventWatcher DemoWatcher = new
ManagementEventWatcher();
DemoWatcher.Query = DemoQuery;
DemoWatcher.Options.Timeout = new TimeSpan(0, 0, 30);

Console.WriteLine("Open an application to trigger an Event.");

ManagementBaseObject e = DemoWatcher.WaitForNextEvent();

EventLog DemoLog = new EventLog("Chap10Demo");
DemoLog.Source = "Chap10Demo";
String EventName =
((ManagementBaseObject)e["TargetInstance"])["Name"].ToString();
Console.WriteLine(EventName);
DemoLog.WriteEntry(EventName, EventLogEntryType.Information);

DemoWatcher.Stop();
}


+++++++++++++++++++++++++++++++Error follows

Open an application to trigger an Event.

Unhandled Exception: System.Management.ManagementException: Invalid class
at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStat
us errorCode)
at System.Management.ManagementEventWatcher.WaitForNextEvent()
at ManagementEvents1.Program.WriteToEventLog() in
I:\Projects\ManagementEvent
s1\ManagementEvents1\Program.cs:line 29
at ManagementEvents1.Program.Main(String[] args) in
I:\Projects\ManagementEve
nts1\ManagementEvents1\Program.cs:line 14
Press any key to continue . . .
--
~
Most good judgment comes from experience.
Most experience comes from bad judgment.
~I'm leaning towards bad judgment.~
Henry
2007-04-09 03:48:38 UTC
Permalink
Tried some new code from microsoft, throws exception on the watcher.Start()
line.... says it's an invalid class....(note, running this code on Vista
Business)

Would appreciate a bit of guidance
Thanks

Code causing exception below.
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;


namespace ManegementEvents2
{
class Program
{
static void Main(string[] args)
{
ManagementClass processClass = new
ManagementClass(@"root\cimv2:Win32_Process");

foreach (ManagementBaseObject processInstance in
processClass.GetInstances())
{
Console.WriteLine(processInstance["Caption"].ToString());

}

ManagementEventWatcher watcher = new ManagementEventWatcher(new
WqlEventQuery("Select * FROM _InstanceCreationEvent WITHIN 1 " +
@"WHERE TargetInstance ISA ""Win32_Process"""));

EventHandler handler = new EventHandler();
watcher.EventArrived += new
EventArrivedEventHandler(handler.Arrived);
watcher.Start();

System.Threading.Thread.Sleep(65000);
watcher.Stop();
Console.WriteLine("Done Waching for Events!");


}
public class EventHandler
{
public void Arrived(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Process Created = " +
((ManagementBaseObject)e.NewEvent["TargetInstance"])["Caption"]);
}
}


}
}
Post by Henry
Below is the code I am trying to use in a console application, each time I
run it I get an Management error Illegal Class error. I have traced the
error to the \"Win32_Process\" line, apparently the wmi path is incorrect.
Tried to run the app on both xp and vista with the same error. Could
someone suggest how I might correct this wmi path variable???
Thanks
Henry
+++++++++++++++++++++++++++++++++++++++++++++++++++++
public static void WriteToEventLog()
{
WqlEventQuery DemoQuery = new
WqlEventQuery("_InstanceCreationEvent", new TimeSpan(0, 0, 1),
"TargetInstance isa \"Win32_Process\" ");
ManagementEventWatcher DemoWatcher = new
ManagementEventWatcher();
DemoWatcher.Query = DemoQuery;
DemoWatcher.Options.Timeout = new TimeSpan(0, 0, 30);
Console.WriteLine("Open an application to trigger an Event.");
ManagementBaseObject e = DemoWatcher.WaitForNextEvent();
EventLog DemoLog = new EventLog("Chap10Demo");
DemoLog.Source = "Chap10Demo";
String EventName =
((ManagementBaseObject)e["TargetInstance"])["Name"].ToString();
Console.WriteLine(EventName);
DemoLog.WriteEntry(EventName, EventLogEntryType.Information);
DemoWatcher.Stop();
}
+++++++++++++++++++++++++++++++Error follows
Open an application to trigger an Event.
Unhandled Exception: System.Management.ManagementException: Invalid class
at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStat
us errorCode)
at System.Management.ManagementEventWatcher.WaitForNextEvent()
at ManagementEvents1.Program.WriteToEventLog() in
I:\Projects\ManagementEvent
s1\ManagementEvents1\Program.cs:line 29
at ManagementEvents1.Program.Main(String[] args) in
I:\Projects\ManagementEve
nts1\ManagementEvents1\Program.cs:line 14
Press any key to continue . . .
--
~
Most good judgment comes from experience.
Most experience comes from bad judgment.
~I'm leaning towards bad judgment.~
Loading...