日期:2014-05-18  浏览次数:20853 次

请问有没有设备状态变更通知的api
问题如下
1.有没有win api能实现功能:当系统设备的状态发生变更后能发个消息通知应用程序。
2.程序的主要功能就是监控用户系统设备的状态的(比如禁用、启动),目前已经能够获取所有设备的状态,也能对其进行控制,关键是状态的维持。
3.状态的维持目前有2种方案:
3.1轮询遍历的方法,就是隔一段时间就检查下所有设备状态,一旦发生改变就立刻改回来。(已经实现)
3.2当系统的某个设备状态改变后,能自动发给消息给程序(或者说能捕捉到这个消息,反正就是这个意思),程序就知道是哪个设备改变了,就能把他改回来,而不用一直检查所有设备。(这就是当前所请教的问题)
4.大家只要告诉我有没有这种api就可以了,或者帮偶看看RegisterDeviceNotification这个api有没有这个功能。
5.谢谢大家能够帮忙!

------解决方案--------------------
RegisterDeviceNotification配合WM_DEVICECHANGE消息,不过好像都是设备添加、删除、插入一类的通知,没看到启用、禁用的
------解决方案--------------------
Platform SDK: Device I/O 
RegisterDeviceNotification

The RegisterDeviceNotification function registers the device or type of device for which a window will receive notifications.


HDEVNOTIFY RegisterDeviceNotification(
HANDLE hRecipient,
LPVOID NotificationFilter,
DWORD Flags
);

Parameters
hRecipient 
[in] Handle to the window or service that will receive device events for the devices specified in the NotificationFilter parameter. The same window handle can be used in multiple calls to RegisterDeviceNotification. 
Services can specify either a window handle or service status handle.

NotificationFilter 
[in] Pointer to a block of data that specifies the type of device for which notifications should be sent. This block always begins with the DEV_BROADCAST_HDR structure. The data following this header is dependent on the value of the dbch_devicetype member, which can be DBT_DEVTYP_DEVICEINTERFACE or DBT_DEVTYP_HANDLE. For more information, see Remarks. 
Flags 
[in] This parameter can be one of the following values. Type Meaning 
DEVICE_NOTIFY_WINDOW_HANDLE The hRecipient parameter is a window handle. 
DEVICE_NOTIFY_SERVICE_HANDLE The hRecipient parameter is a service status handle. 


In addition, you can specify the following value.


Value Meaning 
DEVICE_NOTIFY_ALL_INTERFACE_CLASSES Notifies the recipient of device interface events for all device interface classes. (The dbcc_classguid member is ignored.) 
This value can be used only if the dbch_devicetype member is DBT_DEVTYP_DEVICEINTERFACE.

Windows 2000 and Windows Me/98: This value is not supported. 

Return Values
If the function succeeds, the return value is a device notification handle.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks
Applications send event notifications using the BroadcastSystemMessage function. Any application with a top-level window can receive basic notifications by processing the WM_DEVICECHANGE message. Applications can use the RegisterDeviceNotification function to register to receive device notifications.

Services can use the RegisterDeviceNotification function to register to receive device notifications. If a service specifies a window handle in the hRecipient parameter, the notifications are sent to the window procedure. If hRecipient is a service status handle, SERVICE_CONTROL_DEVICEEVENT notifications are sent to the service control handler. For more information about the service control handler, see HandlerEx.

Be sure to handle Plug and Play device events as quickly as possible. Otherwise, the system may become unresponsive. If your event handler is to perform an operation that may block execution (such as I/O), it is best to start another thread to perform the operation asynchronously.

Device notification handles returned by RegisterDeviceNotification must be closed by calling the UnregisterDeviceNotification function when they are no longer needed.

The DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE events are automatically broad