G
Guest
Hi All,
I have allready tried to ask a similar question [WQLEventQuery:Second local
connect:Access denied!], but got no answer until now. In the meantime, I
found, that I cannot understand some thread-settings for the Main() function
[I am using C#].
If I use the [STAThread] attribute for the Main() function, I get "access
denied error", if I use a ManagementEventWatcher to connect to the local
machine to receive events. Is there anybody out there, how possibly can
explain why this happens?? If I remove this attribute or use [MTAThread]
instead it works one time.
I append a short sample at the end.
Some help would be really very good.
Thanks so far and
best regards
Best regards,
Manfred Braun
(Private)
Lange Roetterstrasse 7
D68167 Mannheim
Germany
mailto:[email protected]
Phone : +49-621-37 53 86
(Remove the anti-spam-underscore to mail me!)
/*
Name: WMIAsyncQuerySampleReconnectSample5a.cs
*/
using System;
using System.Management;
class Sample_ManagementEventWatcher
{
[STAThread]
public static int Main(string[] args)
{
if(args.Length != 0)
{
string comp = args[0];
MyHandler mh;
EventArrivedEventHandler eventArrivedEventHandler;
ManagementEventWatcher watcher;
mh = new MyHandler();
eventArrivedEventHandler = new EventArrivedEventHandler(mh.Arrived);
watcher = Sample_ManagementEventWatcher.getWatcher(comp);
watcher.EventArrived += eventArrivedEventHandler;
watcher.Start(); //Access denied!
Console.WriteLine("Watcher is running, press <enter> to stop...");
Console.ReadLine();
watcher.Stop();
watcher.EventArrived -= eventArrivedEventHandler;
}
else
Console.WriteLine("Args!!! [P1=Computername]");
return 0;
}
public static ManagementEventWatcher getWatcher(string comp)
{
ConnectionOptions co;
ManagementPath mp;
ManagementScope ms;
WqlEventQuery EventQuery;
ManagementEventWatcher watcher;
string wql;
co = new ConnectionOptions();
co.Timeout = new TimeSpan(0, 0, 60);
co.EnablePrivileges = true;
mp = new ManagementPath();
mp.NamespacePath = @"\root\cimv2";
mp.Server = comp;
ms = new ManagementScope(mp, co);
wql = "select * from __instancecreationevent where targetinstance isa
'Win32_NTLogEvent'";
EventQuery = new WqlEventQuery(wql);
watcher = new ManagementEventWatcher(ms, EventQuery);
return watcher;
}
public class MyHandler
{
public void Arrived(object sender, EventArrivedEventArgs e)
{
ManagementBaseObject mbo =
(ManagementBaseObject)e.NewEvent["TargetInstance"];
Console.WriteLine("Event:{0}", mbo["message"]);
}
}
}//Namespace.
I have allready tried to ask a similar question [WQLEventQuery:Second local
connect:Access denied!], but got no answer until now. In the meantime, I
found, that I cannot understand some thread-settings for the Main() function
[I am using C#].
If I use the [STAThread] attribute for the Main() function, I get "access
denied error", if I use a ManagementEventWatcher to connect to the local
machine to receive events. Is there anybody out there, how possibly can
explain why this happens?? If I remove this attribute or use [MTAThread]
instead it works one time.
I append a short sample at the end.
Some help would be really very good.
Thanks so far and
best regards
Best regards,
Manfred Braun
(Private)
Lange Roetterstrasse 7
D68167 Mannheim
Germany
mailto:[email protected]
Phone : +49-621-37 53 86
(Remove the anti-spam-underscore to mail me!)
/*
Name: WMIAsyncQuerySampleReconnectSample5a.cs
*/
using System;
using System.Management;
class Sample_ManagementEventWatcher
{
[STAThread]
public static int Main(string[] args)
{
if(args.Length != 0)
{
string comp = args[0];
MyHandler mh;
EventArrivedEventHandler eventArrivedEventHandler;
ManagementEventWatcher watcher;
mh = new MyHandler();
eventArrivedEventHandler = new EventArrivedEventHandler(mh.Arrived);
watcher = Sample_ManagementEventWatcher.getWatcher(comp);
watcher.EventArrived += eventArrivedEventHandler;
watcher.Start(); //Access denied!
Console.WriteLine("Watcher is running, press <enter> to stop...");
Console.ReadLine();
watcher.Stop();
watcher.EventArrived -= eventArrivedEventHandler;
}
else
Console.WriteLine("Args!!! [P1=Computername]");
return 0;
}
public static ManagementEventWatcher getWatcher(string comp)
{
ConnectionOptions co;
ManagementPath mp;
ManagementScope ms;
WqlEventQuery EventQuery;
ManagementEventWatcher watcher;
string wql;
co = new ConnectionOptions();
co.Timeout = new TimeSpan(0, 0, 60);
co.EnablePrivileges = true;
mp = new ManagementPath();
mp.NamespacePath = @"\root\cimv2";
mp.Server = comp;
ms = new ManagementScope(mp, co);
wql = "select * from __instancecreationevent where targetinstance isa
'Win32_NTLogEvent'";
EventQuery = new WqlEventQuery(wql);
watcher = new ManagementEventWatcher(ms, EventQuery);
return watcher;
}
public class MyHandler
{
public void Arrived(object sender, EventArrivedEventArgs e)
{
ManagementBaseObject mbo =
(ManagementBaseObject)e.NewEvent["TargetInstance"];
Console.WriteLine("Event:{0}", mbo["message"]);
}
}
}//Namespace.