The help you needed in C, C++, MFC
Complete tutorials with free source code
PDF Print E-mail

Simple program which can open the USB drives connected to computer

This program can open the USB drives connected to the computer by just running the executable. If there is only one USB drive connected, the program will open the USB drive. If there are more than one USB drives, it will give a screen like this, where you can select the USB drive you want to open.

Open USB drive screen

This utility is very useful to open the pen drives without infecting the viruses in the pen drive. You know if you just double click or right click and select open or explore, the viruses in the pen drive which have edited the autorun.inf will run and infect your computer. When you open the USB drive with this program, there is no such  risk.

Here is the main function.

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
driveselectdlg dlg;
CString driveletter;
POSITION pos;
LPWSTR lable=new WCHAR[50];
UINT drivetype;
DWORD logicaldrives, devider;
logicaldrives=GetLogicalDrives();
devider=8;

for(int i=0; i<10; i++)
{
driveletter=finddrive(devider);
devider*=2;
drivetype=GetDriveType(driveletter);
if(drivetype==DRIVE_REMOVABLE)
{
dlg.drivelist.AddTail(driveletter);
GetVolumeInformation(driveletter, lable, 50, NULL, NULL, NULL, NULL, 0);
dlg.lablelist.AddTail(lable);
}
}
if(dlg.lablelist.GetSize()==0)
{
::MessageBox(0, TEXT("No pen drive detected!"), TEXT("Message"), 0);
return 0;
}
if(dlg.lablelist.GetSize()==1)
{
pos=dlg.drivelist.GetHeadPosition();
ShellExecute(NULL,TEXT("open"), dlg.drivelist.GetNext(pos), NULL, NULL, SW_SHOWMAXIMIZED);
return 0;
}
if(dlg.DoModal()==IDOK)
{
pos=dlg.drivelist.GetHeadPosition();
for(int i=0; i<(dlg.selection-1); i++)
{
dlg.drivelist.GetNext(pos);}
ShellExecute(NULL,TEXT("open"), dlg.drivelist.GetNext(pos), NULL, NULL, SW_SHOWMAXIMIZED);
}
return 0;
}

The program first obtains a list of drives in the system by using the function GetLogicalDrives. Then, the drive type of each logical drive is obtained using GetDriveType and checked whether the drive is a removable drive. The drive letters of all found removable drives are stored in a CStringList.

Then, if there is only one removable drive, program just opens it using ShellExecute function. If there are multiple removable drives, program displays a dialog, from which the user can select the removable drive he or she want to open. Then, according to the users' chose, correct removable drive is opened using ShellExecute.

You can always download the source code from FrostCode.

Download the source code of the project