// 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; } |