Enable and disable optical drive, soft drive, and USB drive with VC

Source: Internet
Author: User
Because of the actual needs of the work, you need to enable and disable the optical drive, soft drive, and USB for control. Refer to the Code provided by you to disable the NIC to enable and disable the optical drive, soft drive, and USB. The main implementation code is as follows:

// Necessary header files and Lib files to be linked
# Include <setupapi. h>
# Include <shlwapi. h>
# Pragma comment (Lib, "setupapi. lib ")
# Pragma comment (Lib, "shlwapi. lib ")
// Device information set (I have translated it into a device information set)
Hdevinfo = NULL;

// Error message
Void formatmsg (DWORD dwerror, lptstr * lpszmsg)
{
Bool Bok = formatmessage (format_message_from_system | format_message_allocate_buffer, null, dwerror,
Makelangid (lang_chinese, sublang_chinese_simplified), (lptstr) lpszmsg, 0, null );

If (! Bok)
{
Hmodule hdll = LoadLibraryEx (_ T ("netmsg. dll "),
Null,
Dont_resolve_dll_references );
If (null! = Hdll)
{
Formatmessage (format_message_from_hmodule |
Format_message_from_system,
Hdll,
Dwerror,
Makelangid (lang_chinese, sublang_chinese_simplified ),
(Lptstr) lpszmsg, 0, null );
Freelibrary (hdll );
}
}
}

Bool changestatus (DWORD newstatus, DWORD selecteditem, hdevinfo)
{
Lptstr lpszmsg = NULL;
Hcursor = NULL;
Try
{
Sp_propchange_params propchangeparams = {sizeof (sp_classinstall_header )};
Sp_devinfo_data deviceinfodata = {sizeof (sp_devinfo_data )};

Hcursor = setcursor (loadcursor (null, idc_wait ));

// Get a handle to the selected item.
If (! Setupdienumdeviceinfo (hdevinfo, selecteditem, & deviceinfodata ))
{
Formatmsg (getlasterror (), & lpszmsg );
Throw lpszmsg;
}

// Set the propchangeparams structure.
Propchangeparams. classinstallheader. installfunction = dif_propertychange;
Propchangeparams. Scope = dics_flag_global;
Propchangeparams. statechange = newstatus;

If (! Setupdisetclassinstallparams (hdevinfo, & deviceinfodata, (sp_classinstall_header *) & propchangeparams,
Sizeof (propchangeparams )))
{
Formatmsg (getlasterror (), & lpszmsg );
Throw lpszmsg;
}

// Call the classinstaller and perform the change.
If (! Setupdicallclassinstaller (dif_propertychange, hdevinfo, & deviceinfodata ))
{
Formatmsg (getlasterror (), & lpszmsg );
Throw lpszmsg;
}

Setcursor (hcursor );
Return true;
}
Catch (tchar * pszerror)
{
Setcursor (hcursor );
: MessageBox (null, pszerror, _ T ("prompt"), mb_ OK );
If (null! = Lpszmsg)
{
Localfree (hlocal) lpszmsg );
}
Return false;
}
}

// This function is mainly used to enable and disable these devices.
// Parameter description, nstatus can have three values,-1: Nothing
// 0 disable the device, 1 enable the device.
// Nindex is used to control whether to enable or disable the optical drive or USB drive.
// Three values are also recommended. 0 indicates the soft drive, and 1 indicates the optical drive.
// 2 stands for USB
// You can call this function to disable the optical drive.
// Controldisk (0, 1 );
Bool controldisk (INT nstatus, int nindex)
{
If (-1 = nstatus)
{
Return false;
}

Lptstr lpszmsg = NULL;
Try
{
Tchar * guidstring = NULL;
Guid;
Zeromemory (& guid, sizeof (guid ));
Switch (nindex)
{
Case 0: // 0 indicates a soft drive.
Guidstring = _ T ("4d36e980-e325-11ce-bfc1-08002be10318 ");
Uuidfromstring (unsigned char *) guidstring, & guid );
Break;
Case 1: // 1 represents the optical drive
Guidstring = _ T ("4d36e965-e325-11ce-bfc1-08002be10318 ");
Uuidfromstring (unsigned char *) guidstring, & guid );
Break;
Case 2: // 2 stands for USB
Guidstring = _ T ("36fc9e60-c465-11cf-8056-444553540000 ");
Uuidfromstring (unsigned char *) guidstring, & guid );
Break;
}

Hdevinfo = setupdigetclassdevs (& guid, null, null, digcf_present );
If (invalid_handle_value = hdevinfo)
{
Formatmsg (getlasterror (), & lpszmsg );
Throw lpszmsg;
}

Dword I;
Sp_devinfo_data deviceinfodata;
Zeromemory (& deviceinfodata, sizeof (sp_devinfo_data ));
Deviceinfodata. cbsize = sizeof (sp_devinfo_data );

For (I = 0; setupdienumdeviceinfo (hdevinfo, I, & deviceinfodata); ++ I)
{
If (1 = nstatus)
{
Statechange (dics_enable, I, hdevinfo );
}
Else if (0 = nstatus)
{
Statechange (dics_disable, I, hdevinfo );
}
}

// Release device information set
Return setupdidestroydeviceinfolist (hdevinfo );
}
Catch (tchar * pszerror)
{
: MessageBox (null, pszerror, _ T ("prompt"), mb_ OK );
If (null! = Lpszmsg)
{
Localfree (hlocal) lpszmsg );
}
Return false;
}
Return false;
}

The test shows that there is no problem in disabling the Drive drive, the drive, and the USB. However, if you want to enable the drive, you must call controldisk (1, 2) twice ); this function is available, and the other two are enabled. I also want to talk about how to enumerate all the devices and then filter them (this is the method for disabling the NIC on the Internet ).

According to my observation, under the registry:

HKEY_LOCAL_MACHINE/system/CurrentControlSet/control/class/

There are many guids, and each guid represents a device. For example, this is a USB

{36fc9e60-c465-11cf-8056-444553540000}

This is CDROM's

{4d36e965-e325-11ce-bfc1-08002be10318}

Wait.

Even if you change these values to other guids, I can still use the original guid to implement my functions.

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.