接收不到DBT_DEVICEQUERYREMOVE消息怎麼辦?

接收不到 DBT_DEVICEQUERYREMOVE 消息怎麼辦?

 

註冊這個消息就可以了。

 

ms-help://MS.MSDNQTR.v90.en/devio/base/processing_a_request_to_remove_a_device.htm

 

In the following example, an application maintains an open handle, hFile, to the file or device represented by FileName. The application registers for device event notification on the underlying device by calling the RegisterDeviceNotification function, using a DBT_DEVTYP_HANDLE type notification filter and specifying the hFile variable in the dbch_handle member of the filter.

 

MSDN 上面這麼簡單的一句,弄了半天才明白。

 

下面是註冊方法

 

HANDLE  hU = CreateFile(

                   L"F://" ,

                   GENERIC_READ,

                   FILE_SHARE_READ | FILE_SHARE_WRITE,

                   0,

                   OPEN_EXISTING,

                   FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_NORMAL,

                   0);

 

DEV_BROADCAST_HANDLE  NotificationFilter;

ZeroMemory( &NotificationFilter, sizeof (NotificationFilter) );

NotificationFilter.dbch_size = sizeof (DEV_BROADCAST_HANDLE );

NotificationFilter.dbch_devicetype = DBT_DEVTYP_HANDLE;

NotificationFilter.dbch_handle = hU;

*hDevNotify = RegisterDeviceNotification(hDlg, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);

 

if (!*hDevNotify)

{

         DWORD iRet = GetLastError();

         _tprintf(L"RegisterDeviceNotification failed: %d/n" , GetLastError());

         return FALSE;

}

 

簡單的說一下。就是要定義一個 DEV_BROADCAST_HANDLE 的結構,它的 devicetype 被賦值成 DBT_DEVTYP_HANDLE ,它的 handle createFile 創建出來的,這裏是 F ( 我的 u 盤的盤符 ) ,然後調用 RegisterDeviceNotification 方法,第一個參數是接收 DBT_DEVICEQUERYREMOVE 消息的窗口的句柄。第二個參數是剛纔定義的一個 DEV_BROADCAST_HANDLE ,第三個參數是標誌參數,這裏取值 DEVICE_NOTIFY_WINDOW_HANDLE ,第三個和第一個是配合的,這裏是要把這個消息傳給一個窗口,所以用的這個。

 

在接受這個消息的時候:

 

                            case DBT_DEVICEQUERYREMOVE:

                                     {

                                               PDEV_BROADCAST_HDR pDBHdr;

                                               PDEV_BROADCAST_HANDLE pDBHandle;

                                                pDBHdr = (PDEV_BROADCAST_HDR) lParam;

                                               switch (pDBHdr->dbch_devicetype)

                                               {

                                               case DBT_DEVTYP_HANDLE:

                                                        pDBHandle = (PDEV_BROADCAST_HANDLE) pDBHdr;

                                                        //return BROADCAST_QUERY_DENY ;

                                                        if (hU != INVALID_HANDLE_VALUE)

                                                        {

                                                                 CloseHandle(hU);

                                                                 UnregisterDeviceNotification(pDBHandle->dbch_hdevnotify);

return true;

                                                        }

                                               }

                                               break ;

                                     }

這裏我做的是強制的關掉 U 盤句柄。這樣當你點擊拔出 U 盤時,即使你正在使用 U 盤,也不會跳出 U 盤的提示框了。

 

WM_DEVICECHANGE 裏默認是隻註冊了 DBT_DEVICEREMOVECOMPLETE DBT_DEVICEARRIVAL 消息,所以只能接收到這兩個,如果要使用其他的消息,必需手動註冊一下纔可以。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章