Home

Drives

 

Introduction to Drives

 

Description

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 USB Flash Drive
Hard Drive DVD Drive USB Flash Drive
     
Floppy Drive   Memory Card
Floppy 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 each part that acts as its own 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 "\". 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 completely. 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.

Practical LearningPractical Learning: Introducing Drives

  1. Start Embarcadero RAD Studio
  2. To create a new project, on the main menu, click File -> New -> VCL Forms Application - C++Builder
  3. Change the Caption of the form to File Viewer
  4. On the main menu, click File -> New -> Other
  5. In the right list of the New Items dialog box, click Frame and click OK
  6. In the top section of the Code Edirtor, click Unit1.cpp
  7. In the Tool Palette, click Standard
  8. Click Frame and click the form
  9. In the Select Frame to Insert dialog box, select the name of the frame (it should be selected already) and click OK
  10. In the top section of the Code Editor, click Unit2.cpp

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 VCL provides a structure named TDirectory. The TDirectory structure is defined in the IOUtils.hpp header file.

To get a list of drives on a computer, the TDirectory structure is equippped wit a method named GetLogicalDrives. Its syntax is:

static System::DynamicArray<System::UnicodeString>
              __fastcall GetLogicalDrives(void);

When this method is called, it produces a list of all drives on the current computer. To get the same information, you can call the GetLogicalDrives() function of Microsoft Window. Its syntax is:

DWORD GetLogicalDrives(void);

Practical LearningPractical Learning: Getting a List of Drives

  1. From the Standard section of the Tool Palette, click TButton TButton and click the frame
  2. In the Object Inspector, change its properties as follows:
    Caption: Drives
    Name: btnDrives
  3. In the frame, double-click the button and implement its event as follows:
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #include <IOUtils.hpp>
    #pragma hdrstop
    
    #include "Unit2.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TFrame2 *Frame2;
    //---------------------------------------------------------------------------
    __fastcall TFrame2::TFrame2(TComponent* Owner)
    	: TFrame(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TFrame2::btnDrivesClick(TObject *Sender)
    {
    	System::DynamicArray<System::UnicodeString> lstDrives =
    		TDirectory::GetLogicalDrives();
    
    	for(int i = 0; i < lstDrives.Length; i++)
    		ShowMessage(lstDrives[i]);
    }
    //---------------------------------------------------------------------------
  4. Press F9 to execute
  5. Click the button
  6. Close the form and return to your programming environment

Characteristics of Drives

 

Introduction

If you want to get the name of a drive, you can first call the TDirectory::GetLogicalDrives and use the index of the drive you want to access. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnDrivesClick(TObject *Sender)
{
	System::DynamicArray lstDrives =
			TDirectory::GetLogicalDrives();

	UnicodeString strHardDrive = lstDrives[0];
	ShowMessage(strHardDrive);
}
//---------------------------------------------------------------------------

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 witha name or letter exists, this function analyzes if 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

Practical LearningPractical Learning: Identifying the Drives

  1. Change the code of the event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TFrame2::btnDrivesClick(TObject *Sender)
    {
        System::DynamicArray<System::UnicodeString> lstDrives =
    		TDirectory::GetLogicalDrives();
    
        for(int i = 0; i < lstDrives.Length; i++)
        {
    	AnsiString strDrive = lstDrives[i];
    
    	UINT drvType = GetDriveType(strDrive.c_str());
    
    	if( drvType == DRIVE_NO_ROOT_DIR )
    		ShowMessage(L"Drive Name: " + lstDrives[i] + L"\n"
    			L"The root path is invalid; for example, "
    			L"there is no volume is mounted at the path");
    	else if( drvType == DRIVE_REMOVABLE )
    		ShowMessage(L"Drive Name: " + lstDrives[i] + "\n"
    			L"The drive has removable media; for example, "
    			L"a floppy drive or flash card reader");
    	else if( drvType == DRIVE_FIXED )
    		ShowMessage(L"Drive Name: " + lstDrives[i] + "\n"
    			L"The drive has fixed media; for example, a hard "
    			L"drive, flash drive, or thumb drive");
    	else if( drvType == DRIVE_REMOTE )
    		ShowMessage(L"Drive Name: " + lstDrives[i] + "\n"
    			L"The drive is a remote (network) drive");
    	else if( drvType == DRIVE_CDROM )
    	    ShowMessage(L"Drive Name: " + lstDrives[i] + "\n"
    			L"The drive is a CD-ROM drive or a DVD drive");
    	else if( drvType == DRIVE_RAMDISK )
    	    ShowMessage(L"Drive Name: " + lstDrives[i] + "\n"
    			L"The drive is a RAM disk");
    	else //if( drvType == DRIVE_UNKNOWN )
    	    ShowMessage(L"The drive type cannot be determined");
        }
    }
    //---------------------------------------------------------------------------
  2. Press F9 to execute
  3. Click the button
  4. Close the form and return to your programming environment
  5. In the top section of the Code Editor, click Unit1.cpp
  6. On the form, click the frame
  7. Press Delete to remove it
  8. On the main menu, click File -> New -> Other
  9. In the right list of the New Items dialog box, double-click Frame
  10. Notice the name on the tab of the new frame
  11. In the top section of the Code Edirtor, click Unit1.cpp
  12. In the Standard section of the Tool Palette, click Frame and click the form
  13. In the Select Frame to Insert dialog box, select the name of the new frame (it should be the last in the list) and click OK
  14. In the top section of the Code Editor, click the UnitX.cpp of the last frame
 
 
 
 

Home Copyright © 2010-2016, FunctionX