Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1695977
  • 博文数量: 584
  • 博客积分: 13857
  • 博客等级: 上将
  • 技术积分: 11883
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-16 09:34

分类: WINDOWS

2011-04-04 16:13:30

Windows sends all top-level windows a set of default messages when new devices or media (such as a CD or DVD) are added and become available, and when existing devices or media are removed. You do not need to register to receive these default messages. See the Remarks section in for details on which messages are sent by default. The messages in the code example below are among the default messages.

Each message has an associated event that describes the change, and a structure that provides detailed information about the change. The structure consists of an event-independent header, , followed by event-dependent members. The event-dependent members describe the device to which the event applies. To use this structure, applications must first determine the event type and the device type. Then, they can use the correct structure to take appropriate action.

When the user inserts a new CD or DVD into a drive, applications receive a message with a event. The application must check the event to ensure that the type of device arriving is a volume (the dbch_devicetype member is DBT_DEVTYP_VOLUME) and that the change affects the media (the dbcv_flags member is DBTF_MEDIA).

When the user removes a CD or DVD from a drive, applications receive a message with a event. Again, the application must check the event to ensure that the device being removed is a volume and that the change affects the media.

The following code demonstrates how to check for insertion or removal of a CD or DVD.

  1. #include <windows.h>
  2. #include <dbt.h>
  3. #include <strsafe.h>
  4. #pragma comment(lib, "user32.lib" )

  5. void Main_OnDeviceChange( HWND hwnd, WPARAM wParam, LPARAM lParam );
  6. char FirstDriveFromMask( ULONG unitmask ); //prototype

  7. /*------------------------------------------------------------------
  8.    Main_OnDeviceChange( hwnd, wParam, lParam )

  9.    Description
  10.       Handles WM_DEVICECHANGE messages sent to the application's
  11.       top-level window.
  12. --------------------------------------------------------------------*/

  13. void Main_OnDeviceChange( HWND hwnd, WPARAM wParam, LPARAM lParam )
  14.  {
  15.   PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
  16.   TCHAR szMsg[80];

  17.   switch(wParam )
  18.    {
  19.     case DBT_DEVICEARRIVAL:
  20.       // Check whether a CD or DVD was inserted into a drive.
  21.       if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
  22.        {
  23.         PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

  24.         if (lpdbv -> dbcv_flags & DBTF_MEDIA)
  25.          {
  26.           StringCchPrintf( szMsg, sizeof(szMsg)/sizeof(szMsg[0]),
  27.                            TEXT("Drive %c: Media has arrived.\n"),
  28.                            FirstDriveFromMask(lpdbv ->dbcv_unitmask) );

  29.           MessageBox( hwnd, szMsg, TEXT("WM_DEVICECHANGE"), MB_OK );
  30.          }
  31.        }
  32.       break;

  33.     case DBT_DEVICEREMOVECOMPLETE:
  34.       // Check whether a CD or DVD was removed from a drive.
  35.       if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
  36.        {
  37.         PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

  38.         if (lpdbv -> dbcv_flags & DBTF_MEDIA)
  39.          {
  40.           StringCchPrintf( szMsg, sizeof(szMsg)/sizeof(szMsg[0]),
  41.                            TEXT("Drive %c: Media was removed.\n" ),
  42.                            FirstDriveFromMask(lpdbv ->dbcv_unitmask) );

  43.           MessageBox( hwnd, szMsg, TEXT("WM_DEVICECHANGE" ), MB_OK );
  44.          }
  45.        }
  46.       break;

  47.     default:
  48.       /*
  49.         Process other WM_DEVICECHANGE notifications for other
  50.         devices or reasons.
  51.       */
  52.       ;
  53.    }
  54. }

  55. /*------------------------------------------------------------------
  56.    FirstDriveFromMask( unitmask )

  57.    Description
  58.      Finds the first valid drive letter from a mask of drive letters.
  59.      The mask must be in the format bit 0 = A, bit 1 = B, bit 2 = C,
  60.      and so on. A valid drive letter is defined when the
  61.      corresponding bit is set to 1.

  62.    Returns the first drive letter that was found.
  63. --------------------------------------------------------------------*/

  64. char FirstDriveFromMask( ULONG unitmask )
  65.  {
  66.   char i;

  67.   for (i = 0; i < 26; ++i)
  68.    {
  69.     if (unitmask & 0x1)
  70.       break;
  71.     unitmask = unitmask >> 1;
  72.    }

  73.   return( i + 'A' );
  74. }


Related Topics

 

阅读(829) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~