Home

Microsoft Visual C++/MFC File Processing: Drives

       

Introduction to Drives

A drive is a physical device attached to a computer so it can store information. A drive can be a hard disk, a CD ROM, a DVD ROM, a flash (USB) drive, a memory card etc:

   
Hard Drive DVD Drive
Hard Drive DVD Drive
    
USB Flash Drive Memory Card
USB Flash Drive Flash Memory

A drive can reside inside a computer. That's the case for internal hard drives and most CD or DVD drives. A drive can also reside outside. That's the case for most flash (USB) drives. There are also versions of external hard drives and external DVD drives:

Hard Drive Floppy Drive USB Flash Drive
External Hard Drive External Floppy Drive USB Flash Drive Holder

A drive is referred to as virtual if it is not a real physical object. For example, a hard drive can be divided or partitioned internally, giving birth to parts that each acts as a virtual drive.

While most drives are connected to a computer, a device connected to another computer can also be used as a drive. In this case, while the drive is connected to a computer A, a computer B must be connected to the computer A in order to use the drive on computer A. This is the case in computer networks where drives (or their contents) are shared.

Not all computers have the same drives and not all computers deal with the same means of storing data. Still, to simplify their identification, all objects used to hold data are referred to as drives. Because there are different ways to consider drives, there are also various means of accessing them.

There are two techniques of referring to drives. A drive that is directly connected to a computer, whether internally or externally, is referred to as a local drive. In Microsoft Windows, a local drive is represented by a letter, in uppercase, followed by a colon ":", and a backslash "\" (sometimes the backslash is omited). Traditionally, drive A: is used for a 3.5" floppy drive that uses 3.5" floppy disks. Most computers nowadays don't (or hardly) use floppy disks. That drive is almost never used but, because of legacy, it is still represented in the Microsoft Windows operating system. Also, traditionally, drive B:\ was used for a 5.25" floppy drive that used 5.25" floppy disks. These disks have almost disappeared. Based on operating system legacy, in some computers, drive B:\ is still represented (many computers don't show any drive B:\ anymore). Drive C:\ usually represents the main hard drive of a computer. The other letters assigned to the other drive are not standard; they vary from one computer to another. If a hard disk is partitioned, each partition uses its own letter and is represented as its own drive.

Getting the List of Drives of a Computer

Normally, you will hardly be concerned with the creation of drives. The operating system "creates" or assigns a drive whenever it juges it necessary. For example, as soon as you connect a USB drive to a port, the operating system automatically creates a drive and assigns a lette to it. You will only need to identify the drives that are available on a computer on which  your application is running. One of the primary operations you will want to perform is to get a list of drives on the computer.

To support drives on a computer, the Win32 library provides the GetLogicalDrives() function of Microsoft Window. Its syntax is:

DWORD WINAPI GetLogicalDrives(void);

When this method is called, it produces a list of all drives on the current computer. Each drive is represented by a bit in the integral returned value. Here is an example of calling this function:

void CLogicalDrives1Dlg::OnBnClickedLogicaldrivesBtn()
{
	wchar_t drive[512] = L"A:";
	unsigned int drives = GetLogicalDrives();
	CString strListOfDrives = L"The list of drives is ";

	if( drives == 0 )
		AfxMessageBox(L"There is no drive to show");
	else
	{
		while(drives)
		{
			if( drives & 1 )
			{
				strListOfDrives += drive;
				strListOfDrives += L", ";
			}

			drive[0]++;
			drives >>= 1;
		}

		m_ListOfDrives = strListOfDrives;
		UpdateData(FALSE);
	}
}

Here is an result from the computer where this program was run:

Drives

To get a list of drives and store that list in a string, you can call the GetLogicalDriveStrings() function of the Win32 library. Its syntax is:

DWORD WINAPI GetLogicalDriveStrings(__in  DWORD nBufferLength,
  				    __out LPTSTR lpBuffer);

The Type of a Drive

A drive is primarily recognized by its category. Examples of categories are hard drives, CD and DVD drives, etc. In Microsoft Windows, to get the categories of drives, you can call the GetDriveType() function. The GetDriveType() function is defined in the Kernel32.lib that is a member of the Window.h header file. This means that you don't have to import or include any library to use it.

The syntax of the GetDriveType() function is:

UINT GetDriveType(LPCTSTR lpRootPathName);

This function receives a drive name as argument. If a drive with a name or letter exists, this function analyzes it and produces an unsigned constant value that can be one of the following:

Member Description
DRIVE_UNKNOWN The drive is unrecognizable
DRIVE_NO_ROOT_DIR The root of the drive is unrecognizable
DRIVE_REMOVABLE This can be a floppy drive, a USB drive, a memory card, etc. A drive that can be removed at will
DRIVE_FIXED This is a hard drive or a partition on an HD
DRIVE_REMOTE This is a network drive, usually located on another computer
DRIVE_CDROM This is drive CD or DVD drive
DRIVE_RAMDISK This is the random access memory

Here is an example:

void CLogicalDrives1Dlg::OnBnClickedLogicaldrivesBtn()
{
    wchar_t drive[] = L"A:";
    wchar_t drive2[20];
    unsigned int drives = GetLogicalDrives();
    CString strListOfDrives = L"The list of drives is ";

    if( drives == 0 )
	AfxMessageBox(L"There is no drive");
    else
    {
	while(drives)
	{
	    if( drives & 1 )
	    {
		wcscpy(drive2, drive);
		wcscat_s(drive2, L"\\");

		if( GetDriveType(drive2) == DRIVE_UNKNOWN )
			AfxMessageBox(L"Unknown Drive Type");
		else if( GetDriveType(drive2) == DRIVE_REMOVABLE)
			AfxMessageBox(L"Removable Drive");
		else if( GetDriveType(drive2) == DRIVE_FIXED)
		   AfxMessageBox(L"Fixed Media Drive (hard disk, flash drive, etc)");
		else if( GetDriveType(drive2) == DRIVE_REMOTE)
			AfxMessageBox(L"Remote or Network Drive");
		else if( GetDriveType(drive2) == DRIVE_CDROM)
			AfxMessageBox(L"CD or DVD Drive");
		else if( GetDriveType(drive2) == DRIVE_RAMDISK)
			AfxMessageBox(L"Random Access Memory Drive");
		else // if( GetDriveType(drive2) == DRIVE_NO_ROOT_DIR )
			AfxMessageBox(L"Invalid Root Path");
	    }

            drive[0]++;
	    drives >>= 1;
	}

	UpdateData(FALSE);
    }
}
 
 
 
     
 

Home Copyright © 2010-2016, FunctionX