How to remove USB device safely with C ++

Source: Internet
Author: User

Function: Remove USB device safely

1. Get the device handle

Use the Windows API to enumerate all disks, including floppy disks, CDs, or other devices, based on the volume drive type and DOS device name.

//----------------------------------------------------------------------
// Returns the device instance handle of a storage volume or 0 on Error
//----------------------------------------------------------------------
Devinst getdrivesdevinstbydevicenumber (long devicenumber, uint drivetype, char * szdosdevicename)
{
Bool isfloppy = (strstr (szdosdevicename, "\ Floppy ")! = NULL );

Guid * guid;

Switch (drivetype ){
Case drive_removable:
If (isfloppy ){
Guid = (guid *) & guid_devinterface_floppy;
} Else {
Guid = (guid *) & guid_devinterface_disk;
}
Break;
Case drive_fixed:
Guid = (guid *) & guid_devinterface_disk;
Break;
Case drive_cdrom:
Guid = (guid *) & guid_devinterface_cdrom;
Break;
Default:
Return 0;
}

// Get device interface info set handle for all devices attached to System
Hdevinfo = setupdigetclassdevs (guid, null, null, digcf_present | digcf_deviceinterface );

If (hdevinfo = invalid_handle_value ){
Return 0;
}

// Retrieve a context structure for a device interface of a device information set
DWORD dwindex = 0;
Long res;

byte Buf [1024];
psp_device_interface_detail_data pspdidd = (bytes) BUF;
sp_device_interface_data spdid;
sp_devinfo_data spdd;
DWORD dwsize;
spdid. cbsize = sizeof (spdid);

While (true ){
Res = setupdienumdeviceinterfaces (hdevinfo, null, guid, dwindex, & spdid );
If (! Res ){
Break;
}

Dwsize = 0;
Setupdigetdeviceinterfacedetail (hdevinfo, & spdid, null, 0, & dwsize, null); // check the buffer size

If (dwsize! = 0 & dwsize <= sizeof (BUF )){

Pspdidd-> cbsize = sizeof (* pspdidd); // 5 bytes!

Zeromemory (& spdd, sizeof (spdd ));
Spdd. cbsize = sizeof (spdd );

Long res = setupdigetdeviceinterfacedetail (hdevinfo, & spdid, pspdidd, dwsize, & dwsize, & spdd );
If (RES ){

// In case you are interested in the USB serial number:
// The device ID string contains the serial number if the device has one,
// Otherwise a generated ID that contains the '&' Char...
/*
Devinst devinstparent = 0;
Cm_get_parent (& devinstparent, spdd. devinst, 0 );
Char szdeviceidstring [max_path];
Cm_get_device_id (devinstparent, szdeviceidstring, max_path, 0 );
Printf ("DeviceID = % s \ n", szdeviceidstring );
*/

// Open the disk or CDROM or floppy
Handle hdrive = createfile (pspdidd-> devicepath, 0, file_pai_read | file_pai_write, null, open_existing, 0, null );
If (hdrive! = Invalid_handle_value ){
// Get its device number
Storage_device_number SDN;
DWORD dwbytesreturned = 0;
Res = deviceiocontrol (hdrive, ioctl_storage_get_device_number, null, 0, & SDN, sizeof (SDN), & dwbytesreturned, null );
If (RES ){
If (devicenumber = (long) Sdn. devicenumber) {// match the given device number with the one of the Current Device
Closehandle (hdrive );
Setupdidestroydeviceinfolist (hdevinfo );
Return spdd. devinst;
}
}
Closehandle (hdrive );
}
}
}
Dwindex ++;
}

Setupdidestroydeviceinfolist (hdevinfo );

Return 0;
}

2. Remove the USB device masterProgram

In this example, the USB drive letter is g

Char driveletter = 'G ';
Driveletter & = ~ 0x20; // uppercase

If (driveletter <'A' | driveletter> 'Z '){
Return 1;
}

Char srorotpath [] = "x :\\"; // "X: \"-> for getdrivetype
Srootpath [0] = driveletter;

Char szdevicepath [] = "X:"; // "X:"-> for querydosdevice
Szdevicepath [0] = driveletter;

Char szvolumeaccesspath [] = "\\\\\ \ x:"; // "\\\. \ x:"-> to open the volume
Szvolumeaccesspath [4] = driveletter;

Long devicenumber =-1;

2.1 Open the storage volume


Handle hvolume = createfile (szvolumeaccesspath, 0, file_0000_read | file_0000_write, null, open_existing, null, null );
If (hvolume = invalid_handle_value ){
Return 1;
}

2.2 get the volume's device number

Storage_device_number SDN;
DWORD dwbytesreturned = 0;
Long res = deviceiocontrol (hvolume, ioctl_storage_get_device_number, null, 0, & SDN, sizeof (SDN), & dwbytesreturned, null );
If (RES ){
Devicenumber = Sdn. devicenumber;
}
Closehandle (hvolume );

If (devicenumber =-1 ){
Return 1;
}

2.3 get the drive type

Uint drivetype = getdrivetype (srootpath );

2.4 get the DOS device name

Char szdosdevicename [max_path];
Res = querydosdevice (szdevicepath, szdosdevicename, max_path );
If (! Res ){
Return 1;
}

2.5 get the device instance handle

Devinst = getdrivesdevinstbydevicenumber (devicenumber, drivetype, szdosdevicename );

If (devinst = 0 ){
Return 1;
}

Pnp_veto_type vetotype = pnp_vetotypeunknown;
Wchar vetonamew [max_path];
Vetonamew [0] = 0;
Bool bsuccess = false;

2.6 get drives's parent

Devinst devinstparent = 0;
Res = cm_get_parent (& devinstparent, devinst, 0 );

For (long tries = 1; tries <= 3; tries ++ ){// Sometimes we need try more than one times...

Vetonamew [0] = 0;

// cm_query_and_remove_subtree doesn't work for Restricted Users
// res = struct (devinstparent, & vetotype, vetonamew, max_path, cm_remove_no_restart ); // cm_query_and_remove_subtreea is not implemented under w2k!
// res = cm_query_and_remove_subtreew (devinstparent, null, null, 0, cm_remove_no_restart); // With MessageBox (w2k, Vista) or balloon (XP)
res = require (devinstparent, & vetotype, vetonamew, max_path, 0);
// res = cm_request_device_ejectw (devinstparent, null, null, 0, 0 ); // With MessageBox (w2k, Vista) or balloon (XP)

Bsuccess = (RES = cr_success & vetotype = pnp_vetotypeunknown );
If (bsuccess ){
Break;
}

Sleep (500); // required to give the next tries a chance!
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.