How can I monitor status changes of windows services under windows xp?

筅森魡賤 提交于 2019-11-28 05:18:33

问题


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

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?

edit:

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


回答1:


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

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.

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."

The above, and additional examples, may be found on this TechNet page.




回答2:


You will need to do it by polling. Place the code in a separate thread and send it to sleep for as long as reasonable. Say every second, perhaps even 5 seconds to minimize system performance.

As a 'c' example for a single service:

// various handles and strings plus... SERVICE_STATUS ssStatus; ...

    schSCManager = OpenSCManager( ServiceComputerNameStr,
                                  NULL,
                                  SC_MANAGER_ALL_ACCESS );
    if ( schSCManager == NULL )
        {
//        ...  error stuff  
        goto cleanup;
        }

    scphService = OpenService( schSCManager,
                               ServiceNameStr,
//                               SERVICE_QUERY_STATUS );
                               SERVICE_ALL_ACCESS );
    if ( scphService == NULL )
        {
//        ... error stuff  
        goto cleanup;
        }

    if ( !QueryServiceStatus(scphService, ssStatus) )
        {
//        ... error stuff  
        goto cleanup;
        }

The result you want will be in the ssStatus.dwCurrentState.



来源:https://stackoverflow.com/questions/1189815/how-can-i-monitor-status-changes-of-windows-services-under-windows-xp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!