HighTechTalks DotNet Forums  

Access WMI in C# ?

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Access WMI in C# ? in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Henry
 
Posts: n/a

Default Access WMI in C# ? - 04-07-2007 , 02:58 PM






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.ThrowWithExt endedInfo(ManagementStat
us errorCode)
at System.Management.ManagementEventWatcher.WaitForNe xtEvent()
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.~



Reply With Quote
  #2  
Old   
Willy Denoyette [MVP]
 
Posts: n/a

Default Re: Access WMI in C# ? - 04-09-2007 , 09:44 AM






"Henry" <atnorthend_SPAM_ (AT) hotmail (DOT) com> wrote

Quote:
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.ThrowWithExt endedInfo(ManagementStat
us errorCode)
at System.Management.ManagementEventWatcher.WaitForNe xtEvent()
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.~






Notice the space at the end of ..
"TargetInstance isa \"Win32_Process\" ");

should be...
"TargetInstance isa \"Win32_Process\"");

Willy.




Reply With Quote
  #3  
Old   
Chris Mullins [MVP]
 
Posts: n/a

Default Re: Access WMI in C# ? - 04-09-2007 , 02:43 PM



[Willy doing WMI Stuff]

You always have the coolest WMI stuff handy. Have you written a FAQ on it
anywhere, or archived all your examples?

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins



Reply With Quote
  #4  
Old   
Henry
 
Posts: n/a

Default Re: Access WMI in C# ? - 04-09-2007 , 10:35 PM



Well, I wish it was that easy, corrected the space as you suggested, here is
the error...
It really seems to be a problem with the watcher, but what do I know???

Henry
Quote:
Open an application to trigger an Event.

Unhandled Exception: System.Management.ManagementException: Invalid class
at
System.Management.ManagementException.ThrowWithExt endedInfo(ManagementStat
us errorCode)
at System.Management.ManagementEventWatcher.WaitForNe xtEvent()
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 . . .

Quote:
"Willy Denoyette [MVP]" <willy.denoyette (AT) telenet (DOT) be> wrote

Quote:
"Henry" <atnorthend_SPAM_ (AT) hotmail (DOT) com> wrote in message
news:3983A93E-E059-4635-9CAC-B9329D07715E (AT) microsoft (DOT) com...
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.ThrowWithExt endedInfo(ManagementStat
us errorCode)
at System.Management.ManagementEventWatcher.WaitForNe xtEvent()
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.~







Notice the space at the end of ..
"TargetInstance isa \"Win32_Process\" ");

should be...
"TargetInstance isa \"Win32_Process\"");

Willy.



Reply With Quote
  #5  
Old   
majlathm@gmail.com
 
Posts: n/a

Default Re: Access WMI in C# ? - 04-10-2007 , 04:14 AM



On Apr 7, 8:58 pm, "Henry" <atnorthend_SP... (AT) hotmail (DOT) com> wrote:
Quote:
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.ThrowWithExt endedInfo(ManagementStat
us errorCode)
at System.Management.ManagementEventWatcher.WaitForNe xtEvent()
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.~
If you want to play around with WMI (find right syntax etc.) try to
use WMI Creator:

http://www.microsoft.com/downloads/d...displaylang=en

Marian



Reply With Quote
  #6  
Old   
Willy Denoyette [MVP]
 
Posts: n/a

Default Re: Access WMI in C# ? - 04-10-2007 , 05:41 AM



"Henry" <atnorthend_SPAM_ (AT) hotmail (DOT) com> wrote

Quote:
Well, I wish it was that easy, corrected the space as you suggested, here is the error...
It really seems to be a problem with the watcher, but what do I know???

Henry

Open an application to trigger an Event.

Unhandled Exception: System.Management.ManagementException: Invalid class
at System.Management.ManagementException.ThrowWithExt endedInfo(ManagementStat
us errorCode)
at System.Management.ManagementEventWatcher.WaitForNe xtEvent()
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 . . .

Sorry missed this one...

_InstanceCreationEvent
should be:
__InstanceCreationEvent

notice the double underscore!

Willy.



Reply With Quote
  #7  
Old   
Willy Denoyette [MVP]
 
Posts: n/a

Default Re: Access WMI in C# ? - 04-10-2007 , 05:53 AM



"Chris Mullins [MVP]" <cmullins (AT) yahoo (DOT) com> wrote

Quote:
[Willy doing WMI Stuff]

You always have the coolest WMI stuff handy. Have you written a FAQ on it anywhere, or
archived all your examples?

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins


Chris, a have an archive of samples, I just have to clean them up and add some comments, I
will save some time to do this and keep you informed here.
As for the FAQ, I keep some inter-company kind of Knowledge Base articles, but I'm afraid I
can't publish them as they are, I will see what I can do to turn this stuff into some
publically available FAQ .

Willy.




Reply With Quote
  #8  
Old   
Henry
 
Posts: n/a

Default Re: Access WMI in C# ? - 04-10-2007 , 12:19 PM



Yes, thanks for the excellent help, and I did find the WMI Creator, it is a
great help for finding proper syntax...

Again Thanks

Henry

<majlathm (AT) gmail (DOT) com> wrote

Quote:
On Apr 7, 8:58 pm, "Henry" <atnorthend_SP... (AT) hotmail (DOT) com> wrote:
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.ThrowWithExt endedInfo(ManagementStat
us errorCode)
at System.Management.ManagementEventWatcher.WaitForNe xtEvent()
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.~

If you want to play around with WMI (find right syntax etc.) try to
use WMI Creator:

http://www.microsoft.com/downloads/d...displaylang=en

Marian


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.