473,708 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help with [STAThread], WMI and access denied

Hi All,

I have allready tried to ask a similar question [WQLEventQuery:S econd 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 ManagementEvent Watcher 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:_m****** *******@manfbra un.de
Phone : +49-621-37 53 86
(Remove the anti-spam-underscore to mail me!)

/*
Name: WMIAsyncQuerySa mpleReconnectSa mple5a.cs
*/

using System;
using System.Manageme nt;

class Sample_Manageme ntEventWatcher
{
[STAThread]
public static int Main(string[] args)
{
if(args.Length != 0)
{
string comp = args[0];

MyHandler mh;
EventArrivedEve ntHandler eventArrivedEve ntHandler;
ManagementEvent Watcher watcher;

mh = new MyHandler();
eventArrivedEve ntHandler = new EventArrivedEve ntHandler(mh.Ar rived);
watcher = Sample_Manageme ntEventWatcher. getWatcher(comp );
watcher.EventAr rived += eventArrivedEve ntHandler;

watcher.Start() ; //Access denied!
Console.WriteLi ne("Watcher is running, press <enter> to stop...");
Console.ReadLin e();
watcher.Stop();
watcher.EventAr rived -= eventArrivedEve ntHandler;
}
else
Console.WriteLi ne("Args!!! [P1=Computername]");

return 0;
}

public static ManagementEvent Watcher getWatcher(stri ng comp)
{
ConnectionOptio ns co;
ManagementPath mp;
ManagementScope ms;
WqlEventQuery EventQuery;
ManagementEvent Watcher watcher;
string wql;

co = new ConnectionOptio ns();
co.Timeout = new TimeSpan(0, 0, 60);
co.EnablePrivil eges = true;

mp = new ManagementPath( );
mp.NamespacePat h = @"\root\cimv 2";
mp.Server = comp;
ms = new ManagementScope (mp, co);

wql = "select * from __instancecreat ionevent where targetinstance isa
'Win32_NTLogEve nt'";
EventQuery = new WqlEventQuery(w ql);

watcher = new ManagementEvent Watcher(ms, EventQuery);

return watcher;
}
public class MyHandler
{
public void Arrived(object sender, EventArrivedEve ntArgs e)
{
ManagementBaseO bject mbo =
(ManagementBase Object)e.NewEve nt["TargetInstance "];
Console.WriteLi ne("Event:{0}" , mbo["message"]);
}
}

}//Namespace.

Nov 15 '05 #1
9 6329
Works for me with both, STA and MTAThread attribute set.

What OS are you runnng?

Willy.
<__*******@be.d e> wrote in message
news:uE******** ******@TK2MSFTN GP09.phx.gbl...
Hi All,

I have allready tried to ask a similar question [WQLEventQuery:S econd 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 ManagementEvent Watcher 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:_m****** *******@manfbra un.de
Phone : +49-621-37 53 86
(Remove the anti-spam-underscore to mail me!)

/*
Name: WMIAsyncQuerySa mpleReconnectSa mple5a.cs
*/

using System;
using System.Manageme nt;

class Sample_Manageme ntEventWatcher
{
[STAThread]
public static int Main(string[] args)
{
if(args.Length != 0)
{
string comp = args[0];

MyHandler mh;
EventArrivedEve ntHandler eventArrivedEve ntHandler;
ManagementEvent Watcher watcher;

mh = new MyHandler();
eventArrivedEve ntHandler = new EventArrivedEve ntHandler(mh.Ar rived);
watcher = Sample_Manageme ntEventWatcher. getWatcher(comp );
watcher.EventAr rived += eventArrivedEve ntHandler;

watcher.Start() ; //Access denied!
Console.WriteLi ne("Watcher is running, press <enter> to stop...");
Console.ReadLin e();
watcher.Stop();
watcher.EventAr rived -= eventArrivedEve ntHandler;
}
else
Console.WriteLi ne("Args!!! [P1=Computername]");

return 0;
}

public static ManagementEvent Watcher getWatcher(stri ng comp)
{
ConnectionOptio ns co;
ManagementPath mp;
ManagementScope ms;
WqlEventQuery EventQuery;
ManagementEvent Watcher watcher;
string wql;

co = new ConnectionOptio ns();
co.Timeout = new TimeSpan(0, 0, 60);
co.EnablePrivil eges = true;

mp = new ManagementPath( );
mp.NamespacePat h = @"\root\cimv 2";
mp.Server = comp;
ms = new ManagementScope (mp, co);

wql = "select * from __instancecreat ionevent where targetinstance isa
'Win32_NTLogEve nt'";
EventQuery = new WqlEventQuery(w ql);

watcher = new ManagementEvent Watcher(ms, EventQuery);

return watcher;
}
public class MyHandler
{
public void Arrived(object sender, EventArrivedEve ntArgs e)
{
ManagementBaseO bject mbo =
(ManagementBase Object)e.NewEve nt["TargetInstance "];
Console.WriteLi ne("Event:{0}" , mbo["message"]);
}
}

}//Namespace.

Nov 15 '05 #2
Hello Willy,

oh, yes and sorry for my inaccuracy about the environment. I am using
Windows 2000 Server, english, SP3, dot.net runtime 1.0, english too. I am
working in an NT4 domain with logged-on account domain admin and the W2K
machine are domain members.

And my chain of experiments always seems to return different results ....
I used two production machines, just to be sure, that not some fumbling ;-)
on my admin machine have caused that. Accidentally, on the next machine, I
found a new behavior:Both versions are not running. After finding and
removing some <account deleted> entries from the ACLs with DCOMCNFG, the
behavior return to that, what I originally described and this is on all my
W2K machines now really the same. But this last experiment tells me, it
could have something to do with DCOM and/or WMI settings, which are normally
remains at the OS defaults.

Thanks so far and
best regards,
Manfred

"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:Op******** ******@TK2MSFTN GP12.phx.gbl...
Works for me with both, STA and MTAThread attribute set.

What OS are you runnng?

Willy.
<__*******@be.d e> wrote in message
news:uE******** ******@TK2MSFTN GP09.phx.gbl...
Hi All,

I have allready tried to ask a similar question [WQLEventQuery:S econd

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 ManagementEvent Watcher 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:_m****** *******@manfbra un.de
Phone : +49-621-37 53 86
(Remove the anti-spam-underscore to mail me!)

/*
Name: WMIAsyncQuerySa mpleReconnectSa mple5a.cs
*/

using System;
using System.Manageme nt;

class Sample_Manageme ntEventWatcher
{
[STAThread]
public static int Main(string[] args)
{
if(args.Length != 0)
{
string comp = args[0];

MyHandler mh;
EventArrivedEve ntHandler eventArrivedEve ntHandler;
ManagementEvent Watcher watcher;

mh = new MyHandler();
eventArrivedEve ntHandler = new EventArrivedEve ntHandler(mh.Ar rived);
watcher = Sample_Manageme ntEventWatcher. getWatcher(comp );
watcher.EventAr rived += eventArrivedEve ntHandler;

watcher.Start() ; //Access denied!
Console.WriteLi ne("Watcher is running, press <enter> to stop...");
Console.ReadLin e();
watcher.Stop();
watcher.EventAr rived -= eventArrivedEve ntHandler;
}
else
Console.WriteLi ne("Args!!! [P1=Computername]");

return 0;
}

public static ManagementEvent Watcher getWatcher(stri ng comp)
{
ConnectionOptio ns co;
ManagementPath mp;
ManagementScope ms;
WqlEventQuery EventQuery;
ManagementEvent Watcher watcher;
string wql;

co = new ConnectionOptio ns();
co.Timeout = new TimeSpan(0, 0, 60);
co.EnablePrivil eges = true;

mp = new ManagementPath( );
mp.NamespacePat h = @"\root\cimv 2";
mp.Server = comp;
ms = new ManagementScope (mp, co);

wql = "select * from __instancecreat ionevent where targetinstance isa
'Win32_NTLogEve nt'";
EventQuery = new WqlEventQuery(w ql);

watcher = new ManagementEvent Watcher(ms, EventQuery);

return watcher;
}
public class MyHandler
{
public void Arrived(object sender, EventArrivedEve ntArgs e)
{
ManagementBaseO bject mbo =
(ManagementBase Object)e.NewEve nt["TargetInstance "];
Console.WriteLi ne("Event:{0}" , mbo["message"]);
}
}

}//Namespace.



Nov 15 '05 #3
Hi All,

interestingly, in one of my latest tests, I found privilege failure audit in
the eventlog, claming the denied access to the right SeSecurityPrivi lege
......

With this in mind, I changed the query in my sample

from:
select * from __instancecreat ionevent where targetinstance isa
'Win32_NTLogEve nt'";

to:
"select * from __instancecreat ionevent where TargetInstance isa
'Win32_NTLogEve nt' and TargetInstance. LogFile = 'Application'";

This is working. But this means, that setting the
ConnectionOptio ns.EnablePrivil eges = true has no effect using the
ManagementEvent Watcher. But in the constructor, I pass a ManagementScope ,
with this option set!

I found no information inside the SDK in the EventWatcherOpt ions or the
WqlEventQuery about security. But even all this does not explain that after
a first connect, a second one fails and does not explain what this has to do
with setting the [xxxThread] option for my Main() function.

Any help would be really very welcomed!

Thanks so far and
best regards,
Manfred
"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:OK******** *****@tk2msftng p13.phx.gbl...
Hello Willy,

oh, yes and sorry for my inaccuracy about the environment. I am using
Windows 2000 Server, english, SP3, dot.net runtime 1.0, english too. I am
working in an NT4 domain with logged-on account domain admin and the W2K
machine are domain members.

And my chain of experiments always seems to return different results ....
I used two production machines, just to be sure, that not some fumbling ;-) on my admin machine have caused that. Accidentally, on the next machine, I
found a new behavior:Both versions are not running. After finding and
removing some <account deleted> entries from the ACLs with DCOMCNFG, the
behavior return to that, what I originally described and this is on all my
W2K machines now really the same. But this last experiment tells me, it
could have something to do with DCOM and/or WMI settings, which are normally remains at the OS defaults.

Thanks so far and
best regards,
Manfred

"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:Op******** ******@TK2MSFTN GP12.phx.gbl...
Works for me with both, STA and MTAThread attribute set.

What OS are you runnng?

Willy.
<__*******@be.d e> wrote in message
news:uE******** ******@TK2MSFTN GP09.phx.gbl...
Hi All,

I have allready tried to ask a similar question [WQLEventQuery:S econd

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 ManagementEvent Watcher 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:_m****** *******@manfbra un.de
Phone : +49-621-37 53 86
(Remove the anti-spam-underscore to mail me!)

/*
Name: WMIAsyncQuerySa mpleReconnectSa mple5a.cs
*/

using System;
using System.Manageme nt;

class Sample_Manageme ntEventWatcher
{
[STAThread]
public static int Main(string[] args)
{
if(args.Length != 0)
{
string comp = args[0];

MyHandler mh;
EventArrivedEve ntHandler eventArrivedEve ntHandler;
ManagementEvent Watcher watcher;

mh = new MyHandler();
eventArrivedEve ntHandler = new EventArrivedEve ntHandler(mh.Ar rived); watcher = Sample_Manageme ntEventWatcher. getWatcher(comp );
watcher.EventAr rived += eventArrivedEve ntHandler;

watcher.Start() ; //Access denied!
Console.WriteLi ne("Watcher is running, press <enter> to stop...");
Console.ReadLin e();
watcher.Stop();
watcher.EventAr rived -= eventArrivedEve ntHandler;
}
else
Console.WriteLi ne("Args!!! [P1=Computername]");

return 0;
}

public static ManagementEvent Watcher getWatcher(stri ng comp)
{
ConnectionOptio ns co;
ManagementPath mp;
ManagementScope ms;
WqlEventQuery EventQuery;
ManagementEvent Watcher watcher;
string wql;

co = new ConnectionOptio ns();
co.Timeout = new TimeSpan(0, 0, 60);
co.EnablePrivil eges = true;

mp = new ManagementPath( );
mp.NamespacePat h = @"\root\cimv 2";
mp.Server = comp;
ms = new ManagementScope (mp, co);

wql = "select * from __instancecreat ionevent where targetinstance isa 'Win32_NTLogEve nt'";
EventQuery = new WqlEventQuery(w ql);

watcher = new ManagementEvent Watcher(ms, EventQuery);

return watcher;
}
public class MyHandler
{
public void Arrived(object sender, EventArrivedEve ntArgs e)
{
ManagementBaseO bject mbo =
(ManagementBase Object)e.NewEve nt["TargetInstance "];
Console.WriteLi ne("Event:{0}" , mbo["message"]);
}
}

}//Namespace.



Nov 15 '05 #4
This has all to do with Identity Tracking and STATIC/DYNAMIC cloaking.
If you are writing C++ code, all these settings are exposed and obvious.

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of any included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:eC******** ******@TK2MSFTN GP11.phx.gbl...
Hi All,

interestingly, in one of my latest tests, I found privilege failure audit in the eventlog, claming the denied access to the right SeSecurityPrivi lege
.....

With this in mind, I changed the query in my sample

from:
select * from __instancecreat ionevent where targetinstance isa
'Win32_NTLogEve nt'";

to:
"select * from __instancecreat ionevent where TargetInstance isa
'Win32_NTLogEve nt' and TargetInstance. LogFile = 'Application'";

This is working. But this means, that setting the
ConnectionOptio ns.EnablePrivil eges = true has no effect using the
ManagementEvent Watcher. But in the constructor, I pass a ManagementScope ,
with this option set!

I found no information inside the SDK in the EventWatcherOpt ions or the
WqlEventQuery about security. But even all this does not explain that after a first connect, a second one fails and does not explain what this has to do with setting the [xxxThread] option for my Main() function.

Any help would be really very welcomed!

Thanks so far and
best regards,
Manfred
"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:OK******** *****@tk2msftng p13.phx.gbl...
Hello Willy,

oh, yes and sorry for my inaccuracy about the environment. I am using
Windows 2000 Server, english, SP3, dot.net runtime 1.0, english too. I am
working in an NT4 domain with logged-on account domain admin and the W2K
machine are domain members.

And my chain of experiments always seems to return different results ..... I used two production machines, just to be sure, that not some fumbling ;-)
on my admin machine have caused that. Accidentally, on the next machine, I found a new behavior:Both versions are not running. After finding and
removing some <account deleted> entries from the ACLs with DCOMCNFG, the
behavior return to that, what I originally described and this is on all my W2K machines now really the same. But this last experiment tells me, it
could have something to do with DCOM and/or WMI settings, which are

normally
remains at the OS defaults.

Thanks so far and
best regards,
Manfred

"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:Op******** ******@TK2MSFTN GP12.phx.gbl...
Works for me with both, STA and MTAThread attribute set.

What OS are you runnng?

Willy.
<__*******@be.d e> wrote in message
news:uE******** ******@TK2MSFTN GP09.phx.gbl...
> Hi All,
>
> I have allready tried to ask a similar question [WQLEventQuery:S econd 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 ManagementEvent Watcher 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:_m****** *******@manfbra un.de
> Phone : +49-621-37 53 86
> (Remove the anti-spam-underscore to mail me!)
>
> /*
> Name: WMIAsyncQuerySa mpleReconnectSa mple5a.cs
> */
>
> using System;
> using System.Manageme nt;
>
> class Sample_Manageme ntEventWatcher
> {
> [STAThread]
> public static int Main(string[] args)
> {
> if(args.Length != 0)
> {
> string comp = args[0];
>
> MyHandler mh;
> EventArrivedEve ntHandler eventArrivedEve ntHandler;
> ManagementEvent Watcher watcher;
>
> mh = new MyHandler();
> eventArrivedEve ntHandler = new EventArrivedEve ntHandler(mh.Ar rived); > watcher = Sample_Manageme ntEventWatcher. getWatcher(comp );
> watcher.EventAr rived += eventArrivedEve ntHandler;
>
> watcher.Start() ; //Access denied!
> Console.WriteLi ne("Watcher is running, press <enter> to
stop..."); > Console.ReadLin e();
> watcher.Stop();
> watcher.EventAr rived -= eventArrivedEve ntHandler;
> }
> else
> Console.WriteLi ne("Args!!! [P1=Computername]");
>
> return 0;
> }
>
> public static ManagementEvent Watcher getWatcher(stri ng comp)
> {
> ConnectionOptio ns co;
> ManagementPath mp;
> ManagementScope ms;
> WqlEventQuery EventQuery;
> ManagementEvent Watcher watcher;
> string wql;
>
> co = new ConnectionOptio ns();
> co.Timeout = new TimeSpan(0, 0, 60);
> co.EnablePrivil eges = true;
>
> mp = new ManagementPath( );
> mp.NamespacePat h = @"\root\cimv 2";
> mp.Server = comp;
> ms = new ManagementScope (mp, co);
>
> wql = "select * from __instancecreat ionevent where targetinstance

isa > 'Win32_NTLogEve nt'";
> EventQuery = new WqlEventQuery(w ql);
>
> watcher = new ManagementEvent Watcher(ms, EventQuery);
>
> return watcher;
> }
>
>
> public class MyHandler
> {
> public void Arrived(object sender, EventArrivedEve ntArgs e)
> {
> ManagementBaseO bject mbo =
> (ManagementBase Object)e.NewEve nt["TargetInstance "];
> Console.WriteLi ne("Event:{0}" , mbo["message"]);
> }
> }
>
> }//Namespace.
>
>
>

Nov 15 '05 #5
Manfred,

WMI provider COM objects are "free" threaded, that means they need to run in
a MTA thread.
When your main thread is initialized for STA, the Management classes check
the current apartment for MTA compatibility, and will create a new thread
and enter the MTA to create the COM objects, COM will marshal the interfaces
between the STA thread (running your managed code) and the MTA thread
running the native COM code.
But, your main thread (STA) has enabled the SeSecurityPrivi lege for the main
thread, but this privilege is not propagated to the MTA thread, resulting in
an access denied exception.
Therefore I suggest you allways run your code on an MTA thread, the net
benifits are
- no marshaling overhead
- no difficult to track security issues.
- no issues due to eventsink marshalin between MTA and STA threads.

Willy.

"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:eC******** ******@TK2MSFTN GP11.phx.gbl...
Hi All,

interestingly, in one of my latest tests, I found privilege failure audit in the eventlog, claming the denied access to the right SeSecurityPrivi lege
.....

With this in mind, I changed the query in my sample

from:
select * from __instancecreat ionevent where targetinstance isa
'Win32_NTLogEve nt'";

to:
"select * from __instancecreat ionevent where TargetInstance isa
'Win32_NTLogEve nt' and TargetInstance. LogFile = 'Application'";

This is working. But this means, that setting the
ConnectionOptio ns.EnablePrivil eges = true has no effect using the
ManagementEvent Watcher. But in the constructor, I pass a ManagementScope ,
with this option set!

I found no information inside the SDK in the EventWatcherOpt ions or the
WqlEventQuery about security. But even all this does not explain that after a first connect, a second one fails and does not explain what this has to do with setting the [xxxThread] option for my Main() function.

Any help would be really very welcomed!

Thanks so far and
best regards,
Manfred
"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:OK******** *****@tk2msftng p13.phx.gbl...
Hello Willy,

oh, yes and sorry for my inaccuracy about the environment. I am using
Windows 2000 Server, english, SP3, dot.net runtime 1.0, english too. I am
working in an NT4 domain with logged-on account domain admin and the W2K
machine are domain members.

And my chain of experiments always seems to return different results ..... I used two production machines, just to be sure, that not some fumbling ;-)
on my admin machine have caused that. Accidentally, on the next machine, I found a new behavior:Both versions are not running. After finding and
removing some <account deleted> entries from the ACLs with DCOMCNFG, the
behavior return to that, what I originally described and this is on all my W2K machines now really the same. But this last experiment tells me, it
could have something to do with DCOM and/or WMI settings, which are

normally
remains at the OS defaults.

Thanks so far and
best regards,
Manfred

"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:Op******** ******@TK2MSFTN GP12.phx.gbl...
Works for me with both, STA and MTAThread attribute set.

What OS are you runnng?

Willy.
<__*******@be.d e> wrote in message
news:uE******** ******@TK2MSFTN GP09.phx.gbl...
> Hi All,
>
> I have allready tried to ask a similar question [WQLEventQuery:S econd 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 ManagementEvent Watcher 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:_m****** *******@manfbra un.de
> Phone : +49-621-37 53 86
> (Remove the anti-spam-underscore to mail me!)
>
> /*
> Name: WMIAsyncQuerySa mpleReconnectSa mple5a.cs
> */
>
> using System;
> using System.Manageme nt;
>
> class Sample_Manageme ntEventWatcher
> {
> [STAThread]
> public static int Main(string[] args)
> {
> if(args.Length != 0)
> {
> string comp = args[0];
>
> MyHandler mh;
> EventArrivedEve ntHandler eventArrivedEve ntHandler;
> ManagementEvent Watcher watcher;
>
> mh = new MyHandler();
> eventArrivedEve ntHandler = new EventArrivedEve ntHandler(mh.Ar rived); > watcher = Sample_Manageme ntEventWatcher. getWatcher(comp );
> watcher.EventAr rived += eventArrivedEve ntHandler;
>
> watcher.Start() ; //Access denied!
> Console.WriteLi ne("Watcher is running, press <enter> to
stop..."); > Console.ReadLin e();
> watcher.Stop();
> watcher.EventAr rived -= eventArrivedEve ntHandler;
> }
> else
> Console.WriteLi ne("Args!!! [P1=Computername]");
>
> return 0;
> }
>
> public static ManagementEvent Watcher getWatcher(stri ng comp)
> {
> ConnectionOptio ns co;
> ManagementPath mp;
> ManagementScope ms;
> WqlEventQuery EventQuery;
> ManagementEvent Watcher watcher;
> string wql;
>
> co = new ConnectionOptio ns();
> co.Timeout = new TimeSpan(0, 0, 60);
> co.EnablePrivil eges = true;
>
> mp = new ManagementPath( );
> mp.NamespacePat h = @"\root\cimv 2";
> mp.Server = comp;
> ms = new ManagementScope (mp, co);
>
> wql = "select * from __instancecreat ionevent where targetinstance

isa > 'Win32_NTLogEve nt'";
> EventQuery = new WqlEventQuery(w ql);
>
> watcher = new ManagementEvent Watcher(ms, EventQuery);
>
> return watcher;
> }
>
>
> public class MyHandler
> {
> public void Arrived(object sender, EventArrivedEve ntArgs e)
> {
> ManagementBaseO bject mbo =
> (ManagementBase Object)e.NewEve nt["TargetInstance "];
> Console.WriteLi ne("Event:{0}" , mbo["message"]);
> }
> }
>
> }//Namespace.
>
>
>

Nov 15 '05 #6
Hello Willy,

thanks a lot, I had not understand this completely, and so I tried to use
the [STAThread], because my code, using a [MTAThread] fails to re-connect.
Using ManagementEvent Watcher.Start() , followed by .Stop() and another
..Start() failes also with access denied using the [MTAThread] attribute.

As a test, I opened a direct connection to WMI via the
ManagementScope .Connect(), left this connection open, do a .Start(),
followed by a .Stop() and another .Start() and this works! So I have to
assume, the the ConnectionOptio ns.EnablePrivil eges is not passed to my
second ManagementEvent Watcher.Start() code. This now looks like a bug for
me. Because I really want to connect to a huge number of computers with a
service, I would not use the Scope.Connect() , because I assume, it would
need much more resources.

Do you, or maybe someone else, have a good hint to handle this situation? I
am lerning C# and the framework, but I cannot code this in C++.

Thanks so far and
best regards,
Manfred
"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:eF******** ********@TK2MSF TNGP11.phx.gbl. ..
Manfred,

WMI provider COM objects are "free" threaded, that means they need to run in a MTA thread.
When your main thread is initialized for STA, the Management classes check
the current apartment for MTA compatibility, and will create a new thread
and enter the MTA to create the COM objects, COM will marshal the interfaces between the STA thread (running your managed code) and the MTA thread
running the native COM code.
But, your main thread (STA) has enabled the SeSecurityPrivi lege for the main thread, but this privilege is not propagated to the MTA thread, resulting in an access denied exception.
Therefore I suggest you allways run your code on an MTA thread, the net
benifits are
- no marshaling overhead
- no difficult to track security issues.
- no issues due to eventsink marshalin between MTA and STA threads.

Willy.

"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:eC******** ******@TK2MSFTN GP11.phx.gbl...
Hi All,

interestingly, in one of my latest tests, I found privilege failure audit
in
the eventlog, claming the denied access to the right SeSecurityPrivi lege
.....

With this in mind, I changed the query in my sample

from:
select * from __instancecreat ionevent where targetinstance isa
'Win32_NTLogEve nt'";

to:
"select * from __instancecreat ionevent where TargetInstance isa
'Win32_NTLogEve nt' and TargetInstance. LogFile = 'Application'";

This is working. But this means, that setting the
ConnectionOptio ns.EnablePrivil eges = true has no effect using the
ManagementEvent Watcher. But in the constructor, I pass a ManagementScope , with this option set!

I found no information inside the SDK in the EventWatcherOpt ions or the
WqlEventQuery about security. But even all this does not explain that after
a first connect, a second one fails and does not explain what this has to do
with setting the [xxxThread] option for my Main() function.

Any help would be really very welcomed!

Thanks so far and
best regards,
Manfred
"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:OK******** *****@tk2msftng p13.phx.gbl...
Hello Willy,

oh, yes and sorry for my inaccuracy about the environment. I am using
Windows 2000 Server, english, SP3, dot.net runtime 1.0, english too. I
am working in an NT4 domain with logged-on account domain admin and the W2K machine are domain members.

And my chain of experiments always seems to return different results .... I used two production machines, just to be sure, that not some fumbling ;-)
on my admin machine have caused that. Accidentally, on the next
machine, I found a new behavior:Both versions are not running. After finding and
removing some <account deleted> entries from the ACLs with DCOMCNFG,
the behavior return to that, what I originally described and this is on all
my W2K machines now really the same. But this last experiment tells me,
it could have something to do with DCOM and/or WMI settings, which are

normally
remains at the OS defaults.

Thanks so far and
best regards,
Manfred

"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:Op******** ******@TK2MSFTN GP12.phx.gbl...
> Works for me with both, STA and MTAThread attribute set.
>
> What OS are you runnng?
>
> Willy.
>
>
> <__*******@be.d e> wrote in message
> news:uE******** ******@TK2MSFTN GP09.phx.gbl...
> > Hi All,
> >
> > I have allready tried to ask a similar question [WQLEventQuery:S econd > 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 ManagementEvent Watcher 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:_m****** *******@manfbra un.de
> > Phone : +49-621-37 53 86
> > (Remove the anti-spam-underscore to mail me!)
> >
> > /*
> > Name: WMIAsyncQuerySa mpleReconnectSa mple5a.cs
> > */
> >
> > using System;
> > using System.Manageme nt;
> >
> > class Sample_Manageme ntEventWatcher
> > {
> > [STAThread]
> > public static int Main(string[] args)
> > {
> > if(args.Length != 0)
> > {
> > string comp = args[0];
> >
> > MyHandler mh;
> > EventArrivedEve ntHandler eventArrivedEve ntHandler;
> > ManagementEvent Watcher watcher;
> >
> > mh = new MyHandler();
> > eventArrivedEve ntHandler = new

EventArrivedEve ntHandler(mh.Ar rived);
> > watcher = Sample_Manageme ntEventWatcher. getWatcher(comp );
> > watcher.EventAr rived += eventArrivedEve ntHandler;
> >
> > watcher.Start() ; //Access denied!
> > Console.WriteLi ne("Watcher is running, press <enter> to stop..."); > > Console.ReadLin e();
> > watcher.Stop();
> > watcher.EventAr rived -= eventArrivedEve ntHandler;
> > }
> > else
> > Console.WriteLi ne("Args!!! [P1=Computername]");
> >
> > return 0;
> > }
> >
> > public static ManagementEvent Watcher getWatcher(stri ng comp)
> > {
> > ConnectionOptio ns co;
> > ManagementPath mp;
> > ManagementScope ms;
> > WqlEventQuery EventQuery;
> > ManagementEvent Watcher watcher;
> > string wql;
> >
> > co = new ConnectionOptio ns();
> > co.Timeout = new TimeSpan(0, 0, 60);
> > co.EnablePrivil eges = true;
> >
> > mp = new ManagementPath( );
> > mp.NamespacePat h = @"\root\cimv 2";
> > mp.Server = comp;
> > ms = new ManagementScope (mp, co);
> >
> > wql = "select * from __instancecreat ionevent where

targetinstance isa
> > 'Win32_NTLogEve nt'";
> > EventQuery = new WqlEventQuery(w ql);
> >
> > watcher = new ManagementEvent Watcher(ms, EventQuery);
> >
> > return watcher;
> > }
> >
> >
> > public class MyHandler
> > {
> > public void Arrived(object sender, EventArrivedEve ntArgs e)
> > {
> > ManagementBaseO bject mbo =
> > (ManagementBase Object)e.NewEve nt["TargetInstance "];
> > Console.WriteLi ne("Event:{0}" , mbo["message"]);
> > }
> > }
> >
> > }//Namespace.
> >
> >
> >
>
>



Nov 15 '05 #7
This is a known bug in System.Manageme nt.dll in .NET Framework v1.0. The bug
that prevents WMI privileges from propagating from managed code running on
STAThread to WMI core. Applying [MTAThread] to the calling thread appears to
be a partial workaround to this problem as Access Denied would still be
returned on the second time of event subscription. This problem should be
fixed in .NET Framework v1.1. Have you tried running your application with
v1.1?

--
Andy Cheung
Microsoft WMI Test Engineer
This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:eU******** ******@TK2MSFTN GP10.phx.gbl...
Hello Willy,

thanks a lot, I had not understand this completely, and so I tried to use
the [STAThread], because my code, using a [MTAThread] fails to re-connect.
Using ManagementEvent Watcher.Start() , followed by .Stop() and another
.Start() failes also with access denied using the [MTAThread] attribute.

As a test, I opened a direct connection to WMI via the
ManagementScope .Connect(), left this connection open, do a .Start(),
followed by a .Stop() and another .Start() and this works! So I have to
assume, the the ConnectionOptio ns.EnablePrivil eges is not passed to my
second ManagementEvent Watcher.Start() code. This now looks like a bug for
me. Because I really want to connect to a huge number of computers with a
service, I would not use the Scope.Connect() , because I assume, it would
need much more resources.

Do you, or maybe someone else, have a good hint to handle this situation? I am lerning C# and the framework, but I cannot code this in C++.

Thanks so far and
best regards,
Manfred
"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:eF******** ********@TK2MSF TNGP11.phx.gbl. ..
Manfred,

WMI provider COM objects are "free" threaded, that means they need to run
in
a MTA thread.
When your main thread is initialized for STA, the Management classes check the current apartment for MTA compatibility, and will create a new thread and enter the MTA to create the COM objects, COM will marshal the interfaces
between the STA thread (running your managed code) and the MTA thread
running the native COM code.
But, your main thread (STA) has enabled the SeSecurityPrivi lege for the

main
thread, but this privilege is not propagated to the MTA thread, resulting in
an access denied exception.
Therefore I suggest you allways run your code on an MTA thread, the net
benifits are
- no marshaling overhead
- no difficult to track security issues.
- no issues due to eventsink marshalin between MTA and STA threads.

Willy.

"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:eC******** ******@TK2MSFTN GP11.phx.gbl...
Hi All,

interestingly, in one of my latest tests, I found privilege failure

audit
in
the eventlog, claming the denied access to the right SeSecurityPrivi lege .....

With this in mind, I changed the query in my sample

from:
select * from __instancecreat ionevent where targetinstance isa
'Win32_NTLogEve nt'";

to:
"select * from __instancecreat ionevent where TargetInstance isa
'Win32_NTLogEve nt' and TargetInstance. LogFile = 'Application'";

This is working. But this means, that setting the
ConnectionOptio ns.EnablePrivil eges = true has no effect using the
ManagementEvent Watcher. But in the constructor, I pass a

ManagementScope , with this option set!

I found no information inside the SDK in the EventWatcherOpt ions or the WqlEventQuery about security. But even all this does not explain that

after
a first connect, a second one fails and does not explain what this has to
do
with setting the [xxxThread] option for my Main() function.

Any help would be really very welcomed!

Thanks so far and
best regards,
Manfred
"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:OK******** *****@tk2msftng p13.phx.gbl...
> Hello Willy,
>
> oh, yes and sorry for my inaccuracy about the environment. I am using > Windows 2000 Server, english, SP3, dot.net runtime 1.0, english too. I am
> working in an NT4 domain with logged-on account domain admin and the

W2K > machine are domain members.
>
> And my chain of experiments always seems to return different results

....
> I used two production machines, just to be sure, that not some fumbling ;-)
> on my admin machine have caused that. Accidentally, on the next machine,
I
> found a new behavior:Both versions are not running. After finding

and > removing some <account deleted> entries from the ACLs with DCOMCNFG,

the > behavior return to that, what I originally described and this is on all
my
> W2K machines now really the same. But this last experiment tells me,

it > could have something to do with DCOM and/or WMI settings, which are
normally
> remains at the OS defaults.
>
> Thanks so far and
> best regards,
> Manfred
>
> "Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message > news:Op******** ******@TK2MSFTN GP12.phx.gbl...
> > Works for me with both, STA and MTAThread attribute set.
> >
> > What OS are you runnng?
> >
> > Willy.
> >
> >
> > <__*******@be.d e> wrote in message
> > news:uE******** ******@TK2MSFTN GP09.phx.gbl...
> > > Hi All,
> > >
> > > I have allready tried to ask a similar question

[WQLEventQuery:S econd
> > 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 ManagementEvent Watcher 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:_m****** *******@manfbra un.de
> > > Phone : +49-621-37 53 86
> > > (Remove the anti-spam-underscore to mail me!)
> > >
> > > /*
> > > Name: WMIAsyncQuerySa mpleReconnectSa mple5a.cs
> > > */
> > >
> > > using System;
> > > using System.Manageme nt;
> > >
> > > class Sample_Manageme ntEventWatcher
> > > {
> > > [STAThread]
> > > public static int Main(string[] args)
> > > {
> > > if(args.Length != 0)
> > > {
> > > string comp = args[0];
> > >
> > > MyHandler mh;
> > > EventArrivedEve ntHandler eventArrivedEve ntHandler;
> > > ManagementEvent Watcher watcher;
> > >
> > > mh = new MyHandler();
> > > eventArrivedEve ntHandler = new
EventArrivedEve ntHandler(mh.Ar rived);
> > > watcher = Sample_Manageme ntEventWatcher. getWatcher(comp );
> > > watcher.EventAr rived += eventArrivedEve ntHandler;
> > >
> > > watcher.Start() ; //Access denied!
> > > Console.WriteLi ne("Watcher is running, press <enter> to

stop...");
> > > Console.ReadLin e();
> > > watcher.Stop();
> > > watcher.EventAr rived -= eventArrivedEve ntHandler;
> > > }
> > > else
> > > Console.WriteLi ne("Args!!! [P1=Computername]");
> > >
> > > return 0;
> > > }
> > >
> > > public static ManagementEvent Watcher getWatcher(stri ng comp)
> > > {
> > > ConnectionOptio ns co;
> > > ManagementPath mp;
> > > ManagementScope ms;
> > > WqlEventQuery EventQuery;
> > > ManagementEvent Watcher watcher;
> > > string wql;
> > >
> > > co = new ConnectionOptio ns();
> > > co.Timeout = new TimeSpan(0, 0, 60);
> > > co.EnablePrivil eges = true;
> > >
> > > mp = new ManagementPath( );
> > > mp.NamespacePat h = @"\root\cimv 2";
> > > mp.Server = comp;
> > > ms = new ManagementScope (mp, co);
> > >
> > > wql = "select * from __instancecreat ionevent where targetinstance isa
> > > 'Win32_NTLogEve nt'";
> > > EventQuery = new WqlEventQuery(w ql);
> > >
> > > watcher = new ManagementEvent Watcher(ms, EventQuery);
> > >
> > > return watcher;
> > > }
> > >
> > >
> > > public class MyHandler
> > > {
> > > public void Arrived(object sender, EventArrivedEve ntArgs e)
> > > {
> > > ManagementBaseO bject mbo =
> > > (ManagementBase Object)e.NewEve nt["TargetInstance "];
> > > Console.WriteLi ne("Event:{0}" , mbo["message"]);
> > > }
> > > }
> > >
> > > }//Namespace.
> > >
> > >
> > >
> >
> >
>


Nov 15 '05 #8
Hello Andy,

that's the best news I heard about this !!!!! Ok, not that bugs are all the
time good news, but if there
is a solution .... ;-) ;-)

I have a lot of ASP.Net page using the framework and I was to "shy" to triy
this, but now, I have a good reason. I'll do the update shortly and will see
and answer, if this helps/or other results.

Thanks a lot and
best regards,
Manfred

"Andy Cheung [MSFT]" <ha***@online.m icrosoft.com> wrote in message
news:3f******@n ews.microsoft.c om...
This is a known bug in System.Manageme nt.dll in .NET Framework v1.0. The bug that prevents WMI privileges from propagating from managed code running on
STAThread to WMI core. Applying [MTAThread] to the calling thread appears to be a partial workaround to this problem as Access Denied would still be
returned on the second time of event subscription. This problem should be
fixed in .NET Framework v1.1. Have you tried running your application with
v1.1?

--
Andy Cheung
Microsoft WMI Test Engineer
This posting is provided "As Is" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:eU******** ******@TK2MSFTN GP10.phx.gbl...
Hello Willy,

thanks a lot, I had not understand this completely, and so I tried to use
the [STAThread], because my code, using a [MTAThread] fails to re-connect. Using ManagementEvent Watcher.Start() , followed by .Stop() and another
.Start() failes also with access denied using the [MTAThread] attribute.

As a test, I opened a direct connection to WMI via the
ManagementScope .Connect(), left this connection open, do a .Start(),
followed by a .Stop() and another .Start() and this works! So I have to
assume, the the ConnectionOptio ns.EnablePrivil eges is not passed to my
second ManagementEvent Watcher.Start() code. This now looks like a bug for me. Because I really want to connect to a huge number of computers with a service, I would not use the Scope.Connect() , because I assume, it would
need much more resources.

Do you, or maybe someone else, have a good hint to handle this situation?
I
am lerning C# and the framework, but I cannot code this in C++.

Thanks so far and
best regards,
Manfred
"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:eF******** ********@TK2MSF TNGP11.phx.gbl. ..
Manfred,

WMI provider COM objects are "free" threaded, that means they need to
run
in
a MTA thread.
When your main thread is initialized for STA, the Management classes

check the current apartment for MTA compatibility, and will create a new thread and enter the MTA to create the COM objects, COM will marshal the

interfaces
between the STA thread (running your managed code) and the MTA thread
running the native COM code.
But, your main thread (STA) has enabled the SeSecurityPrivi lege for the main
thread, but this privilege is not propagated to the MTA thread, resulting
in
an access denied exception.
Therefore I suggest you allways run your code on an MTA thread, the
net benifits are
- no marshaling overhead
- no difficult to track security issues.
- no issues due to eventsink marshalin between MTA and STA threads.

Willy.

"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:eC******** ******@TK2MSFTN GP11.phx.gbl...
> Hi All,
>
> interestingly, in one of my latest tests, I found privilege failure

audit
in
> the eventlog, claming the denied access to the right

SeSecurityPrivi lege > .....
>
> With this in mind, I changed the query in my sample
>
> from:
> select * from __instancecreat ionevent where targetinstance isa
> 'Win32_NTLogEve nt'";
>
> to:
> "select * from __instancecreat ionevent where TargetInstance isa
> 'Win32_NTLogEve nt' and TargetInstance. LogFile = 'Application'";
>
> This is working. But this means, that setting the
> ConnectionOptio ns.EnablePrivil eges = true has no effect using the
> ManagementEvent Watcher. But in the constructor, I pass a

ManagementScope ,
> with this option set!
>
> I found no information inside the SDK in the EventWatcherOpt ions or the > WqlEventQuery about security. But even all this does not explain that after
> a first connect, a second one fails and does not explain what this has to
do
> with setting the [xxxThread] option for my Main() function.
>
> Any help would be really very welcomed!
>
> Thanks so far and
> best regards,
> Manfred
>
>
> "Manfred Braun" <_m************ *@manfbraun.de> wrote in message
> news:OK******** *****@tk2msftng p13.phx.gbl...
> > Hello Willy,
> >
> > oh, yes and sorry for my inaccuracy about the environment. I am using > > Windows 2000 Server, english, SP3, dot.net runtime 1.0, english
too. I am
> > working in an NT4 domain with logged-on account domain admin and
the
W2K
> > machine are domain members.
> >
> > And my chain of experiments always seems to return different
results ....
> > I used two production machines, just to be sure, that not some

fumbling
> ;-)
> > on my admin machine have caused that. Accidentally, on the next

machine,
I
> > found a new behavior:Both versions are not running. After finding

and > > removing some <account deleted> entries from the ACLs with DCOMCNFG, the
> > behavior return to that, what I originally described and this is
on
all
my
> > W2K machines now really the same. But this last experiment tells
me, it
> > could have something to do with DCOM and/or WMI settings, which

are > normally
> > remains at the OS defaults.
> >
> > Thanks so far and
> > best regards,
> > Manfred
> >
> > "Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in

message > > news:Op******** ******@TK2MSFTN GP12.phx.gbl...
> > > Works for me with both, STA and MTAThread attribute set.
> > >
> > > What OS are you runnng?
> > >
> > > Willy.
> > >
> > >
> > > <__*******@be.d e> wrote in message
> > > news:uE******** ******@TK2MSFTN GP09.phx.gbl...
> > > > Hi All,
> > > >
> > > > I have allready tried to ask a similar question
[WQLEventQuery:S econd
> > > 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 ManagementEvent Watcher 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:_m****** *******@manfbra un.de
> > > > Phone : +49-621-37 53 86
> > > > (Remove the anti-spam-underscore to mail me!)
> > > >
> > > > /*
> > > > Name: WMIAsyncQuerySa mpleReconnectSa mple5a.cs
> > > > */
> > > >
> > > > using System;
> > > > using System.Manageme nt;
> > > >
> > > > class Sample_Manageme ntEventWatcher
> > > > {
> > > > [STAThread]
> > > > public static int Main(string[] args)
> > > > {
> > > > if(args.Length != 0)
> > > > {
> > > > string comp = args[0];
> > > >
> > > > MyHandler mh;
> > > > EventArrivedEve ntHandler eventArrivedEve ntHandler;
> > > > ManagementEvent Watcher watcher;
> > > >
> > > > mh = new MyHandler();
> > > > eventArrivedEve ntHandler = new
> EventArrivedEve ntHandler(mh.Ar rived);
> > > > watcher = Sample_Manageme ntEventWatcher. getWatcher(comp );
> > > > watcher.EventAr rived += eventArrivedEve ntHandler;
> > > >
> > > > watcher.Start() ; //Access denied!
> > > > Console.WriteLi ne("Watcher is running, press <enter> to
stop...");
> > > > Console.ReadLin e();
> > > > watcher.Stop();
> > > > watcher.EventAr rived -= eventArrivedEve ntHandler;
> > > > }
> > > > else
> > > > Console.WriteLi ne("Args!!! [P1=Computername]");
> > > >
> > > > return 0;
> > > > }
> > > >
> > > > public static ManagementEvent Watcher getWatcher(stri ng comp)
> > > > {
> > > > ConnectionOptio ns co;
> > > > ManagementPath mp;
> > > > ManagementScope ms;
> > > > WqlEventQuery EventQuery;
> > > > ManagementEvent Watcher watcher;
> > > > string wql;
> > > >
> > > > co = new ConnectionOptio ns();
> > > > co.Timeout = new TimeSpan(0, 0, 60);
> > > > co.EnablePrivil eges = true;
> > > >
> > > > mp = new ManagementPath( );
> > > > mp.NamespacePat h = @"\root\cimv 2";
> > > > mp.Server = comp;
> > > > ms = new ManagementScope (mp, co);
> > > >
> > > > wql = "select * from __instancecreat ionevent where

targetinstance
> isa
> > > > 'Win32_NTLogEve nt'";
> > > > EventQuery = new WqlEventQuery(w ql);
> > > >
> > > > watcher = new ManagementEvent Watcher(ms, EventQuery);
> > > >
> > > > return watcher;
> > > > }
> > > >
> > > >
> > > > public class MyHandler
> > > > {
> > > > public void Arrived(object sender, EventArrivedEve ntArgs e)
> > > > {
> > > > ManagementBaseO bject mbo =
> > > > (ManagementBase Object)e.NewEve nt["TargetInstance "];
> > > > Console.WriteLi ne("Event:{0}" , mbo["message"]);
> > > > }
> > > > }
> > > >
> > > > }//Namespace.
> > > >
> > > >
> > > >
> > >
> > >
> >
>



Nov 15 '05 #9
Hello Andy and All,

a lot of thanks for the help again! After upgrading to runtime V1.1 the same
code is excact doing what I expect!!!

A lot of thanks and
best regards,
Manfred

"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hello Andy,

that's the best news I heard about this !!!!! Ok, not that bugs are all the time good news, but if there
is a solution .... ;-) ;-)

I have a lot of ASP.Net page using the framework and I was to "shy" to triy this, but now, I have a good reason. I'll do the update shortly and will see and answer, if this helps/or other results.

Thanks a lot and
best regards,
Manfred

"Andy Cheung [MSFT]" <ha***@online.m icrosoft.com> wrote in message
news:3f******@n ews.microsoft.c om...
This is a known bug in System.Manageme nt.dll in .NET Framework v1.0. The bug
that prevents WMI privileges from propagating from managed code running on
STAThread to WMI core. Applying [MTAThread] to the calling thread appears to
be a partial workaround to this problem as Access Denied would still be
returned on the second time of event subscription. This problem should
be fixed in .NET Framework v1.1. Have you tried running your application with v1.1?

--
Andy Cheung
Microsoft WMI Test Engineer
This posting is provided "As Is" with no warranties, and confers no

rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Manfred Braun" <_m************ *@manfbraun.de> wrote in message
news:eU******** ******@TK2MSFTN GP10.phx.gbl...
Hello Willy,

thanks a lot, I had not understand this completely, and so I tried to use the [STAThread], because my code, using a [MTAThread] fails to re-connect. Using ManagementEvent Watcher.Start() , followed by .Stop() and another
.Start() failes also with access denied using the [MTAThread] attribute.
As a test, I opened a direct connection to WMI via the
ManagementScope .Connect(), left this connection open, do a .Start(),
followed by a .Stop() and another .Start() and this works! So I have to assume, the the ConnectionOptio ns.EnablePrivil eges is not passed to my
second ManagementEvent Watcher.Start() code. This now looks like a bug for me. Because I really want to connect to a huge number of computers with a
service, I would not use the Scope.Connect() , because I assume, it
would need much more resources.

Do you, or maybe someone else, have a good hint to handle this

situation?
I
am lerning C# and the framework, but I cannot code this in C++.

Thanks so far and
best regards,
Manfred
"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:eF******** ********@TK2MSF TNGP11.phx.gbl. ..
> Manfred,
>
> WMI provider COM objects are "free" threaded, that means they need to run
in
> a MTA thread.
> When your main thread is initialized for STA, the Management classes

check
> the current apartment for MTA compatibility, and will create a new

thread
> and enter the MTA to create the COM objects, COM will marshal the
interfaces
> between the STA thread (running your managed code) and the MTA
thread > running the native COM code.
> But, your main thread (STA) has enabled the SeSecurityPrivi lege for

the main
> thread, but this privilege is not propagated to the MTA thread,

resulting
in
> an access denied exception.
> Therefore I suggest you allways run your code on an MTA thread, the net > benifits are
> - no marshaling overhead
> - no difficult to track security issues.
> - no issues due to eventsink marshalin between MTA and STA threads.
>
> Willy.
>
> "Manfred Braun" <_m************ *@manfbraun.de> wrote in message
> news:eC******** ******@TK2MSFTN GP11.phx.gbl...
> > Hi All,
> >
> > interestingly, in one of my latest tests, I found privilege failure audit
> in
> > the eventlog, claming the denied access to the right

SeSecurityPrivi lege
> > .....
> >
> > With this in mind, I changed the query in my sample
> >
> > from:
> > select * from __instancecreat ionevent where targetinstance isa
> > 'Win32_NTLogEve nt'";
> >
> > to:
> > "select * from __instancecreat ionevent where TargetInstance isa
> > 'Win32_NTLogEve nt' and TargetInstance. LogFile = 'Application'";
> >
> > This is working. But this means, that setting the
> > ConnectionOptio ns.EnablePrivil eges = true has no effect using the
> > ManagementEvent Watcher. But in the constructor, I pass a
ManagementScope ,
> > with this option set!
> >
> > I found no information inside the SDK in the EventWatcherOpt ions or the
> > WqlEventQuery about security. But even all this does not explain that > after
> > a first connect, a second one fails and does not explain what this has to
> do
> > with setting the [xxxThread] option for my Main() function.
> >
> > Any help would be really very welcomed!
> >
> > Thanks so far and
> > best regards,
> > Manfred
> >
> >
> > "Manfred Braun" <_m************ *@manfbraun.de> wrote in message
> > news:OK******** *****@tk2msftng p13.phx.gbl...
> > > Hello Willy,
> > >
> > > oh, yes and sorry for my inaccuracy about the environment. I am

using
> > > Windows 2000 Server, english, SP3, dot.net runtime 1.0, english too.
I
> am
> > > working in an NT4 domain with logged-on account domain admin and the W2K
> > > machine are domain members.
> > >
> > > And my chain of experiments always seems to return different results > ....
> > > I used two production machines, just to be sure, that not some
fumbling
> > ;-)
> > > on my admin machine have caused that. Accidentally, on the next
machine,
> I
> > > found a new behavior:Both versions are not running. After
finding and
> > > removing some <account deleted> entries from the ACLs with DCOMCNFG, the
> > > behavior return to that, what I originally described and this is on all
> my
> > > W2K machines now really the same. But this last experiment tells me, it
> > > could have something to do with DCOM and/or WMI settings, which are > > normally
> > > remains at the OS defaults.
> > >
> > > Thanks so far and
> > > best regards,
> > > Manfred
> > >
> > > "Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in

message
> > > news:Op******** ******@TK2MSFTN GP12.phx.gbl...
> > > > Works for me with both, STA and MTAThread attribute set.
> > > >
> > > > What OS are you runnng?
> > > >
> > > > Willy.
> > > >
> > > >
> > > > <__*******@be.d e> wrote in message
> > > > news:uE******** ******@TK2MSFTN GP09.phx.gbl...
> > > > > Hi All,
> > > > >
> > > > > I have allready tried to ask a similar question
> [WQLEventQuery:S econd
> > > > 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 ManagementEvent Watcher 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:_m****** *******@manfbra un.de
> > > > > Phone : +49-621-37 53 86
> > > > > (Remove the anti-spam-underscore to mail me!)
> > > > >
> > > > > /*
> > > > > Name: WMIAsyncQuerySa mpleReconnectSa mple5a.cs
> > > > > */
> > > > >
> > > > > using System;
> > > > > using System.Manageme nt;
> > > > >
> > > > > class Sample_Manageme ntEventWatcher
> > > > > {
> > > > > [STAThread]
> > > > > public static int Main(string[] args)
> > > > > {
> > > > > if(args.Length != 0)
> > > > > {
> > > > > string comp = args[0];
> > > > >
> > > > > MyHandler mh;
> > > > > EventArrivedEve ntHandler eventArrivedEve ntHandler;
> > > > > ManagementEvent Watcher watcher;
> > > > >
> > > > > mh = new MyHandler();
> > > > > eventArrivedEve ntHandler = new
> > EventArrivedEve ntHandler(mh.Ar rived);
> > > > > watcher =

Sample_Manageme ntEventWatcher. getWatcher(comp ); > > > > > watcher.EventAr rived += eventArrivedEve ntHandler;
> > > > >
> > > > > watcher.Start() ; //Access denied!
> > > > > Console.WriteLi ne("Watcher is running, press <enter> to
> stop...");
> > > > > Console.ReadLin e();
> > > > > watcher.Stop();
> > > > > watcher.EventAr rived -= eventArrivedEve ntHandler;
> > > > > }
> > > > > else
> > > > > Console.WriteLi ne("Args!!! [P1=Computername]");
> > > > >
> > > > > return 0;
> > > > > }
> > > > >
> > > > > public static ManagementEvent Watcher getWatcher(stri ng comp) > > > > > {
> > > > > ConnectionOptio ns co;
> > > > > ManagementPath mp;
> > > > > ManagementScope ms;
> > > > > WqlEventQuery EventQuery;
> > > > > ManagementEvent Watcher watcher;
> > > > > string wql;
> > > > >
> > > > > co = new ConnectionOptio ns();
> > > > > co.Timeout = new TimeSpan(0, 0, 60);
> > > > > co.EnablePrivil eges = true;
> > > > >
> > > > > mp = new ManagementPath( );
> > > > > mp.NamespacePat h = @"\root\cimv 2";
> > > > > mp.Server = comp;
> > > > > ms = new ManagementScope (mp, co);
> > > > >
> > > > > wql = "select * from __instancecreat ionevent where
targetinstance
> > isa
> > > > > 'Win32_NTLogEve nt'";
> > > > > EventQuery = new WqlEventQuery(w ql);
> > > > >
> > > > > watcher = new ManagementEvent Watcher(ms, EventQuery);
> > > > >
> > > > > return watcher;
> > > > > }
> > > > >
> > > > >
> > > > > public class MyHandler
> > > > > {
> > > > > public void Arrived(object sender, EventArrivedEve ntArgs e) > > > > > {
> > > > > ManagementBaseO bject mbo =
> > > > > (ManagementBase Object)e.NewEve nt["TargetInstance "];
> > > > > Console.WriteLi ne("Event:{0}" , mbo["message"]);
> > > > > }
> > > > > }
> > > > >
> > > > > }//Namespace.
> > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> >
>
>



Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
13714
by: Jim Andersen | last post by:
Hi, I made a DTS-package and it works when I execute it manually, but when it is run by the SQL Server Agent, it fails. I have use the guide to create a maintenance plan. That doesn't work so good either. It runs Optimizations, but not integrity checks or backups (probably because integrity checks failed). The following from the log file:
1
1350
by: Gary | last post by:
Hi, I am working with drag and drop functionality. I am draging a button and dropping in a text box. I am able to drop only button text value. 1) Can i drop button itself? 2) If i work with a single form in a project having it work fine but in my project my start page is login - and drag and drop does not have so it displays an error "dragdrop registration failed" and i cant include my in my drag and drop page too..
3
4696
by: Deepal | last post by:
Hi; I'm doing an example program from a book regarding .NET Remoting. Both the Client and Server are C# Console Applications. Server Code is using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp;
11
1604
by: dhnriverside | last post by:
Hi peeps Ok, so I thought I'd have a go at making a console app in VS2k5... I haven't written any windows apps for years, let alone dos apps (been web programming) and I've hit a dumb error... "An object reference is required for the nonstatic field, method or property " This is occuring in my main function...
1
9474
by: Rachel | last post by:
I have created a simple web service in VC# that adds two numbers. It returns an integer value to the client once invoked. I used Visual Studio .NET 2003, Windows XP Pro and .NET 1.1. I created a Windows form client. While debugging I get the following error : Unhandled Exception: System.Net.WebException: The request failed with HTTP status 401: Access Denied. in the following line:
10
2544
by: Bryce Calhoun | last post by:
Hello, First of all, this is a .NET 1.1 component I'm creating. SUMMARY ----------------------- This component that I'm creating is, for all intents and purposes, a document parser (I'm actually deserializing a SOAP document into an object and parsing out the fields, but that's fairly immaterial to this
2
1805
by: Prophet | last post by:
Can someone help me troubleshoot this IISSTATE log ? I have tried to make sense out of it but with no luck I am currently running IIS5 on a Win 2k box. Hosting 3 web sites and runnning asp pages. One of the asp pages (confirmations.asp) is making the dllhost.exe process hog all the cpu for a while and the underlying application (the one that it communicates with) crashes. here is the log file, any comments/help would be greatly...
0
3470
by: p.thorn.ru | last post by:
Hello, I am running db2 express-c 9.5 under linux (fedora 7), and my error log frequently shows messages like these: 2008-01-08-18.30.01.952964+180 I651334G1048 LEVEL: Error (OS) PID : 2639 TID : 45063056 PROC : db2fmp ( INSTANCE: db2inst1 NODE : 000 FUNCTION: DB2 Common, OSSe, ossGetDiskInfo, probe:140 MESSAGE : ECF=0x90000017=-1879048169=ECF_FILE_PATH_ACCESS_DENIED
0
3404
by: private.anders | last post by:
Hi David! Really need assistance since I have been struggling with a problem long time now. I am running a web application on a Win 2003 Std (Active Directory). Everything works fine. I have installed same application on another server Win 2003 R2 Std (No Active Directory). On that server I get “Permission denied” using the following code (used to zip folder content).
0
8787
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9289
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9158
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9060
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4712
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3151
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2096
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.