RemoveUsb.cpp
上传用户:posgewe
上传日期:2022-07-05
资源大小:164k
文件大小:3k
源码类别:

系统编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <winioctl.h>
  4. #include <process.h>
  5. #include <tchar.h>
  6. #include <stdio.h>
  7. #include "DoStorageEnum.h"
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define MAX_DEVICE 16
  11. DEFINE_GUID(DiskClassGuid,               0x53f56307L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
  12. DEFINE_GUID(CdRomClassGuid,              0x53f56308L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
  13. //DEFINE_GUID(PartitionClassGuid,          0x53f5630aL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
  14. DEFINE_GUID(TapeClassGuid,               0x53f5630bL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
  15. //DEFINE_GUID(WriteOnceDiskClassGuid,      0x53f5630cL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
  16. //DEFINE_GUID(VolumeClassGuid,             0x53f5630dL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
  17. //DEFINE_GUID(MediumChangerClassGuid,      0x53f56310L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
  18. //DEFINE_GUID(FloppyClassGuid,             0x53f56311L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
  19. //DEFINE_GUID(CdChangerClassGuid,          0x53f56312L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
  20. //DEFINE_GUID(StoragePortClassGuid,        0x2accfe60L, 0xc130, 0x11d2, 0xb0, 0x82, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
  21. char * MakeDriveInfo(LPCTSTR pszDevicePath, PDISK_GEOMETRY pGeometry, PSTORAGE_DEVICE_DESCRIPTOR pDevDesc);
  22. char * MakeDriveInfo(LPCTSTR pszDevicePath, PDISK_GEOMETRY pGeometry, PSTORAGE_DEVICE_DESCRIPTOR pDevDesc)
  23. {
  24. char* p;
  25. char* strTmp;
  26. p= (char*)pDevDesc;
  27. strTmp = (pDevDesc->ProductIdOffset ? &p[pDevDesc->ProductIdOffset] : "");
  28. return strTmp;
  29. }
  30. /*-------------------------------------------------------------------------*/
  31. char* WINAPI GetDiskId(int index)
  32. {
  33. char* strInfo;
  34. HANDLE hDevice;
  35. PDISK_GEOMETRY pGeometry;
  36. PSTORAGE_DEVICE_DESCRIPTOR pDevDesc;
  37. int nDevice;
  38. int i,j;
  39. char* szDevicePath[MAX_DEVICE]; // 设备路径
  40. LPGUID lpGuid[] = {
  41. (LPGUID)&DiskClassGuid, 
  42. (LPGUID)&CdRomClassGuid, 
  43. // (LPGUID)&TapeClassGuid,
  44. }; // 一些存储设备的GUID
  45. // 分配需要的空间
  46. for(i=0;i<MAX_DEVICE;i++) szDevicePath[i]=new char[256];
  47. pGeometry = new DISK_GEOMETRY;
  48. pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)new BYTE[sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1];
  49. pDevDesc->Size = sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1;
  50. // 对感兴趣的类型的存储设备,逐一获取设备信息
  51. for(i=0; i<sizeof(lpGuid)/sizeof(LPGUID); i++)
  52. {
  53. // 取设备路径
  54. nDevice=::GetDevicePath(lpGuid[i], szDevicePath);
  55. // 对同一类的存储设备,逐一获取设备信息
  56. for(j=0; j<nDevice; j++)
  57. {
  58. // 打开设备
  59. hDevice=OpenDevice(szDevicePath[j]);
  60. if(hDevice != INVALID_HANDLE_VALUE)
  61. {
  62. // 取设备信息
  63. GetDriveGeometry(hDevice, pGeometry);
  64. GetDriveProperty(hDevice, pDevDesc);
  65. // 制作显示信息串
  66. if(j==index)
  67. {
  68.    strInfo = MakeDriveInfo(szDevicePath[j], pGeometry, pDevDesc);
  69. }
  70. // 关闭设备
  71. CloseHandle(hDevice);
  72. }
  73. }
  74. }
  75. // 释放空间
  76. delete pDevDesc;
  77. delete pGeometry;
  78. for(i=0;i<MAX_DEVICE;i++) delete []szDevicePath[i];
  79. return strInfo;
  80. }