WQLEventQuery:Second local connect:Access denied![II]

M

Manfred Braun

Hi All,

I've already asked another question [wmi newsgroup only], but this is
finally the same issue. I cannot execute two asynchron WMI queries on the
local machine, one after the other. The original question is based on
complicated code. So I reduced the "kernel" to a small C# sample [compilable
to exe], which shows the problem. The question is:

- Is this a problem with my code, as not freeing resources etc?
- Is this possibly a WMI issue?

In the original code, I tried to remove the querying class completely, but
even this does not help. Each second query fails.

Any help would really become very welcomed, each simple tip or hint!!!

Best regards,
Manfred Braun

(Private)
Lange Roetterstrasse 7
D68167 Mannheim
Germany

mailto:[email protected]
(Remove the anti-spam-underscore to mail me!)


Annotation:The executable has to be called with an computername as an
argument. The exe should work with each remote machine, but not with the
local one.

/*
Name: WMIAsyncQuerySampleReconnectSample5a.cs
*/
using System;
using System.Management;

class Sample_ManagementEventWatcher
{
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;

//Start watching for events:
watcher.Start();
Console.WriteLine("Watcher is running, press <enter> to stop...");
Console.ReadLine();
//Stop watching:
watcher.Stop();
watcher.EventArrived -= eventArrivedEventHandler;

mh = null;
watcher = null;
eventArrivedEventHandler = null;

Console.WriteLine("Watcher is stopped, press <enter> to re-start...");
Console.ReadLine();

mh = new MyHandler();
eventArrivedEventHandler = new EventArrivedEventHandler(mh.Arrived);
watcher = Sample_ManagementEventWatcher.getWatcher(comp);
watcher.EventArrived += eventArrivedEventHandler;

//Start watching for events:
try
{
watcher.Start(); //BOMBs here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Console.WriteLine("Watcher is running, press <enter> to stop...");
Console.ReadLine();
//Stop watching:
watcher.Stop();
watcher.EventArrived -= eventArrivedEventHandler;
}
catch(Exception e)
{
Console.WriteLine("Exception:{0}", e.ToString());
}
}
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);

co = null;
mp = null;
ms = null;
EventQuery = null;
wql = null;

return watcher;
}

public class MyHandler
{
public void Arrived(object sender, EventArrivedEventArgs e)
{
ManagementBaseObject mbo =
(ManagementBaseObject)e.NewEvent["TargetInstance"];
Console.WriteLine("Event:{0}", mbo["message"]);
}

}//Inner class.
}//Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top