1. 程式人生 > >vc++中新增WM_DEVICECHANGE訊息函式

vc++中新增WM_DEVICECHANGE訊息函式

1.新增訊息
訊息對映
BEGIN_MESSAGE_MAP(CAFT_1394Dlg, CDialog)
//{{AFX_MSG_MAP(CAFT_1394Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
...
ON_WM_DEVICECHANGE()
//}}AFX_MSG_MAP

聲 明訊息響應函式:
afx_msg BOOL OnDeviceChange(UINT nEventType, DWORD dwData);

定 義函式內容:
BOOL XXXXXXX::OnDeviceChange(UINT nEventType,DWORD dwData)
{
   DEV_BROADCAST_DEVICEINTERFACE* dbd = (DEV_BROADCAST_DEVICEINTERFACE*) dwData;

   //這裡進行資訊匹配,比如guid等

// 針對各個事件進行處理.
switch (nEventType)
{
case DBT_DEVICEREMOVECOMPLETE:
   ...
   break;
case DBT_DEVICEARRIVAL:
   ...
   break;
...
...
...

default:
   break;
}

return TRUE;

}

2.註冊裝置


if (Handle == NULL)
   return FALSE;

DEV_BROADCAST_DEVICEINTERFACE DevInt;
memset(&DevInt,0,sizeof(DEV_BROADCAST_DEVICEINTERFACE));
DevInt.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
DevInt.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;

DevInt.dbcc_classguid = GetCurrentUSBGUID();//m_usb->GetDriverGUID();
if (!RegisterDeviceNotification(m_hWnd, &DevInt,DEVICE_NOTIFY_WINDOW_HANDLE) )
   return FALSE;

只有 註冊了該裝置,OnDeviceChange才能獲得詳細的資訊,否則收到的引數都是0007.