I am creating a simple GUI using Visual C++ MFC. My IDE is Visual Studios 2005. The GUI has a BUTTON with the title "Detect" and an EDIT CONTROL to display text. When a user clicks on the Detect button, the program scans through all the devices on your computer to retrieve ONLY the external USB Hard Drives. Please do take note that the HARD DRIVES are ALREADY PLUGGED IN. I am not interested in detecting hard drives upon insertion. The reason for this is that in my main app, I don't want the user to have access to the other drives other than the USB drive. The drive letter is then displayed in the EDIT CONTROL.
I have most of the program up and running. The only problem right now is that the drive letter is not being displayed. I used a GetDriveType() function to obtain the type. This works perfectly. My current external USB hard disk is in E:. All USB hard drives are detected as DRIVE_FIXED. When the iteration of 'E' arrives, it enters DRIVE_FIXED, which is good. However when I try to open a the drive using CreateFile() function, the returned output (hDevice) is always an INVALID_HANDLE_VALUE and hence the rest of the code is not executed.
I read up the MSDN on GetDriveType() and CreateFile(). GetDriveType() requires a trailing backslash for drive names but CreateFile() does not require one. I am a bit confused what a trailing backslash is. Then again there is the format of the drive string. For GetDriveType "E:\" seems to be fine. But for CreateFile(), as quoted from MSDN, " lpFileName string should be the following form: \\.\X: " . What does this mean? What is the X? How do we replace the X? Can't we just use the same E:\ ?
During debugging, the value of my hDevice turns to 0xffffffff when the C drive is encountered. However, even after I close the handle, when E drive is encountered, hDevice is still 0xffffffff. I assume this is an error becuase the next if statement block is never executed (0xffffffff is equal to INVALID_HANDLE_VALUE).
Can someone please help me with this? I really need this to be working. If you have any questions, please do not hesitate to ask. I really want the drive letter to be displayed.
head = GetLogicalDrives();
for ( cDrive = 'A'; cDrive <= 'Z'; cDrive++,head>>=1 )
if ( !(head&1) )
_stprintf(szDriveRoot, _T("%c:\\"), cDrive);
_stprintf(szDriveCreate, _T("\\.\%c:"), cDrive);
switch ( GetDriveType( szDriveRoot ) )
case 0: // The drive type cannot be determined.
case 1: // The root directory does not exist.
drivetype = DRVUNKNOWN;
case DRIVE_REMOVABLE: // The drive can be removed from the drive.
drivetype = DRVREMOVE;
case DRIVE_CDROM: // The drive is a CD-ROM drive.
drivetype = DRVCD;
case DRIVE_FIXED: // The disk cannot be removed from the drive.
drivetype = DRVFIXED;
hDevice = CreateFile(szDriveCreate,
FILE_SHARE_READ | FILE_SHARE_WRITE,
if (hDevice != INVALID_HANDLE_VALUE)
pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)new BYTE[sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1];
pDevDesc->Size = sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1;
if(pDevDesc->BusType == BusTypeUsb)
wsprintf(tempString, L"USB disk(s) are: \n%s",
case DRIVE_REMOTE: // The drive is a remote (network) drive.
drivetype = DRVREMOTE;
case DRIVE_RAMDISK: // The drive is a RAM disk.
drivetype = DRVRAM;