DBT_CONFIGCHANGECANCELED A request to change the current configuration (dock or undock) has been canceled. DBT_CONFIGCHANGED The current configuration has changed, due to a dock or undock. DBT_DEVICEARRIVAL A device has been inserted and is now available. DBT_DEVICEQUERYREMOVE Permission is requested to remove a device. Any application can deny this request and cancel the removal. DBT_DEVICEQUERYREMOVEFAILED A request to remove a device has been canceled. DBT_DEVICEREMOVECOMPLETE A device has been removed. DBT_DEVICEREMOVEPENDING A device is about to be removed. Cannot be denied. DBT_DEVICETYPESPECIFIC A device-specific event has occurred. DBT_QUERYCHANGECONFIG Permission is requested to change the current configuration (dock or undock).
MADN中定义的事件如下:
DBT_CONFIGCHANGECANCELED A request to change the current configuration (dock or undock) has been canceled.
DBT_CONFIGCHANGED The current configuration has changed, due to a dock or undock.
DBT_DEVICEARRIVAL A device has been inserted and is now available.
DBT_DEVICEQUERYREMOVE Permission is requested to remove a device. Any application can deny this request and cancel the removal.
DBT_DEVICEQUERYREMOVEFAILED A request to remove a device has been canceled.
DBT_DEVICEREMOVECOMPLETE A device has been removed.
DBT_DEVICEREMOVEPENDING A device is about to be removed. Cannot be denied.
DBT_DEVICETYPESPECIFIC A device-specific event has occurred.
DBT_QUERYCHANGECONFIG Permission is requested to change the current configuration (dock or undock).
其中DBT_DEVICEARRIVAL表示设备插入。这样我们的程序只需要建立一个窗口,便可接收这个消息,然后lParam为指向DEV_BROADCAST_HDR结构的指针。如果其中的dbch_devicetype参数为BT_DEVTYP_VOLUME,则lParam又可以理解为PDEV_BROADCAST_VOLUME的指针,其中的dbcv_unitmask指出了盘符。(MSDN自带了一个非常不错的例子)
利用的代码如下:
void OnDeviceChange(HWND hWnd,WPARAM wParam,LPARAM lParam)
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
switch(wParam)
{
case DBT_DEVICEARRIVAL:
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
ULONG unitmask=lpdbv ->dbcv_unitmask;
for (int i = 0; i < 26; ++i)
{
//取得设备的盘号
if (unitmask & 0x1)
{
//这里我们加入你要做的事
//其中i+'A'表示当前的盘符
}
unitmask = unitmask >> 1;
}
}
}
return;
}