如何监控在Windows XP的Windows服务状态的变化? [英] How can I monitor status changes of windows services under windows xp?

查看:185
本文介绍了如何监控在Windows XP的Windows服务状态的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用C编写一个程序,当某些Windows服务(亦称NT服务)启动或停止,可以探测到。

I'm trying to write a program in C, that can detect when some Windows services (aka. NT services) are started or stopped.

有似乎是一个功能 NotifyServiceStatusChange ,但是这仅适用于Vista和Windows 7。我试图做到这一点在Win XP,所以什么是最好的方式?难道还有比连续轮询其他?

There seems to be a function NotifyServiceStatusChange, but that's only available on Vista and Windows 7. I'm trying to do this on Win XP, so what's the best way? Is there any other than continuous polling?

编辑:

时有人能够给出答案C 2我也OK使用C ++,但我想从脚本望而却步。

Is anybody able to give answer in C? I'm also ok with C++, but I'd like to stay away from scripting.

推荐答案

貌似可以在XP中得到最接近的是的 QueryServiceStatusEx (单服务)或 EnumServicesStatusEx (多服务)。

Looks like the closest you can get in XP is QueryServiceStatusEx (single service) or EnumServicesStatusEx (multiple services).

要避免多次拨打其中任意一个,一些推荐WMI设置,查询 win32_service时状态属性。请参见有关详细信息,该线程的底部。

To avoid repeatedly calling either of these, some recommend a WMI setup, querying Win32_Service's state property. See the bottom of this thread for more details.

下面是一个(基本)WMI脚本监视Alerter服务的状态:

The following is a (basic) WMI script to monitor the alerter service's status:

strComputer = "."
Set objSWbemServices = GetObject("winmgmts:" &_
    "{impersonationLevel=impersonate}!" &_
    "\\" & strComputer & "\root\cimv2")

Set objEventSource = objSWbemServices.ExecNotificationQuery( _
    "SELECT * FROM __InstanceModificationEvent " &_
    "WITHIN 10 " &_
    "WHERE TargetInstance " &_
    "ISA 'Win32_Service' " &_
    "AND TargetInstance.Name = 'alerter'")

Set objEventObject = objEventSource.NextEvent()
Wscript.Echo "The status of the alerter service just changed."

上面的和另外的实施例中,可以在这个的TechNet页面

这篇关于如何监控在Windows XP的Windows服务状态的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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