WM_DEVICECHANGE message

msdn:http://msdn.microsoft.com/en-us/library/windows/desktop/aa363480%28v=vs.85%29.aspx

otifies an application of a change to the hardware configuration of a device or the computer.

A window receives this message through its WindowProc function.

C++
LRESULT CALLBACK WindowProc(HWND   hwnd,     // handle to window
                            UINT   uMsg,     // WM_DEVICECHANGE
                            WPARAM wParam,   // device-change event
                            LPARAM lParam ); // event-specific data

Parameters

hwnd

A handle to the window.

uMsg

The WM_DEVICECHANGE identifier.

wParam

The event that has occurred. This parameter can be one of the following values from the Dbt.h header file.

ValueMeaning
DBT_CONFIGCHANGECANCELED 0x0019

A request to change the current configuration (dock or undock) has been canceled.

DBT_CONFIGCHANGED 0x0018

The current configuration has changed, due to a dock or undock.

DBT_CUSTOMEVENT 0x8006

A custom event has occurred.

DBT_DEVICEARRIVAL 0x8000

A device or piece of media has been inserted and is now available.

DBT_DEVICEQUERYREMOVE 0x8001

Permission is requested to remove a device or piece of media. Any application can deny this request and cancel the removal.

DBT_DEVICEQUERYREMOVEFAILED 0x8002

A request to remove a device or piece of media has been canceled.

DBT_DEVICEREMOVECOMPLETE 0x8004

A device or piece of media has been removed.

DBT_DEVICEREMOVEPENDING 0x8003

A device or piece of media is about to be removed. Cannot be denied.

DBT_DEVICETYPESPECIFIC 0x8005

A device-specific event has occurred.

DBT_DEVNODES_CHANGED 0x0007

A device has been added to or removed from the system.

DBT_QUERYCHANGECONFIG 0x0017

Permission is requested to change the current configuration (dock or undock).

DBT_USERDEFINED 0xFFFF

The meaning of this message is user-defined.

 

lParam

A pointer to a structure that contains event-specific data. Its format depends on the value of the wParam parameter. For more information, refer to the documentation for each event.

Return value

Return TRUE to grant the request.

Return BROADCAST_QUERY_DENY to deny the request.

Remarks

For devices that offer software-controllable features, such as ejection and locking, the system typically sends a DBT_DEVICEREMOVEPENDING message to let applications and device drivers end their use of the device gracefully. If the system forcibly removes a device, it may not send a DBT_DEVICEQUERYREMOVE message before doing so.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

Winuser.h (include Windows.h or Dbt.h)

See also

DBT_CONFIGCHANGECANCELED DBT_CONFIGCHANGED DBT_CUSTOMEVENT DBT_DEVICEARRIVAL DBT_DEVICEQUERYREMOVE DBT_DEVICEQUERYREMOVEFAILED DBT_DEVICEREMOVECOMPLETE DBT_DEVICEREMOVEPENDING DBT_DEVICETYPESPECIFIC DBT_DEVNODES_CHANGED DBT_QUERYCHANGECONFIG DBT_USERDEFINED

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要检测Windows中的设备变更,可以使用Windows API提供的一些函数和消息。 一种方法是使用RegisterDeviceNotification函数注册一个设备通知,然后在设备变更时接收通知。这个函数有以下几个参数: - hRecipient: 接收通知的窗口句柄。 - NotificationFilter: 指定要接收通知的设备类别。 - Flags: 指定设备通知的类型。 在接收到设备变更通知时,系统会发送WM_DEVICECHANGE消息给接收通知的窗口。你可以通过重写窗口过程(Window Procedure)来处理这个消息,并根据需要进行相应的处理。 以下是一个简单的示例代码,演示了如何使用RegisterDeviceNotification函数和处理WM_DEVICECHANGE消息: ```c #include <Windows.h> LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { if (message == WM_DEVICECHANGE) { // 处理设备变更通知 PDEV_BROADCAST_HDR pBroadcastHdr = (PDEV_BROADCAST_HDR)lParam; switch (wParam) { case DBT_DEVICEARRIVAL: // 设备插入 if (pBroadcastHdr->dbch_devicetype == DBT_DEVTYP_PORT) { // 串口设备插入 PDEV_BROADCAST_PORT pBroadcastPort = (PDEV_BROADCAST_PORT)pBroadcastHdr; // 处理插入的串口设备 } else if (pBroadcastHdr->dbch_devicetype == DBT_DEVTYP_VOLUME) { // 磁盘设备插入 PDEV_BROADCAST_VOLUME pBroadcastVolume = (PDEV_BROADCAST_VOLUME)pBroadcastHdr; // 处理插入的磁盘设备 } break; case DBT_DEVICEREMOVECOMPLETE: // 设备移除 if (pBroadcastHdr->dbch_devicetype == DBT_DEVTYP_PORT) { // 串口设备移除 PDEV_BROADCAST_PORT pBroadcastPort = (PDEV_BROADCAST_PORT)pBroadcastHdr; // 处理移除的串口设备 } else if (pBroadcastHdr->dbch_devicetype == DBT_DEVTYP_VOLUME) { // 磁盘设备移除 PDEV_BROADCAST_VOLUME pBroadcastVolume = (PDEV_BROADCAST_VOLUME)pBroadcastHdr; // 处理移除的磁盘设备 } break; default: break; } } return DefWindowProc(hWnd, message, wParam, lParam); } int main() { // 创建窗口 HWND hWnd = CreateWindowEx(0, L"STATIC", L"Device Change Window", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL); // 注册设备通知 DEV_BROADCAST_DEVICEINTERFACE filter = {}; filter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE); filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; filter.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE; // 这里以USB设备为例 HDEVNOTIFY hDevNotify = RegisterDeviceNotification(hWnd, &filter, DEVICE_NOTIFY_WINDOW_HANDLE); // 创建消息循环 MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } // 注销设备通知 UnregisterDeviceNotification(hDevNotify); return 0; } ``` 这个示例代码创建了一个窗口,并注册了一个USB设备的设备通知。在设备插入或移除时,会通过处理WM_DEVICECHANGE消息来处理相应的逻辑。你可以根据需要修改处理逻辑,并根据具体的设备类型进行处理。 希望对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值