//function check for new partition //get volume letter for each partition //and set free letter for it int CDiskRepartition::enumeratevolumes(void) { //buffer for dos device volume name char Let[4]; //buffer for GUID volume name char buf[BUFSIZE]; HANDLE hVol; BOOL bFlag = true; sprintf(Let,"%c:\\",DiskName); hVol = FindFirstVolumeA(buf, BUFSIZE ); if (hVol == INVALID_HANDLE_VALUE) { printf (TEXT("No volumes found!\n")); return (-1); } while (bFlag) { bFlag = FindNextVolumeA(hVol, buf, BUFSIZE ); if (GetVolumesLetter(buf)<65) { OutputDebugStringA(buf); SetVolumeMountPoint(Let,buf); bFlag = FindVolumeClose(hVol); return false; } } bFlag = FindVolumeClose(hVol); return true; }
bool PrintMountPoints(const char *path) { char volumeUniqueName[128]; BOOL res = GetVolumeNameForVolumeMountPointA(path, volumeUniqueName, 128); if (!res) { return false; } char buf[BUFSIZE]; // buffer for unique volume identifiers HANDLE hVol; // handle for the volume scan // Open a scan for volumes. hVol = FindFirstVolumeA(buf, BUFSIZE); // Shall we error out? if (hVol == INVALID_HANDLE_VALUE) { return false; } bool success = true; do { if (!strcmp(buf, volumeUniqueName)) { success = PrintMountPointsForVolume(hVol, path, buf); if (!success) break; } } while (FindNextVolumeA(hVol, buf, BUFSIZE)); FindVolumeClose(hVol); return success; }
/* * Return the first GUID volume name for the associated drive or NULL if not found * See http://msdn.microsoft.com/en-us/library/cc542456.aspx * The returned string is allocated and must be freed */ char* GetLogicalName(DWORD DriveIndex, BOOL bKeepTrailingBackslash, BOOL bSilent) { BOOL success = FALSE; char volume_name[MAX_PATH]; HANDLE hDrive = INVALID_HANDLE_VALUE, hVolume = INVALID_HANDLE_VALUE; size_t len; char path[MAX_PATH]; VOLUME_DISK_EXTENTS DiskExtents; DWORD size; UINT drive_type; int i, j; static const char* ignore_device[] = { "\\Device\\CdRom", "\\Device\\Floppy" }; static const char* volume_start = "\\\\?\\"; CheckDriveIndex(DriveIndex); for (i=0; hDrive == INVALID_HANDLE_VALUE; i++) { if (i == 0) { hVolume = FindFirstVolumeA(volume_name, sizeof(volume_name)); if (hVolume == INVALID_HANDLE_VALUE) { suprintf("Could not access first GUID volume: %s\n", WindowsErrorString()); goto out; } } else { if (!FindNextVolumeA(hVolume, volume_name, sizeof(volume_name))) { if (GetLastError() != ERROR_NO_MORE_FILES) { suprintf("Could not access next GUID volume: %s\n", WindowsErrorString()); } goto out; } } // Sanity checks len = safe_strlen(volume_name); if ((len <= 1) || (safe_strnicmp(volume_name, volume_start, 4) != 0) || (volume_name[len-1] != '\\')) { suprintf("'%s' is not a GUID volume name\n", volume_name); continue; } drive_type = GetDriveTypeA(volume_name); if ((drive_type != DRIVE_REMOVABLE) && (drive_type != DRIVE_FIXED)) continue; volume_name[len-1] = 0; if (QueryDosDeviceA(&volume_name[4], path, sizeof(path)) == 0) { suprintf("Failed to get device path for GUID volume '%s': %s\n", volume_name, WindowsErrorString()); continue; } for (j=0; (j<ARRAYSIZE(ignore_device)) && (_strnicmp(path, ignore_device[j], safe_strlen(ignore_device[j])) != 0); j++); if (j < ARRAYSIZE(ignore_device)) { suprintf("Skipping GUID volume for '%s'\n", path); continue; } // If we can't have FILE_SHARE_WRITE, forget it hDrive = CreateFileA(volume_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hDrive == INVALID_HANDLE_VALUE) { suprintf("Could not open GUID volume '%s': %s\n", volume_name, WindowsErrorString()); continue; } if ((!DeviceIoControl(hDrive, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0, &DiskExtents, sizeof(DiskExtents), &size, NULL)) || (size <= 0)) { suprintf("Could not get Disk Extents: %s\n", WindowsErrorString()); safe_closehandle(hDrive); continue; } safe_closehandle(hDrive); if ((DiskExtents.NumberOfDiskExtents >= 1) && (DiskExtents.Extents[0].DiskNumber == DriveIndex)) { if (bKeepTrailingBackslash) volume_name[len-1] = '\\'; success = TRUE; break; } } out: if (hVolume != INVALID_HANDLE_VALUE) FindVolumeClose(hVolume); return (success)?safe_strdup(volume_name):NULL; }