Hardware Device monitoring and processing 3-(disable and enable USB devices)

Source: Internet
Author: User

Two methods,

Method 1: Find the Registry

 

 

Ideas:First, enumerate the subkeys in the Registry path "HKEY_LOCAL_MACHINE // drivers // active, check whether the value of the key item "key" contains the "drivers // USB // clientdrivers // mass_storage_class" field. If it contains the value of the key item "name, obtain the devname and call the following code to disable it.

 

Handle HND = openstore (devname); // (L "dsk1 :")

If (HND = NULL)

{

: MessageBox (null, l "failed to Enable USB", l "notice", mb_ OK );

Return;

}

Bool ret = dismountstore (HND );

Closehandle (HND );

 

Process:

In the start dialog box, call the subkey under the enumactivedir enumeration registry key "HKEY_LOCAL_MACHINE // drivers // active", and then call the searchanddismountusbstro (preg_item_name change_itemstruct) function to dismount the USB storage device.

The callback function of the response window is windowproc. When the wm_devicechange message is received, the system checks whether the USB storage device is connected or removed. The prompt dialog box is displayed, where you can choose to dismount or not process the device.

If enabled, the device is re-inserted.

Related functions:

Searchanddismountusbstro

# Include "storemgr. H "<br/> void searchanddismountusbstro (preg_item_name change_itemstruct) // find and delete. <br/>{< br/> preg_item_name tempchange_itemstruct = change_itemstruct; <br/> while (tempchange_itemstruct) <br/>{< br/> char szkeyname [10] = {0}; <br/> strcpy (szkeyname, tempchange_itemstruct-> szitemname ); <br/> zeromemory (& szkeyname [strlen (szkeyname) + 1], 10-strlen (szkeyname)-1 ); <br/> tchar szusbkey [max_path] = text ("drivers // active //"); <br/> wcscat (szusbkey, short2wide (szkeyname )); <br/> hkey husbkey; <br/> If (error_success = regopenkeyex (HKEY_LOCAL_MACHINE, szusbkey, & husbkey) <br/>{< br/> DWORD keytype; <br/> DWORD dwlength = 100; <br/> tchar Buf [max_path] = {0}; <br/> If (error_success = regqueryvalueex (husbkey, text ("key"), 0, & keytype, (byte *) BUF, & dwlength) <br/> If (strstr (wide2short (BUF ), "drivers // USB // clientdrivers // mass_storage_class") // determines whether a USB storage device is used. <br/>{< br/> tchar devname [max_path] = {0 }; <br/> If (error_success = regqueryvalueex (husbkey, text ("name"), 0, & keytype, (byte *) devname, & dwlength )) <br/> {<br/> handle HND = openstore (devname); // (L "dsk1:") <br/> If (HND = NULL) <br/>{< br/>: MessageBox (null, l "failed to Enable USB", l "notice", mb_ OK); <br/> return; <br/>}< br/> bool ret = dismountstore (HND); <br/> closehandle (HND ); <br/>}< br/> regclosekey (husbkey); <br/> tempchange_itemstruct = tempchange_itemstruct-> pnext; <br/>}< br/>} 

Enumactivedir

Typedef struct _ reg_item_name <br/>{< br/> int num; // No. <br/> char szitemname [max_path]; // key value content <br/> _ reg_item_name * pnext; <br/>} reg_item_name, * preg_item_name; <br/> # define regitemlen sizeof (struct _ reg_item_name) </P> <p> preg_item_name enumactivedir () <br/> {<br/> preg_item_name P, last, head; <br/> hkey; <br/> long lresult; <br/> tchar szkeyname [64]; <br/> If (error_success = regopenkeyex (hkey _ LOCAL_MACHINE, text ("drivers // active"), & hkey) <br/>{< br/> int COUNT = 0; <br/> head = last = NULL; <br/> P = (preg_item_name) malloc (regitemlen ); <br/> DO <br/> {<br/> DWORD dwkeynamelength = max_path; <br/> lresult = regenumkeyex (hkey, Count, szkeyname, & dwkeynamelength, 0, null, null, null); <br/> If (lresult! = Error_success) <br/>{< br/> break; <br/>}< br/> P-> num = count; <br/> strcpy (p-> szitemname, wide2short (szkeyname); <br/> P-> pnext = NULL; <br/> ++ count; <br/> If (COUNT = 1) <br/> head = P; <br/> else <br/> last-> pnext = P; <br/> last = P; <br/> P = (preg_item_name) malloc (regitemlen); <br/>} while (lresult! = Error_no_more_items); <br/> regclosekey (hkey); <br/> free (p); <br/> return head; <br/>}< br/> return NULL; <br/>} 

Windowproc

<Br/> lresult cdlgusbctrldlg: windowproc (uint message, wparam, lparam) <br/>{< br/> If (Message = wm_devicechange) <br/>{< br/> If (dbt_devicearrival = wparam | dbt_deviceremovecomplete = wparam) <br/>{< br/> pdev_broadcast_hdr phdr = (pdev_broadcast_hdr) lparam; <br/> switch (phdr-> dbch_devicetype) <br/> {<br/> case dbt_devtyp_port: <br/> If (dbt_devicearrival = wparam) <br/>{< br/> change_itemstruct = enumactivedir (); <br/> If (: MessageBox (null, l "there are new USB devices, disable ", L "notice", mb_okcancel) = idok) <br/> searchanddismountusbstro (change_itemstruct ); <br/> else <br/> MessageBox (L "User selected to run this USB storage device to join "); <br/>}< br/> else if (dbt_deviceremovecomplete = wparam) <br/>{< br/> // count_afterchange = enumactivedir (); <br/> // MessageBox (L "Remove USB storage device"); <br/>}< br/> break; <br/> default: <br/> break; <br/>}< br/> return cdialog: windowproc (message, wparam, lparam ); <br/>}< br/> 

 

Method 2 storage device processing

 

 

Ideas:

Disable:

USB storage device. If the storage device is turned on, it cannot be used by others to disable it, and "system // storagemanager // profiles // usbhdprofile "; in the write key mounthidden = 1, dismount the SD card

Enable:

Write the key entry mounthidden = 0 in "system // storagemanager // profiles // sdmemory"; and The mountsd card

Re-insert SD card to enable

 

 

Disable USB storage devices

Void disableusbstorage () <br/>{< br/> storeinfo; <br/> memset (& storeinfo, 0, sizeof (storeinfo )); // remember to initialize after definition, otherwise no storage will be found later <br/> storeinfo. cbsize = sizeof (storeinfo); <br/> handle hdevice = findfirststore (& storeinfo); <br/> If (invalid_handle_value! = Hdevice) <br/> {<br/> DO <br/> {<br/> // determine whether the device is a USB storage device. <br/> If (0 = wcscmp (storeinfo. szstorename, text ("USB hard disk drive") <br/>{< br/> // if the specified storage device is found, print the information to the debug serial port <br/> wchar_t * devname = storeinfo. szdevicename; <br/> handle HND = openstore (devname); // (L "dsk1:") <br/> If (HND = NULL) <br/>{< br/> //: MessageBox (null, l "failed to Enable USB", l "notice", mb_ OK ); <br/> const char * notice = "failed to Enable USB! "; <Br/> log (Notice, strlen (Notice); <br/> return; <br/>}< br/> bool ret = dismountstore (HND ); <br/> closehandle (HND); <br/> break; <br/>}< br/>}while (findnextstore (hdevice, & storeinfo )); <br/>}< br/> closehandle (hdevice); <br/> DWORD dwdata = 1; <br/> wchar_t * skeyname = l "system // storagemanager // profiles // usbhdprofile"; <br/> If (writereg (dwdata, skeyname )) <br/>{< br/> const char * notice = "Disable Usbstorage is successfully written to mounthidden = 1! "; <Br/> // log (Notice, strlen (Notice); <br/> const char * notice1 =" the USB storage device is disabled successfully! "; <Br/> log (notice1, strlen (notice1 )); <br/>}< br/> else <br/> {<br/> const char * notice = "disableusbstorage write mounthidden = 1 failed! "; <Br/> // log (Notice, strlen (Notice); <br/> const char * notice1 =" An error occurred while disabling the USB storage device! "; <Br/> log (notice1, strlen (notice1); <br/>}< br/>} 

Enable USB storage device

Void enableusbstorage () <br/>{< br/> DWORD dwdata = 0; <br/> wchar_t * skeyname = l "system // storagemanager // profiles // usbhdprofile "; <br/> If (writereg (dwdata, skeyname) <br/>{< br/> const char * notice = "enableusbstorage is successfully written to mounthidden = 0! "; <Br/> // log (Notice, strlen (Notice); <br/> const char * notice1 =" the USB storage device is enabled successfully! "; <Br/> log (notice1, strlen (notice1 )); <br/>}< br/> else <br/> {<br/> const char * notice = "enableusbstorage writing mounthidden = 0 failed! "; <Br/> // log (Notice, strlen (Notice); <br/> const char * notice1 =" failed to Enable USB storage device! "; <Br/> log (notice1, strlen (notice1); <br/>}< br/>} 

 

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.