C++ and STL
  Common Issues
  Destructor
  regex
  string
 MFC
  CButton
  CDialog
  CEdit
  CInternetSession
  CWinThread
  CWnd
 MS VS FAQ
  Compiling, building
  Debugging
  Editor
  Settings
 Win32, API
  Console
  File System
  Graphics
  Internet functions
  Kernel objects
  Security
  Sound
  Thread, process
  Window
 Windows NT/2K*
  Logon, logoff...
  Networking
  Service Packs
 | About
  About
  Links & Freeware

Powered by
CoderTown



How to enumerate logical drives in the system?

To find out what drives are in the system and of what kind, you need three functions: GetLogicalDriveStrings() or GetLogicalDrives and GetDriveType(). I've borrowed the sample from MSDN Magazine:

 ////////////////////////////////////////////////////////////////
// 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;
}

Created: 2003-05-12
Updated: 2003-05-12

Google
 
Web visualcpp.net
msdn.microsoft.com codeguru.com