Detects access and removal of USB and SD card storage devices

Source: Internet
Author: User

Method 1: dependency window

Method 2: start with the Registry

 

 

Method 1:

 

 

Because both USB storage devices and SD cards are storage devices,

When the device is connected, enumerate the sub-keys under the Registry path "HKEY_LOCAL_MACHINE // drivers // active". Find the items that match this value in self-check according to P-> dbcp_name, check whether the value of the key item "key" contains the "drivers // USB // clientdrivers // mass_storage_class" field, if the device contains an SD field, an SD card device is added.

However, this information cannot be found from the Registry when the device is removed. The only viable option is to store the registry key content, which is troublesome and must be implemented by relying on the window, some problems occur when writing DLL, so find the following method.

 

 

Method 2:

 

Ideas:

1. enumerate the subkeys in the Registry path "HKEY_LOCAL_MACHINE // drivers // active,

If the key value of the key item "key" contains "drivers // USB // clientdrivers // mass_storage_class", the device is a USB device;

If the key value of the key "key" includes "drivers // sdcard // clientdrivers // class", the device is an SD card device;

Required, if no SD card device value = 1; if no USB storage device value = 0.

2. during enumeration, the system inserts data from the head and reads data from the head of the linked list, reading the value of a key does not mean that the USB storage device or the SD card device is stopped.

Storage obtained and sum;

Old_itemnamelist = enumactivedir ();

Old_sum = count (old_itemnamelist );

3. The Metric registry item "HKEY_LOCAL_MACHINE, text // drivers // active". This change indicates that a device is connected or removed. When the change occurs, reenumerate and compare the structure before and after the change.

Cur_itemnamelist = enumactivedir (); // preg_item_name enumactivedir ()

Compare (old_itemnamelist, cur_itemnamelist); // The comparison is mainly to view the number of child items of two struct. If the old value is greater than the cur value, a device is removed. Otherwise, a device is added.

Determine the type of device access or removal based on the difference between old and cur.

 

 

The enumactivedir function is implemented as follows:

Typedef struct _ reg_item_name
{
Int num; // number
Char szitemname [max_path]; // key value content
_ Reg_item_name * pnext;
} Reg_item_name, * preg_item_name;
Preg_item_name enumactivedir ();
# Define regitemlen sizeof (struct _ reg_item_name)

Preg_item_name enumactivedir ()
{
Preg_item_name P, last, head;
Hkey;
Long lresult;
Tchar szkeyname [64];
If (error_success = regopenkeyex (HKEY_LOCAL_MACHINE, text ("drivers // active"), 0, & hkey ))
{
Int COUNT = 0;
Head = last = NULL;
P = (preg_item_name) malloc (regitemlen );
Do
{
DWORD dwkeynamelength = max_path;
Lresult = regenumkeyex (hkey, Count, szkeyname, & dwkeynamelength, 0, null, null );
If (lresult! = Error_success)
{
Break;
}
P-> num = count;
Strcpy (p-> szitemname, wide2short (szkeyname ));
P-> pnext = NULL;
++ Count;
If (COUNT = 1)
Head = P;
Else
Last-> pnext = P;
Last = P;
P = (preg_item_name) malloc (regitemlen );
} While (lresult! = Error_no_more_items );
Regclosekey (hkey );
Free (P );
Return head;
}
Return NULL;
}

The Compare function is implemented as follows:

Void compare (preg_item_name old_itemnamestruct, preg_item_name cur_itemnamestruct)
{
Preg_item_name old_struct = old_itemnamestruct;
Preg_item_name cur_struct = cur_itemnamestruct;
Int old_count = 0;
Int cur_count = 0;
While (old_struct)
{
Old_count ++;
Old_struct = old_struct-> pnext;
}
While (cur_struct)
{
Cur_count ++;
Cur_struct = cur_struct-> pnext;
}
// Restore pointer
Cur_struct = cur_itemnamestruct;
Old_struct = old_itemnamestruct;
If (old_count> cur_count) & (old_count-cur_count = 1) // There is a device to remove
{
// Take the top one of the old
New_sum = count (cur_struct );
If (new_sum = old_sum)
Afxmessagebox (L "Remove USB device ");
Else if (old_sum-new_sum = 1)
Afxmessagebox (L "SD card removal ");
}
If (cur_count> old_count) & (cur_count-old_count = 1) // new device added
{
New_sum = count (cur_struct );
If (new_sum = old_sum)
Afxmessagebox (L "Access USB device ");
Else if (new_sum-old_sum = 1)
Afxmessagebox (L "Access SD card ");
}
Old_itemnamelist = cur_itemnamelist;
Old_sum = new_sum;
}

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.