C# 中的 WMI 查询不适用于非英语机器

发布于 2024-11-06 09:35:57 字数 1364 浏览 4 评论 0原文

我正在创建一个应用程序,需要跟踪进程何时启动,然后在进程完成时引发一个事件。

我的代码运行完美,并且完全符合我在英语机器上的需要,但是当我在法语机器上运行相同的应用程序时,它失败了。

失败的错误命中代码

qstart = new WqlEventQuery("__InstanceCreationEvent",
            new TimeSpan(0, 0, 0, 0, 5),
            "TargetInstance isa \"Win32_Process\"");

qstop = new WqlEventQuery("__InstanceDeletionEvent",
            new TimeSpan(0, 0, 0, 0, 5),
            "TargetInstance isa \"Win32_Process\"");
        try
        {
            using (wstart = new ManagementEventWatcher(qstart))
            {
                wstart.EventArrived += new EventArrivedEventHandler(ProcessStarted);
                Log.DebugEntry("BeginProcess() - Starting wstart Event");
                wstart.Start();
            }
        }
        catch (Exception ex)
        {
            Log.DebugEntry("error on wstart: " + ex.Message);
        }

        using (wstop = new ManagementEventWatcher(qstop))
        {
            wstop.EventArrived += new EventArrivedEventHandler(ProcessStopped);
            Log.DebugEntry("BeginProcess() - Starting wstop Event");
            wstop.Start();
        }

这是尝试启动查询时 : wstart.Start();

并做同样的事情 wstop.Start();

我只能猜测这与语言和查询字符串有关,但我正在抓住救命稻草。

它出现的错误是: “要求不可分析”

非常感谢任何帮助!

Martyn

编辑:在 2 台相同的机器上进行了测试,唯一的区别是首次启动时选择的语言。

I am creating an application that needs to track when a process starts, then raise an event when it's finished.

I have code that works perfectly, and does exactly what I need on an English machine, but when I run the same application on a French language machine it fails.

here's the code that fails

qstart = new WqlEventQuery("__InstanceCreationEvent",
            new TimeSpan(0, 0, 0, 0, 5),
            "TargetInstance isa \"Win32_Process\"");

qstop = new WqlEventQuery("__InstanceDeletionEvent",
            new TimeSpan(0, 0, 0, 0, 5),
            "TargetInstance isa \"Win32_Process\"");
        try
        {
            using (wstart = new ManagementEventWatcher(qstart))
            {
                wstart.EventArrived += new EventArrivedEventHandler(ProcessStarted);
                Log.DebugEntry("BeginProcess() - Starting wstart Event");
                wstart.Start();
            }
        }
        catch (Exception ex)
        {
            Log.DebugEntry("error on wstart: " + ex.Message);
        }

        using (wstop = new ManagementEventWatcher(qstop))
        {
            wstop.EventArrived += new EventArrivedEventHandler(ProcessStopped);
            Log.DebugEntry("BeginProcess() - Starting wstop Event");
            wstop.Start();
        }

the error hits when it tries to start the query:
wstart.Start();

and does the same for
wstop.Start();

I can only guess it has something to do with the language and the query string, but I'm clutching at straws.

The error it comes up with is:
"demande non analysable"

Any help is gratefully recieved!

Martyn

Edit: Tested on 2 identical Machines, only difference being the language chosen on first startup.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿得七秒忆 2024-11-13 09:35:57

显然是因为您指定的间隔太小...我刚刚在法语 Windows XP SP3 上尝试过,并得到了相同的错误。但是,如果我将间隔更改为 1 秒,则效果很好...似乎您无法指定小于 1 秒的间隔。不过,不知道为什么这只发生在非英语操作系统上...

编辑:实际上我刚刚意识到这可能是 WqlEventQuery 中的一个错误。 qstart.QueryString 看起来像

select * from __InstanceCreationEvent within 0.005 where TargetInstance isa "Win32_Process"

CurrentCulture = "en-US" :但是 CurrentCulture = "fr-FR" 看起来像这样:(

select * from __InstanceCreationEvent within 0,005 where TargetInstance isa "Win32_Process"

注意数字格式的差异)

所以显然是代码WqlEventQuery 中的 不会强制使用不变区域性来格式化数字,从而导致在小数点分隔符不是“.”的区域性中查询不正确。

如果将 CurrentCulture 强制为 CultureInfo.Invariant,即使在法语操作系统上,查询也能正常工作。您还可以手动编写 WQL 查询...

Apparently its because the interval you specified is too small... I just tried it on a French Windows XP SP3, and got the same error. But if I change the interval to 1 second instead, it works fine... It seems you can't specify an interval smaller than 1 second. Not sure why this only happens on a non-English OS, though...

EDIT: actually I just realized it's probably a bug in WqlEventQuery. The qstart.QueryString looks like that with CurrentCulture = "en-US" :

select * from __InstanceCreationEvent within 0.005 where TargetInstance isa "Win32_Process"

But with CurrentCulture = "fr-FR" it looks like that:

select * from __InstanceCreationEvent within 0,005 where TargetInstance isa "Win32_Process"

(note the difference in the number format)

So apparently the code in WqlEventQuery doesn't force the use of the invariant culture to format the number, making the query incorrect in cultures where the decimal separator is not "."

If you force the CurrentCulture to CultureInfo.Invariant, the query works fine, even on a French OS. You can also write the WQL query manually...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文