article

Open USB drives from desktop

Email
Submitted on: 1/9/2015 11:23:00 PM
By: lahiru chandima (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: Microsoft Visual C++
Views: 2376
 
     This article demonstrates how to create a simple program which can open USB drives connected to the computer from the desktop.


 
				To open a USB drive connected to your Windows installed computer, you have to go to My Computer. But, with this simple program, you can do it from your desktop. Also,if there are multiple USB drives connected, it will give a window to choose the drive you need to open.
Also,if you double click a pen drive icon in My Computer, there is a risk that the viruses that may be in the pen drive will infect your computer by auto running. But, there is no such risk when you use this program.
Here is the main code
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;}
To view the full article and download the source code visit http://www.frostcode.info/example-projects/open-usb-drives.html.


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.