Guest User

Untitled

a guest
Jan 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. #include<DeviceInfos.h>
  2.  
  3. QString DisplayVolumePaths( PWCHAR VolumeName )
  4. {
  5. DWORD CharCount = MAX_PATH + 1;
  6. PWCHAR Names = NULL;
  7. PWCHAR NameIdx = NULL;
  8. BOOL Success = FALSE;
  9.  
  10. for (;;)
  11. {
  12. //
  13. // Allocate a buffer to hold the paths.
  14. Names = (PWCHAR) new BYTE [CharCount * sizeof(WCHAR)];
  15.  
  16. if ( !Names )
  17. {
  18. //
  19. // If memory can't be allocated, return.
  20. return "memory can't be allocated";
  21. }
  22.  
  23. //
  24. // Obtain all of the paths
  25. // for this volume.
  26. Success = GetVolumePathNamesForVolumeNameW(
  27. VolumeName, Names, CharCount, &CharCount
  28. );
  29.  
  30. if ( Success )
  31. {
  32. qDebug()<<"GetVolumePathNamesForVolumeNameW succeed count"<<CharCount;
  33. break;
  34. }
  35.  
  36. qDebug()<<"GetVolumePathNamesForVolumeNameW failed count"<<CharCount<<" error "<<GetLastError();
  37. if ( GetLastError() != ERROR_MORE_DATA )
  38. {
  39. break;
  40. }
  41.  
  42. //
  43. // Try again with the
  44. // new suggested size.
  45. delete [] Names;
  46. Names = NULL;
  47. }
  48.  
  49. if ( Success )
  50. {
  51. //
  52. // Display the various paths.
  53. for ( NameIdx = Names;
  54. NameIdx[0] != L'';
  55. NameIdx += wcslen(NameIdx) + 1 )
  56. {
  57. qDebug()<<" "<<QString::fromWCharArray(NameIdx);
  58. return QString::fromWCharArray(NameIdx);
  59. }
  60. qDebug()<<"n";
  61. }
  62.  
  63. if ( Names != NULL )
  64. {
  65. delete [] Names;
  66. Names = NULL;
  67. }
  68.  
  69. return "s";
  70. }
  71.  
  72. QMap<QString,DeviceVolumeName> __cdecl GetDeviceVolumeInfos(void)
  73. {
  74. DWORD CharCount = 0;
  75. WCHAR DeviceName[MAX_PATH] = L"";
  76. DWORD Error = ERROR_SUCCESS;
  77. HANDLE FindHandle = INVALID_HANDLE_VALUE;
  78. BOOL Found = FALSE;
  79. size_t Index = 0;
  80. BOOL Success = FALSE;
  81. WCHAR VolumeName[MAX_PATH] = L"";
  82. QMap<QString,DeviceVolumeName> mapDeviceInfos=QMap<QString,DeviceVolumeName>();
  83.  
  84.  
  85. //
  86. // Enumerate all volumes in the system.
  87. FindHandle = FindFirstVolumeW(VolumeName, ARRAYSIZE(VolumeName));
  88.  
  89. if (FindHandle == INVALID_HANDLE_VALUE)
  90. {
  91. Error = GetLastError();
  92. qDebug()<<"FindFirstVolumeW failed with error code %dn"<< QString::number( Error);
  93.  
  94. return mapDeviceInfos;
  95. }
  96.  
  97. for (;;)
  98. {
  99. DeviceVolumeName DVName;
  100. //
  101. // Skip the \? prefix and remove the trailing backslash.
  102. Index = wcslen(VolumeName) - 1;
  103.  
  104. if (VolumeName[0] != L'\' ||
  105. VolumeName[1] != L'\' ||
  106. VolumeName[2] != L'?' ||
  107. VolumeName[3] != L'\' ||
  108. VolumeName[Index] != L'\')
  109. {
  110. Error = ERROR_BAD_PATHNAME;
  111. qDebug()<<"FindFirstVolumeW/FindNextVolumeW returned a bad path: %sn"<<QString::fromWCharArray( VolumeName);
  112. break;
  113. }
  114.  
  115. //
  116. // QueryDosDeviceW does not allow a trailing backslash,
  117. // so temporarily remove it.
  118. VolumeName[Index] = L'';
  119.  
  120. CharCount = QueryDosDeviceW(&VolumeName[4], DeviceName, ARRAYSIZE(DeviceName));
  121.  
  122. VolumeName[Index] = L'\';
  123.  
  124. if ( CharCount == 0 )
  125. {
  126. Error = GetLastError();
  127. qDebug()<<L"QueryDosDeviceW failed with error code n"<<QString::number( Error);
  128. break;
  129. }
  130.  
  131.  
  132. qDebug()<<"Found a device: "<<QString::fromWCharArray( DeviceName);
  133. qDebug()<<"Volume name: "<< QString::fromWCharArray(VolumeName);
  134. qDebug()<<"Paths: ";
  135. QString VolPath=DisplayVolumePaths(VolumeName);
  136. qDebug()<<"VolPath.at(0)"<<VolPath.at(0);
  137.  
  138.  
  139. DVName.DeviceN=QString::fromWCharArray(DeviceName);
  140. DVName.VolumeN=QString::fromWCharArray(VolumeName);
  141. mapDeviceInfos.insert(VolPath.at(0),DVName);
  142.  
  143.  
  144.  
  145.  
  146. //
  147. // Move on to the next volume.
  148. Success = FindNextVolumeW(FindHandle, VolumeName, ARRAYSIZE(VolumeName));
  149.  
  150. if ( !Success )
  151. {
  152. Error = GetLastError();
  153.  
  154. if (Error != ERROR_NO_MORE_FILES)
  155. {
  156. qDebug()<<"FindNextVolumeW failed with error code n"<<QString::number( Error);
  157. break;
  158. }
  159.  
  160. //
  161. // Finished iterating
  162. // through all the volumes.
  163. Error = ERROR_SUCCESS;
  164. break;
  165. }
  166. }
  167.  
  168. FindVolumeClose(FindHandle);
  169. FindHandle = INVALID_HANDLE_VALUE;
  170.  
  171. return mapDeviceInfos;
  172. }
  173.  
  174. Found a device: "DeviceHarddiskVolume1"
  175. Volume name: "\?Volume{af2c546d-c067-11e1-9f1c-806e6f6e6963}"
  176. Paths:
  177. GetVolumePathNamesForVolumeNameW succeed count 5
  178. "C:"
  179. VolPath.at(0) 'C'
  180. Found a device: "DeviceHarddiskVolume2"
  181. Volume name: "\?Volume{af2c546e-c067-11e1-9f1c-806e6f6e6963}"
  182. Paths:
  183. GetVolumePathNamesForVolumeNameW succeed count 5
  184. "D:"
  185. VolPath.at(0) 'D'
  186. Found a device: "Device{A5E1065D-0AD1-48ED-8457-A80B2D9B6FE2}#0#0"
  187. Volume name: "\?Volume{4dee513c-0b96-11e2-9b3b-d4bed9a624c5}"
  188. Paths:
  189. GetVolumePathNamesForVolumeNameW failed count 261 error 2
  190. VolPath.at(0) 's'
  191. Found a device: "DeviceCdRom0"
  192. Volume name: "\?Volume{af2c5471-c067-11e1-9f1c-806e6f6e6963}"
  193. Paths:
  194. GetVolumePathNamesForVolumeNameW succeed count 5
  195. "E:"
  196. VolPath.at(0) 'E'
  197.  
  198. Found a device: "Device{A5E1065D-0AD1-48ED-8457-A80B2D9B6FE2}#0#0"
  199. Volume name: "\?Volume{4dee513c-0b96-11e2-9b3b-d4bed9a624c5}"
  200. Paths:
  201. GetVolumePathNamesForVolumeNameW failed count 261 error 2
Add Comment
Please, Sign In to add comment