根据盘符安全移除U盘

  1. //
  2. //http://www.codeproject.com/KB/system/RemoveDriveByLetter.aspx
  3. //根据盘符停止U盘
  4. BOOL  CPubFunction::StopDeviceByLetter(LPCTSTR lpDisk)  
  5. {   
  6.     TCHAR szRootPath[] = _T("X://");   // "X:/"  -> for GetDriveType
  7.     szRootPath[0] = lpDisk[0];
  8.     TCHAR szDevicePath[] = _T("X:");   // "X:"   -> for QueryDosDevice
  9.     szDevicePath[0] = lpDisk[0];
  10.     CString strLogicDisk;
  11.     strLogicDisk.Format(_T(".//%c:"), lpDisk[0]);   //   "//./X:" 
  12.     DWORD DiskNumber   =   -1;   
  13.     HANDLE   hVolume = CreateFile(strLogicDisk, 0, FILE_SHARE_READ| FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);   
  14.     if(hVolume   ==   INVALID_HANDLE_VALUE)   
  15.     {   
  16.         return 0;   
  17.     }   
  18.     STORAGE_DEVICE_NUMBER  sdn;   
  19.     DWORD  dwBytesReturned = 0;   
  20.     BOOL res = DeviceIoControl(hVolume, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn), &dwBytesReturned, NULL);   
  21.     if(res)   
  22.     {   
  23.         DiskNumber = sdn.DeviceNumber;   
  24.     }   
  25.     CloseHandle(hVolume);   
  26.     if(DiskNumber==-1   )   
  27.     {   
  28.         return   0;   
  29.     }   
  30.     
  31.     // get the drive type which is required to match the device numbers correctely
  32.     UINT DriveType = GetDriveType(lpDisk);
  33.     // get the dos device name (like /device/floppy0) to decide if it's a floppy or not - who knows a better way?
  34.     TCHAR szDosDeviceName[MAX_PATH];
  35.     res = QueryDosDevice(szDevicePath, szDosDeviceName, MAX_PATH);
  36.     if ( !res ) {
  37.         return 0;
  38.     }
  39.     // get the device instance handle of the storage volume by means of a SetupDi enum and matching the device number
  40.     DWORD DevInst = GetDrivesDevInstByDeviceNumber(DiskNumber, DriveType, szDosDeviceName);
  41.     if ( DevInst == 0 ) {
  42.         return 0;
  43.     }
  44.     PNP_VETO_TYPE VetoType = PNP_VetoTypeUnknown; 
  45.     WCHAR VetoNameW[MAX_PATH];
  46.     VetoNameW[0] = 0;
  47.     BOOL bSuccess = FALSE;
  48.     // get drives's parent, e.g. the USB bridge, the SATA port, an IDE channel with two drives!
  49.     DWORD DevInstParent = 0;
  50.     res = CM_Get_Parent(&DevInstParent, DevInst, 0); 
  51.     for ( int tries=1; tries<=3; tries++ ) { // sometimes we need some tries...
  52.         VetoNameW[0] = 0;
  53.         // CM_Query_And_Remove_SubTree doesn't work for restricted users
  54.         //res = CM_Query_And_Remove_SubTreeW(DevInstParent, &VetoType, VetoNameW, MAX_PATH, CM_REMOVE_NO_RESTART); // CM_Query_And_Remove_SubTreeA is not implemented under W2K!
  55.         //res = CM_Query_And_Remove_SubTreeW(DevInstParent, NULL, NULL, 0, CM_REMOVE_NO_RESTART);  // with messagebox (W2K, Vista) or balloon (XP)
  56.         
  57.         //res = CM_Request_Device_EjectW(DevInstParent, &VetoType, VetoNameW, MAX_PATH, 0);
  58.         res = CM_Request_Device_EjectW(DevInstParent, NULL, NULL, 0, 0); // with messagebox (W2K, Vista) or balloon (XP)
  59.         bSuccess = (res==CR_SUCCESS && VetoType==PNP_VetoTypeUnknown);
  60.         if ( bSuccess )  { 
  61.             break;
  62.         }
  63.         Sleep(500); // required to give the next tries a chance!
  64.     }
  65.     if ( bSuccess ) {
  66.     //  printf("Success/n/n");
  67.         return 1;
  68.     }
  69.     //printf("failed/n");
  70.     
  71.     //printf("Result=0x%2X/n", res);
  72.     //if ( VetoNameW[0] ) {
  73.     //  printf("VetoName=%ws)/n/n", VetoNameW);
  74.     //} 
  75.     return 0;
  76. }   
  77. //-----------------------------------------------------------   
  78. //http://www.codeproject.com/KB/system/RemoveDriveByLetter.aspx
  79. //----------------------------------------------------------------------
  80. // returns the device instance handle of a storage volume or 0 on error
  81. //----------------------------------------------------------------------
  82. DEVINST CPubFunction::GetDrivesDevInstByDeviceNumber(DWORD DeviceNumber, UINT DriveType, TCHAR* szDosDeviceName)
  83. {
  84. //  bool IsFloppy = (strstr(szDosDeviceName, "//Floppy") != NULL); // who knows a better way?
  85.     GUID* guid;
  86.     switch (DriveType) {
  87.     case DRIVE_REMOVABLE:
  88. //      if ( IsFloppy ) {
  89. //          guid = (GUID*)&GUID_DEVINTERFACE_FLOPPY;
  90. //      } else {
  91.             guid = (GUID*)&GUID_DEVINTERFACE_DISK;
  92. //      }
  93.         break;
  94.     case DRIVE_FIXED:
  95.         guid = (GUID*)&GUID_DEVINTERFACE_DISK;
  96.         break;
  97.     case DRIVE_CDROM:
  98.         guid = (GUID*)&GUID_DEVINTERFACE_CDROM;
  99.         break;
  100.     default:
  101.         return 0;
  102.     }
  103.     // Get device interface info set handle for all devices attached to system
  104.     HDEVINFO hDevInfo = SetupDiGetClassDevs(guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  105.     if (hDevInfo == INVALID_HANDLE_VALUE)   {
  106.         return 0;
  107.     }
  108.     // Retrieve a context structure for a device interface of a device information set
  109.     DWORD dwIndex = 0;
  110.     int res;
  111.     BYTE Buf[1024];
  112.     PSP_DEVICE_INTERFACE_DETAIL_DATA pspdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)Buf;
  113.     SP_DEVICE_INTERFACE_DATA         spdid;
  114.     SP_DEVINFO_DATA                  spdd;
  115.     DWORD                            dwSize;
  116.     
  117.     spdid.cbSize = sizeof(spdid);
  118.     while ( true )  {
  119.         res = SetupDiEnumDeviceInterfaces(hDevInfo, NULL, guid, dwIndex, &spdid);
  120.         if ( !res ) {
  121.             break;
  122.         }
  123.         dwSize = 0;
  124.         SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, NULL, 0, &dwSize, NULL); // check the buffer size
  125.         if ( dwSize!=0 && dwSize<=sizeof(Buf) ) {
  126.             pspdidd->cbSize = sizeof(*pspdidd); // 5 Bytes!
  127.             ZeroMemory(&spdd, sizeof(spdd));
  128.             spdd.cbSize = sizeof(spdd);
  129.             int res = SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, pspdidd, dwSize, &dwSize, &spdd);
  130.             if ( res ) {
  131.                 // in case you are interested in the USB serial number:
  132.                 // the device id string contains the serial number if the device has one,
  133.                 // otherwise a generated id that contains the '&' char...
  134.                 /*
  135.                 DEVINST DevInstParent = 0;
  136.                 CM_Get_Parent(&DevInstParent, spdd.DevInst, 0); 
  137.                 char szDeviceIdString[MAX_PATH];
  138.                 CM_Get_Device_ID(DevInstParent, szDeviceIdString, MAX_PATH, 0);
  139.                 printf("DeviceId=%s/n", szDeviceIdString);
  140.                 */
  141.                 // open the disk or cdrom or floppy
  142.                 HANDLE hDrive = CreateFile(pspdidd->DevicePath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  143.                 if ( hDrive != INVALID_HANDLE_VALUE ) {
  144.                     // get its device number
  145.                     STORAGE_DEVICE_NUMBER sdn;
  146.                     DWORD dwBytesReturned = 0;
  147.                     res = DeviceIoControl(hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn), &dwBytesReturned, NULL);
  148.                     if ( res ) {
  149.                         if ( DeviceNumber == (DWORD)sdn.DeviceNumber ) {  // match the given device number with the one of the current device
  150.                             CloseHandle(hDrive);
  151.                             SetupDiDestroyDeviceInfoList(hDevInfo);
  152.                             return spdd.DevInst;
  153.                         }
  154.                     }
  155.                     CloseHandle(hDrive);
  156.                 }
  157.             }
  158.         }
  159.         dwIndex++;
  160.     }
  161.     SetupDiDestroyDeviceInfoList(hDevInfo);
  162.     return 0;
  163. }
  164. //-----------------------------------------------------------

 

这个方法可以用来确定多盘符U盘的多个逻辑盘符所对应的物理U盘。根据盘符可以得到这个设备接口的句柄DevInst, 通过CM_Get_Parent函数可以得到这个句柄对应的父设备的句柄,这个父设备的句柄就是两个设备间的usb pridge,  SATA port, IDE channel 。而同一个物理U盘的各个逻辑盘符的父设备的句柄则相同。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值