WINDOWS XP下拦截WM_DEVICECHANGE消息
目的是检测有否U盘插入,并发U盘盘符显示在ListBox中
但当U盘有CD-ROM分区时,盘符发生重复怎么解决?
代码如下:LRESULT OnDeviceChange(HWND hwnd,WPARAM wp,LPARAM lp)
{
PDEV_BROADCAST_HDR lpdb=(PDEV_BROADCAST_HDR)lp;
TCHAR c[3];

switch(wp)
{
case DBT_DEVICEARRIVAL:
if(lpdb->dbch_devicetype==DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv=(PDEV_BROADCAST_VOLUME)lpdb;
c[0]=FirstDriveFromMask(lpdbv->dbcv_unitmask)+32;
c[1]=':';
c[2]='\0';
SendMessage(hListBox,LB_ADDSTRING,0,(LPARAM)c);
}
break;
case DBT_DEVICEREMOVECOMPLETE:
SendMessage(hListBox,LB_RESETCONTENT,0,0);
FindU(hwnd);
break;
}
return 0;
}

解决方案 »

  1.   

    API GetDriveType可以判断DRIVE的类型,例如DRIVE_CDROM  DRIVE_REMOVABLE
      

  2.   

    dbcv_unitmask中可以包含多个盘符,你需要逐一取出。
      

  3.   

    size_t   szAllDriveStrings   =   GetLogicalDriveStrings(0,NULL);   
    char   *pDriveStrings   =   new   char[szAllDriveStrings   +   sizeof(_T( " "))];   
    GetLogicalDriveStrings(szAllDriveStrings,pDriveStrings);   
    size_t   szDriveString   =   strlen(pDriveStrings);   
    while(szDriveString   >   0)   
    {   
    AfxMessageBox(pDriveStrings);   
    pDriveStrings   +=   szDriveString   +   1;   
    szDriveString   =   strlen(pDriveStrings);   
    }   //   pDriveStrings   就索盘符撒 
    ---------------------------------------------------------------   DiskType=GetDriveType(strTempDirver);   
                  switch(DiskType)     
          {   
                case   DRIVE_NO_ROOT_DIR:   
                  return;   
                case   DRIVE_REMOVABLE:   
                  //::AfxMessageBox( "移动存储设备 ");   
                  break;   
                case   DRIVE_FIXED:   
                  //::AfxMessageBox( "固定硬盘驱动器 ");   
                  break;   
                case   DRIVE_REMOTE:   
                  //::AfxMessageBox( "这是网络驱动器 ");   
                  return;   
                case   DRIVE_CDROM:   
                  //::AfxMessageBox( "这是光盘驱动器 ");   
                  return;