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

主要通过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是具体的类型,根据类型来设置结构体指针就可以正常处理了。




  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值