Remove the USB flash drive from the program

Source: Internet
Author: User

Note:

Cr = cm_request_device_eject (deviceinfodata. devinst, & pnpvietotype, vetoname, Len, 0); // No bubble prompt
Cr = cm_request_device_eject (deviceinfodata. devinst, & pnpvietotype, null, 0, 0); // a bubble prompt is displayed.

For functions such as cm_request_device_eject in DDK, you can use the following methods:
1. Include the path and library of the DDK header file, but the header file must contain
Extern "C"
{
# Include "hidsdi. H"
}
2. Copy the related files in DDK to the project.
3. Use getprocaddress to obtain the function from the relevant DLL for execution.

This method can only remove all USB flash disks and cannot remove the USB flash disks corresponding to a drive letter.

# Include
# Include
# Include

# Define dword_ptr DWORD
# Define ulong_ptr DWORD

Extern "C "{
# Include "hidsdi. H"
}
// Add hid. Lib

# Include
# Include
# Pragma comment (Lib, "setupapi. lib ")

# Include
# Include

# Include
# Pragma comment (Lib, "cfgmgr32.lib ")

# Include
// # Include
Define_guid (guid_devinterface_usb_device,
0xa5dcbf10l, 0x6530, 0x11d2, 0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed );
# Define guid_class_usb_device guid_devinterface_usb_device

Int main (INT argc, _ tchar * argv [])
{
Hdevinfo;

Sp_devinfo_data deviceinfodata;
Dword I;

//--------------------------------------------------------------------------
// Obtain device information
Hdevinfo = setupdigetclassdevs (lpguid) & guid_class_usb_device,
0, // enumerator
0,
Digcf_present | digcf_deviceinterface );
If (hdevinfo = invalid_handle_value)
{
// Information Query failed
Printf ("error-setupdigetclassdevs ()");
Return 1;
}
//--------------------------------------------------------------------------

// Enumerate each USB device
Deviceinfodata. cbsize = sizeof (sp_devinfo_data );
For (I = 0; setupdienumdeviceinfo (hdevinfo, I, & deviceinfodata); I ++)
{
Lptstr buffer = NULL;
Pvoid buffer2 = NULL;
DWORD buffersize = 0;
Ulong Len;
Configret CR;
Pnp_veto_type pnpvietotype;
Char vetoname [max_path];
Ulong ulstatus;
Ulong ulproblemnumber;

Cr = cm_get_devnode_status (& ulstatus,
& Ulproblemnumber,
Deviceinfodata. devinst,
0 );
If (cr_success = Cr ){
Printf ("OK-cm_get_devnode_status () [% d]/n", CR );
Printf ("OK-cm_get_devnode_status () Sts [% x]/n", ulstatus );
Printf ("OK-cm_get_devnode_status () Pro [% x]/n", ulproblemnumber );
} Else {
Printf ("error-cm_get_devnode_status () [% d]/n", CR );
Printf ("error-cm_get_devnode_status () [% d]/n", getlasterror ());
}
// Dn_disableable or dn_removable
If (dn_disableable & ulstatus )! = 0 ){
Printf ("has-dn_disableable () [% x]/n", dn_disableable & ulstatus );
} Else {
Continue;
}
If (dn_removable & ulstatus )! = 0 ){
Printf ("has-dn_removable () [% x]/n", dn_removable & ulstatus );
} Else {
Continue;
}

Len = max_path;
// Pnpvietotype = pnp_vetodevice;
Cr = cm_request_device_eject (
Deviceinfodata. devinst,
& Pnpvietotype,
Vetoname,
Len,
0
);
If (cr_success = Cr ){
Printf ("OK-cm_request_device_eject () [% d]/n", CR );
} Else {
Printf ("error-cm_request_device_eject () [% d]/n", CR );
Printf ("error-cm_request_device_eject () [% d]/n", getlasterror ());
}

}

If (getlasterror ()! = No_error &&
Getlasterror ()! = Error_no_more_items)
{
// Insert error handling here.
Return 1;
}

// Cleanup
Setupdidestroydeviceinfolist (hdevinfo );

Return 0;
}

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.