hi,
how to get drives letters which are available (not in use) in MFC using C++ ? Any code snippet..
hi,
how to get drives letters which are available (not in use) in MFC using C++ ? Any code snippet..
From Here :
This gives you the drives that are in use, just take them away from the rest of the alphabet A-Z
////////////////////////////////////////////////////////////////
// MSDN Magazine -- April 2002
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual C++ 6.0. Set tabsize = 3 in your editor.
// Runs in Windows XP and probably Windows 2000 too.
//
#include "stdafx.h"
#include "resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
using namespace std; // for string class
//////////////////
// This mini-table maps GetDriveType codes to human-readable string
//
struct {
UINT type; // return code from GetDriveType
LPCSTR name; // ascii name
} DriveTypeFlags [] = {
{ DRIVE_UNKNOWN, "Unknown" },
{ DRIVE_NO_ROOT_DIR, "Invalid path" },
{ DRIVE_REMOVABLE, "Removable" },
{ DRIVE_FIXED, "Fixed" },
{ DRIVE_REMOTE, "Network drive" },
{ DRIVE_CDROM, "CD-ROM" },
{ DRIVE_RAMDISK, "RAM disk" },
{ 0, NULL},
};
//////////////////
// Standard tmain for MFC ListDrives app
//
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
cerr << _T("Fatal Error: MFC initialization failed") << endl;
return -1;
}
// Get logical drive strings-- a:\b:\c:\... etc.
// Could also use GetLogicalDrives to get in the form of a bitmap instead
// of character string.
//
TCHAR buf[100];
DWORD len = GetLogicalDriveStrings(sizeof(buf)/sizeof(TCHAR),buf);
// Display information about each drive.
//
string msg = "Logical Drives:\n"; // STL string
for (TCHAR* s=buf; *s; s+=_tcslen(s)+1) {
LPCTSTR sDrivePath = s;
msg += sDrivePath;
msg += " ";
// GetDriveType gets one of the enum values DRIVE_UNKNOWN, etc.
//
UINT uDriveType = GetDriveType(sDrivePath);
// Find drive type in table. I do a table lookup here to be extra
// cautious, but since the uDriveType values are sequential, i could've
// used DriveTypeFlags[uDriveType] instead of linear lookup. In
// practice you would usually perform some check like
//
// if (uDriveType & DEVICE_CDROM) {
// ...
// }
//
for (int i=0; DriveTypeFlags[i].name; i++) {
if (uDriveType == DriveTypeFlags[i].type) {
msg += DriveTypeFlags[i].name;
break;
}
}
msg += '\n';
}
cout << msg.c_str();
return 0;
}
Your probably after GetLogicalDrives, this gives you a bit mask of all the drive letters in use by the system, it'll be up to you to convert these to letters and add them into the combo box
To make it a little more clear:
Return Value
If the function succeeds, the return value is a bitmask representing the currently available disk drives. Bit position 0 (the least-significant bit) is drive A, bit position 1 is drive B, bit position 2 is drive C, and so on.
so GetLogicalDrives() & 1
checks if drive A is present, GetLogicalDrives() & 4
checks if drives C is there
Call GetLogicalDrives()
. Bits set to zero correspond to unused drive letters (except of course bits 26-31)