Advertisement
ASP_Volume3 Complete Applications #65894

Open USB drives from desktop

This article demonstrates how to create a simple program which can open USB drives connected to the computer from the desktop.

AI

AI Samenvatting: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Broncode
original-source
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.
Originele reacties (3)
Hersteld van de Wayback Machine