如何在服务程序中监听硬件事件

主要通过RegisterDeviceNotification函数来完成,网上一般能找到都是window程序,很少缺少服务程序的例子。

1. 从网上下载一个服务程序的框架。

2.svcmain 函数中

DEV_BROADCAST_DEVICEINTERFACE  dev_broadcast_deviceinterface = {0};

    dev_broadcast_deviceinterface.dbcc_size = sizeof(dev_broadcast_deviceinterface);

    dev_broadcast_deviceinterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;

    //dev_broadcast_deviceinterface.dbcc_name= NULL;

    dev_broadcast_deviceinterface.dbcc_reserved = 0;

    dev_broadcast_deviceinterface.dbcc_classguid = GUID_DEVINTERFACE_NET;

    hDevNotify = RegisterDeviceNotification(sshStatusHandle, &dev_broadcast_deviceinterface,DEVICE_NOTIFY_SERVICE_HANDLE);

    if(NULL== hDevNotify)

    {

       //error handler

}

注意这里的变量是sshStatusHandle,类型是DEVICE_NOTIFY_SERVICE_HANDLE

SERVICE_STATUS_HANDLE  sshStatusHandle;

sshStatusHandle = RegisterServiceCtrlHandlerEx(pService, (LPHANDLER_FUNCTION_EX)svc_ctrl, NULL);

窗口程序用的是窗口的句柄,服务程序这里用的是SERVICE_STATUS_HANDLE类型,具体就是从RegisterServiceCtrlHandlerEx函数的返回值。

要收到DEVICEEVENT消息需要使用RegisterServiceCtrlHandlerEx,而不是RegisterServiceCtrlHandler

switch(dwCtrlCode)

    {

        case SERVICE_CONTROL_STOP:

        case SERVICE_CONTROL_SHUTDOWN:

            ssStatus.dwWin32ExitCode = 0;

            ssStatus.dwCurrentState = SERVICE_STOP_PENDING;

            ssStatus.dwCheckPoint   = 0;

            ssStatus.dwWaitHint     = 0;

            SetServiceStatus(sshStatusHandle, &ssStatus);

            // terminate all processesstarted by this service before shutdown

            SetEvent(ghRegStopEvent);

            SetEvent(ghSvcStopEvent);

            break;

        case SERVICE_CONTROL_PAUSE:

            ssStatus.dwCurrentState = SERVICE_PAUSED;

            svc_stop(0);

            break;

        case SERVICE_CONTROL_CONTINUE:

            ssStatus.dwCurrentState = SERVICE_RUNNING;

            svc_stop(1);

            break;

        case SERVICE_CONTROL_INTERROGATE:

            break;

        case SERVICE_CONTROL_DEVICEEVENT:

            //我们关注的消息

 OutputDebugStringA("hello world"

            break;     

        default:

         break;

 

If dwControlis SERVICE_CONTROL_DEVICEEVENT, this parameter can beone of the following values:

·        DBT_DEVICEARRIVAL

·        DBT_DEVICEREMOVECOMPLETE

·        DBT_DEVICEQUERYREMOVE

·        DBT_DEVICEQUERYREMOVEFAILED

·        DBT_DEVICEREMOVEPENDING

·        DBT_CUSTOMEVENT

·        lpEventData

·        Additional device information, ifrequired. The format of this data depends on the value of the dwControland dwEventType parameters. If dwEventType isSERVICE_CONTROL_DEVICEEVENT, this data corresponds to the lParamparameter that applications receive as part of a WM_DEVICECHANGEmessage. If dwEventType is SERVICE_CONTROL_POWEREVENT, this data is apointer to a POWERBROADCAST_SETTING structure. If dwEventType isSERVICE_CONTROL_SESSIONCHANGE, this parameter is a pointer to a WTSSESSION_NOTIFICATIONstructure.

从这里可以知道lpEventData类型windows消息的lParam参数,dwControls是具体的类型,根据类型来设置结构体指针就可以正常处理了。




你可能感兴趣的:(Service,硬件,Win32)