如何在服务程序中监听硬件事件
2016-07-12 13:58
477 查看
主要通过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是具体的类型,根据类型来设置结构体指针就可以正常处理了。
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是具体的类型,根据类型来设置结构体指针就可以正常处理了。
相关文章推荐
- Android IPC进程间通讯机制
- android之定时器AlarmManager
- axis备忘
- 康诺云推出三款智能硬件产品,为健康管理业务搭建数据池
- Windows XP Service Pack 3 RC1 v.3244 winxp补丁3 提供下载
- Run As Service runassrv.exe 详细参数第1/2页
- 安装MySQL在最后的start service停住了解决方法
- android使用Messenger绑定Service的多种实现方法
- win32下进程间通信(共享内存)实例分析
- asp.net Web Service 接口大量数据传输解决方案
- Silverlight中动态获取Web Service地址
- WMI中的Win32_PingStatus类(ping命令实现)
- win32安装配置非安装版的MySQL
- 一个win32窗口创建示例
- android调用web service(cxf)实例应用详解
- 在Android中 获取正在运行的Service 实例
- ASP.NET State service状态服务的问题解决方法
- 卸载ZkeysPHP 后iis网站出现Service Unavailable 解决办法