请帮助[STAThread],WMI和访问被拒绝 [英] Please help with [STAThread], WMI and access denied

查看:93
本文介绍了请帮助[STAThread],WMI和访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我已经尝试过问一个类似的问题[WQLEventQuery:第二个本地

连接:拒绝访问!],但是没有回答到现在为止。在此期间,我发现
,我无法理解Main()函数的一些线程设置

[我正在使用C#]。

如果我使用Main()函数的[STAThread]属性,我得到访问

拒绝错误,如果我使用ManagementEventWatcher连接到本地

机器接收事件。有没有人在那里,怎么可能

解释为什么会这样?如果我删除这个属性或使用[MTAThread]

而不是它工作一次。

我在最后添加一个简短的样本。


一些帮助会非常好。


到目前为止感谢

最好的问候

祝你好运,

Manfred Braun


(私人)

Lange Roetterstrasse 7

D68167曼海姆

德国


mailto:_m ************* @ manfbraun.de

电话:+ 49-621- 37 53 86

(删除反垃圾邮件下划线给我发邮件!)


/ *

名称:WMIAsyncQuerySampleReconnectSample5a。 cs

* /


使用系统;

使用System.Management;


class Sample_ManagementEventWatcher

{

[STAThread]

public static int Main(string [] args)

{

if(args.Length!= 0)

{

string comp = args [0];


MyHandler mh;

EventArrivedEv entHandler eventArrivedEventHandler;

ManagementEventWatcher观察者;


mh = new MyHandler();

eventArrivedEventHandler = new EventArrivedEventHandler(mh.Arrived) ;

watcher = Sample_ManagementEventWatcher.getWatcher(comp);

watcher.EventArrived + = eventArrivedEventHandler;


watcher.Start() ; //拒绝访问!

Console.WriteLine(Watcher正在运行,请按< enter>停止......);

Console.ReadLine() ;

watcher.Stop();

watcher.EventArrived - = eventArrivedEventHandler;

}

else

Console.WriteLine(" Args !!! [P1 = Computername]");


返回0;

}


public static ManagementEventWatcher getWatcher(string comp)

{

ConnectionOptions co;

ManagementPath \\ tmp;

ManagementScope ms;

WqlEventQuery EventQuery;

ManagementEventWatcher观察者;

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 =新的ManagementScope(mp,co);


wql =" select * from __instancecreationevent where targetinstance isa

''Win32_NTLogEvent''" ;;

EventQuery = new WqlEventQuery(wql);


watcher = new ManagementEventWatcher(ms,EventQuery);


返回观察者;

}

公共类MyHandler

{

public void Arrived(object sender,EventArrivedEventArgs e)

{

ManagementBaseObject mbo =

(ManagementBaseObject)e.NewEvent [" TargetInstance"];

Console.WriteLine(" Event:{0}",mbo [" message"]);

}

}


} //命名空间。

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:_m*************@manfbraun.de
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.

推荐答案

适用于我,同时设置STA和MTAThread属性。


你运行的操作系统是什么?


威利。

< __ ******* @ be.de>在消息中写道

news:uE ************** @ TK2MSFTNGP09.phx.gbl ...
Works for me with both, STA and MTAThread attribute set.

What OS are you runnng?

Willy.
<__*******@be.de> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl...
大家好,

我已经尝试过问一个类似的问题[WQLEventQuery:第二个
本地连接:拒绝访问!],但直到现在才得到答案。在此期间,我发现,我无法理解Main()
函数的一些线程设置[我正在使用C#]。
如果我使用[STAThread]属性Main()函数,我得到访问
拒绝错误,如果我使用ManagementEventWatcher连接到本地
机器来接收事件。那里有人,怎么可能解释为什么会这样?如果我删除这个属性或使用[MTAThread]
而不是它可以工作一次。
我在最后添加一个简短的样本。

一些帮助真的非常好。

到目前为止,感谢最好的问候
致以诚挚的问候,
Manfred Braun

(私人)
Lange Roetterstrasse 7
D68167曼海姆
德国

mailto:_m ************* @ manfbraun.de
电话:+ 49-621-37 53 86
(删除反垃圾邮件下划线给我发邮件!)名称:WMIAsyncQuerySampleReconnectSample5a.cs
* /
使用System;
使用System.Management;

类Sample_ManagementEventWatcher
{
[STAThread]
public static int Main(string [] args)
{
if(args.Length!= 0)
{
字符串comp = args [0];

MyHandler mh;
EventArrivedEventHandler eventArrivedEventHandler;
ManagementEventWatcher观察者;

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

watcher.Start(); //拒绝访问!
Console.WriteLine(Watcher正在运行,请按< enter>停止......);
Console.ReadLine();
watcher.Stop ();
watcher.EventArrived - = eventArrivedEventHandler;
}

Console.WriteLine(" Args !!! [P1 = Computername]");

返回0;
}
公共静态ManagementEventWatcher getWatcher(字符串comp)
{
ConnectionOptions co;
ManagementPath mp;
WqlEventQuery EventQuery;
ManagementEventWatcher观察者;
字符串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其中targetinstance isa
'Win32_NTLogEvent''" ;;
EventQuery = new WqlEventQuery(wql);

watcher = new ManagementEventWatcher(ms,EventQuery);

返回观察者;
}

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

} //命名空间。
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:_m*************@manfbraun.de
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.



Hello Willy,


哦,是的,对不起我对环境的不准确。我正在使用

Windows 2000 Server,英文,SP3,dot.net运行时1.0,英文也。我是

在NT4域工作,登录帐户域管理员和W2K

机器是域成员。


我的实验链似乎总是会返回不同的结果....

我使用了两台生产机器,只是为了确保,不是一些摸索;-)

on我的管理员机器造成的。无意中,在下一台机器上,我发现了一个新的行为:两个版本都没有运行。找到并

删除一些<帐户已删除>使用DCOMCNFG的ACL中的条目,

行为返回到那个,我最初描述的内容和我现在所有的
W2K机器现在真的一样。但是这最后一个实验告诉我,它可能与DCOM和/或WMI设置有关,这通常是在操作系统默认值下保持



到目前为止,感谢你好。

最好的问候,

Manfred


" Willy Denoyette [MVP ]" <无线************* @ pandora.be>在消息中写道

新闻:Op ************** @ TK2MSFTNGP12.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**************@TK2MSFTNGP12.phx.gbl...
适用于我,STA和MTAThread属性集。

你运行什么操作系统?

Willy。

< __ ******* @ be.de>在消息中写道
新闻:uE ************** @ TK2MSFTNGP09.phx.gbl ...
Works for me with both, STA and MTAThread attribute set.

What OS are you runnng?

Willy.
<__*******@be.de> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl...
大家好,
Hi All,

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


local


local

connect:Access denied!],但直到现在才得到答案。与此同时,我发现,我无法理解Main()
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

[我正在使用C#]。如果我使用Main()函数的[STAThread]属性,我得到
访问被拒绝错误,如果我使用ManagementEventWatcher连接到本地
机器来接收事件。那里有人,怎么可能解释为什么会这样?如果我删除这个属性或使用[MTAThread]
而不是它可以工作一次。
我在最后添加一个简短的样本。

一些帮助真的非常好。

到目前为止,感谢最好的问候
致以诚挚的问候,
Manfred Braun

(私人)
Lange Roetterstrasse 7
D68167曼海姆
德国

mailto:_m ************* @ manfbraun.de
电话:+ 49-621-37 53 86
(删除反垃圾邮件下划线给我发邮件!)名称:WMIAsyncQuerySampleReconnectSample5a.cs
* /
使用System;
使用System.Management;

类Sample_ManagementEventWatcher
{
[STAThread]
public static int Main(string [] args)
{
if(args.Length!= 0)
{
字符串comp = args [0];

MyHandler mh;
EventArrivedEventHandler eventArrivedEventHandler;
ManagementEventWatcher观察者;

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

watcher.Start(); //拒绝访问!
Console.WriteLine(Watcher正在运行,请按< enter>停止......);
Console.ReadLine();
watcher.Stop ();
watcher.EventArrived - = eventArrivedEventHandler;
}

Console.WriteLine(" Args !!! [P1 = Computername]");

返回0;
}
公共静态ManagementEventWatcher getWatcher(字符串comp)
{
ConnectionOptions co;
ManagementPath mp;
WqlEventQuery EventQuery;
ManagementEventWatcher观察者;
字符串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其中targetinstance isa
'Win32_NTLogEvent''" ;;
EventQuery = new WqlEventQuery(wql);

watcher = new ManagementEventWatcher(ms,EventQuery);

返回观察者;
}

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

} //命名空间。
[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:_m*************@manfbraun.de
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.







大家好


有趣的是,在我最近的一次测试中,我在事件日志中发现了特权失败审计,并且拒绝了对权利SeSecurityPrivilege的拒绝访问

......


考虑到这一点,我改变了我的样本中的查询


来自:

从__instancecreationevent中选择*其中targetinstance isa

''Win32_NTLogEvent''" ;;


to:

" select * from __instancecreationevent其中TargetInstance isa

''Win32_NTLogEvent ''和TargetInstance.LogFile =''申请''" ;;


这是有效的。但这意味着,使用

ManagementEventWatcher设置

ConnectionOptions.EnablePrivileges = true无效。但是在构造函数中,我传递了一个ManagementScope,

,并设置了这个选项!


我在EventWatcherOptions中的SDK中找不到任何信息或

关于安全性的WqlEventQuery。但即使这一切也没有解释,在第一次连接后,第二次失败并且没有解释这个必须做什么

为我设置[xxxThread]选项Main()函数。


任何帮助都会非常受欢迎!


到目前为止感谢

最好问候,

Manfred

Manfred Braun < _M ************* @ manfbraun.de>在消息中写道

新闻:好的************* @ tk2msftngp13.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 SeSecurityPrivilege
......

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

from:
select * from __instancecreationevent where targetinstance isa
''Win32_NTLogEvent''";

to:
"select * from __instancecreationevent where TargetInstance isa
''Win32_NTLogEvent'' and TargetInstance.LogFile = ''Application''";

This is working. But this means, that setting the
ConnectionOptions.EnablePrivileges = true has no effect using the
ManagementEventWatcher. But in the constructor, I pass a ManagementScope,
with this option set!

I found no information inside the SDK in the EventWatcherOptions 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*************@tk2msftngp13.phx.gbl...
Hello Willy,
哦,是的,对不起我对环境的不准确。我正在使用Windows 2000 Server,英语,SP3,dot.net运行时1.0,英语。我在NT4域工作,登录帐户域管理员,W2K
机器是域成员。

我的实验链似乎总是会返回不同的结果。 ...
我使用了两台生产机器,只是为了确保,在我的管理员机器上没有一些笨拙的
;-)导致了这一点。无意中,在下一台机器上,我发现了一个新的行为:两个版本都没有运行。找到并删除一些<帐户已删除>使用DCOMCNFG的ACL中的条目,
行为返回到那个,我最初描述的内容和现在我所有的W2K机器上都是一样的。但是最后一个实验告诉我,它可能与DCOM和/或WMI设置有关,这些设置通常保留在操作系统默认值为


感谢到目前为止
最好的问候,
曼弗雷德

Willy Denoyette [MVP]" <无线************* @ pandora.be>在消息中写道
新闻:Op ************** @ TK2MSFTNGP12.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**************@TK2MSFTNGP12.phx.gbl...
适用于我,包括STA和MTAThread属性集。

你运行的是什么操作系统?

Willy。

< __ ******* @ be.de>在消息中写道
新闻:uE ************** @ TK2MSFTNGP09.phx.gbl ...
Works for me with both, STA and MTAThread attribute set.

What OS are you runnng?

Willy.
<__*******@be.de> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl...
大家好,
Hi All,

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


local


local

connect:Access denied!],但直到现在才得到答案。与此同时,
我发现,我无法理解Main()
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

[我正在使用C#]。如果我使用Main()函数的[STAThread]属性,如果我使用ManagementEventWatcher连接到
本地机器来接收事件,我会得到访问被拒绝错误。那里有人,
怎么可能解释为什么会这样?如果我删除这个属性或者使用
[MTAThread]而不是它可以工作一次。
我在最后添加一个简短的样本。

一些帮助真的非常好。

到目前为止,感谢最好的问候
致以诚挚的问候,
Manfred Braun

(私人)
Lange Roetterstrasse 7
D68167曼海姆
德国

mailto:_m ************* @ manfbraun.de
电话:+ 49-621-37 53 86
(删除反垃圾邮件下划线给我发邮件!)名称:WMIAsyncQuerySampleReconnectSample5a.cs
* /
使用System;
使用System.Management;

类Sample_ManagementEventWatcher
{
[STAThread]
public static int Main(string [] args)
{
if(args.Length!= 0)
{
字符串comp = args [0];

MyHandler mh;
EventArrivedEventHandler eventArrivedEventHandler;
ManagementEventWatcher观察者;

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

watcher.Start(); //拒绝访问!
Console.WriteLine(Watcher正在运行,请按< enter>停止......);
Console.ReadLine();
watcher.Stop ();
watcher.EventArrived - = eventArrivedEventHandler;
}

Console.WriteLine(" Args !!! [P1 = Computername]");

返回0;
}
公共静态ManagementEventWatcher getWatcher(字符串comp)
{
ConnectionOptions co;
ManagementPath mp;
WqlEventQuery EventQuery;
ManagementEventWatcher观察者;
字符串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其中targetinstance
isa'Win32_NTLogEvent''" ;;
EventQuery = new WqlEventQuery(wql);

watcher = new ManagementEventWatcher(ms,EventQuery);

返回观察者;
}

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

} //命名空间。
[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:_m*************@manfbraun.de
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.







这篇关于请帮助[STAThread],WMI和访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆