A program that can disable USB storage devices (setupapi usage)

Source: Internet
Author: User
A program that can disable USB storage devices (setupapi usage) A program that can disable USB storage devices (setupapi usage) Author: WY. lslrt Environment: XP SP1 DDKThis program uses setupapi and the cm_xx function family in DDK to enumerate, query, and control devices. The following is a description of the setupapi function used, followed by the program code. This program can actually disable any device that can be disabled, but I didn't do it, the first operation disables all USB mass storage devices and the second operation disables them. Use DDK for compiling. Or use driverstdio in VC to set the environment to DDK environment and compile setupdigetclassdevs. If the second parameter is set to null and the fourth parameter is digcf_allclasses, all device information is returned.

Setupdigetclassimagelist creates an image list containing all installed class bitmaps and returns the data structure containing this list: sp_classimage_data
typedef struct _SP_CLASSIMAGE_DATA {
  DWORD cbSize;
  HIMAGELIST cbSize;
  HIMAGELIST ImageList;
  DWORD Reserved;
} SP_CLASSIMAGE_DATA, *PSP_CLASSIMAGE_DATA;
The cbsize of the imagelist image list handle must be initialized first, sizeof (sp_classimage_data)

Setupdienumdeviceinfo is the unit of the device information set. The returned context structure is sp_devinfo_data.
typedef struct _SP_DEVINFO_DATA {
 DWORD cbSize;
cbSize;
 GUID   ClassGuid;
ClassGuid;
 DWORD DevInst;
DevInst;
 ULONG_PTR Reserved;
} Sp_devinfo_data, * psp_devinfo_data; this structure defines the device instance of a device information integrator (?? I am not quite clear about it, But DDK says so in English)

Cm_get_devnode_status is used to obtain the status of the device instance. The third input parameter can be the devinst in the above structure.

Setupdiopenclassregkey

Setupdigetdeviceregistryproperty obtains the specified plug-and-play device property.

Setupdigetclassimageindex obtains the index of the specified device in the device image list.

Setupdigetdeviceinstallparams

Setupdisetclassinstallparams is a device information set or an actual device information unit. To set or clear the class installation parameters, You need to input the sp_propchange_params structure pointer, which is responsible for coordinating the dif_propertychange installation request.

Setupdicallclassinstaller calls the installation service according to the DIF code/* ------------------------------------------------------- common. h using */# include <stdio. h>
# Include <malloc. h>
# Include <tchar. h>
# Include <string. h>
# Include <windows. h>
# Include <setupapi. h>
# Include <cfgmgr32.h>
# Include <regstr. h> # define unknowndevice text ("<Unknown device> ")
# Define USB disk text ("mass") // device information structure linked list
Typedef struct _ device_info
{
Sp_devinfo_data wy_devinfodata; // The device information includes the instance handle and guid of the device class.
DWORD wy_devid; // The device ID in the global enumeration.
Tchar * wy_lpszdevname; // device name
Ulong wy_uldevnamelen; // The length of the device name
Bool wy_isdisableable; // whether it can be disabled
Bool wy_isdisabled; // whether it has been disabled
Struct _ device_info * wy_pnextdevinfo; // The next Structure Block
} Wy_device_info, * wy_pdevice_info; tchar wy_tcdebugstr [1024]; // output debugging information
// The statechange function is used to change the device status. It can be set to enable or disable
Bool statechange (DWORD wy_dwnewstate, DWORD wy_dwdevid, hdevinfo wy_hdevinfo );
// The enumdevice function is used to enumerate all devices contained in a computer.
Bool enumdevices (hwnd wy_hdevinfo );
// The getregistryproperty function is used to obtain the content in the device registry.
Bool getregistryproperty (hdevinfo wy_hdevinfo, psp_devinfo_data wy_devinfodata, ulong wy_ulproperty, pvoid wy_pbuffer, Pulong wy_pulbuflen );
// The constructdevicename function is used to construct the device name.
Bool constructdevicename (hdevinfo deviceinfoset, psp_devinfo_data deviceinfodata, pvoid buffer, Pulong length );
// Enabledevice function used to enable the device
Bool enabledevice (DWORD wy_dwdevid, hdevinfo wy_hdevinfo );
// The disabledevice function is used to disable a device.
Bool disabledevice (DWORD wy_dwdevid, hdevinfo wy_hdevinfo );
// Query whether the specified device can be disabled
Bool isdisableable (DWORD wy_dwdevid, hdevinfo wy_hdevinfo );
// Query whether the specified device is disabled
Bool isdisabled (DWORD wy_dwdevid, hdevinfo wy_hdevinfo );
// Guid structure replication function,
// Because the header file of the SDK function does not know why it cannot be used in the DDK environment, you have to write it yourself
Void guidcopy (guid wy_guidsrc, guid * wy_guiddest );
// Control the USB device
Bool controldevice (DWORD wy_dwdevid, hdevinfo wy_hdevinfo);/* -------------------------------------------- devctrl. c ---------------------------------------------------------------------------------- */# include "Common. H "wy_device_info wy_devinfo; // device linked list header int apientry winmain (hinstance wy_hinstance, hinstance wy_hpreinstance, lpstr wy_lpcmd, int wy_n1_num)
{
Hdevinfo wy_hdevinfo;
Wy_pdevice_info wy_pdevinfo;
// Return device information
Wy_hdevinfo = setupdigetclassdevs (null, digcf_present | digcf_allclasses );
_ Stprintf (wy_tcdebugstr, "wy_hdevinfo % x", wy_hdevinfo );
Outputdebugstring (wy_tcdebugstr );
 
Wy_devinfo.wy_devinfodata.cbsize = sizeof (sp_devinfo_data );
Wy_devinfo.wy_uldevnamelen = 256;
Wy_devinfo.wy_pnextdevinfo = NULL;
// Enumerate system devices
Enumdevices (wy_hdevinfo );
Wy_pdevinfo = wy_devinfo.wy_pnextdevinfo;
While (wy_pdevinfo)
{// Find the USB mass storage device from the System Device
_ Stprintf (wy_tcdebugstr, "devid: % d guid: % x-% x devinst: % x, devname: % s isdisableable: % d, isdisabled: % d"
, Wy_pdevinfo-> wy_devid
, Wy_pdevinfo-> wy_devinfodata.classguid.data1
, Wy_pdevinfo-> wy_devinfodata.classguid.data2
, Wy_pdevinfo-> wy_devinfodata.classguid.data3
, Wy_pdevinfo-> wy_devinfodata.classguid.data4 [0]
, Wy_pdevinfo-> wy_devinfodata.classguid.data4 [1]
, Wy_pdevinfo-> wy_devinfodata.classguid.data4 [2]
, Wy_pdevinfo-> wy_devinfodata.classguid.data4 [3]
, Wy_pdevinfo-> wy_devinfodata.classguid.data4 [4]
, Wy_pdevinfo-> wy_devinfodata.classguid.data4 [5]
, Wy_pdevinfo-> wy_devinfodata.classguid.data4 [6]
, Wy_pdevinfo-> wy_devinfodata.classguid.data4 [7]
, Wy_pdevinfo-> wy_devinfodata.devinst
, Wy_pdevinfo-> wy_lpszdevname
, Wy_pdevinfo-> wy_isdisableable
, Wy_pdevinfo-> wy_isdisabled); outputdebugstring (wy_tcdebugstr );
If (strstr (wy_pdevinfo-> wy_lpszdevname, usbdisk ))
{

// Control the status of the USB mass storage device. If enabled, it is disabled. If used only, it is enabled.
If (controldevice (wy_pdevinfo-> wy_devid, wy_hdevinfo ))
{
Outputdebugstring ("successful ");
}
Else outputdebugstring ("failed ");

}
Wy_pdevinfo = wy_pdevinfo-> wy_pnextdevinfo;
}
Setupdidestroydeviceinfolist (wy_hdevinfo );
Return 0;
} Bool statechange (DWORD wy_dwnewstate, DWORD wy_dwdevid, hdevinfo wy_hdevinfo)
{
Sp_propchange_params wy_propchangeparams;
Sp_devinfo_data wy_devinfodata = {sizeof (sp_devinfo_data )};
Sp_devinstall_params wy_devparams; // query the device information.
If (! Setupdienumdeviceinfo (wy_hdevinfo, wy_dwdevid, & wy_devinfodata ))
{
Outputdebugstring ("setupdienumdeviceinfo failed ");
Return false;
} // Set parameters for device property changes
Wy_propchangeparams.classinstallheader.cbsize = sizeof (sp_classinstall_header );
Wy_propchangeparams.classinstallheader.installfunction = dif_propertychange;
Wy_propchangeparams.scope = dics_flag_global; // Save the modified attributes to all hardware attribute files.
Wy_propchangeparams.statechange = wy_dwnewstate;
Wy_propchangeparams.hwprofile = 0;
// Change device Properties
If (! Setupdisetclassinstallparams (wy_hdevinfo, & wy_devinfodata, (sp_classinstall_header *) & wy_propchangeparams, sizeof (wy_propchangeparams )))
{
Outputdebugstring ("setupdisetclassinstallparams failed ");
Return false;
}
 
Wy_propchangeparams.classinstallheader.cbsize = sizeof (sp_classinstall_header );
Wy_propchangeparams.classinstallheader.installfunction = dif_propertychange;
Wy_propchangeparams.scope = dics_flag_configspecific; // Save the modified attribute to the specified attribute file.
Wy_propchangeparams.statechange = wy_dwnewstate;
Wy_propchangeparams.hwprofile = 0;
// Change device properties and call the installation service
If (! Setupdisetclassinstallparams (wy_hdevinfo, & wy_devinfodata, (sp_classinstall_header *) & wy_propchangeparams, sizeof (wy_propchangeparams ))
|! Setupdicallclassinstaller (dif_propertychange, wy_hdevinfo, & wy_devinfodata ))
{
Outputdebugstring ("setupdisetclassinstallparams or setupdicallclassinstaller failed ");
Return true;
}
Else
{// Determine whether a restart is required
Wy_devparams.cbsize = sizeof (wy_devparams );
If (! Setupdigetdeviceinstallparams (wy_hdevinfo, & wy_devinfodata, & wy_devparams ))
{
Outputdebugstring ("setupdigetdeviceinstallparams failed ");
Return false;
}
If (! Wy_devparams.flags & (di_needrestart | di_needreboot ))
{
Outputdebugstring ("need Restart computer ");
Return true;
}
Return true;
}
} Bool enumdevices (hwnd wy_hdevinfo)
{
DWORD wy_dwdevid, wy_dwstatus, wy_dwproblem;
Sp_devinfo_data wy_devinfodata = {sizeof (sp_devinfo_data )};
Wy_pdevice_info wy_pdevinfo, wy_ppredevinfo;
Tchar * wy_devicename;
Ulong wy_ulnamelen = 256;
 
Wy_ppredevinfo = & wy_devinfo;
// List every device
For (wy_dwdevid = 0; setupdienumdeviceinfo (wy_hdevinfo, wy_dwdevid, & wy_devinfodata); wy_dwdevid ++)
{
// Construct the device information block
Wy_pdevinfo = (wy_pdevice_info) malloc (sizeof (wy_device_info ));
Wy_pdevinfo-> wy_pnextdevinfo = NULL;
Wy_pdevinfo-> wy_devinfodata.cbsize = sizeof (sp_devinfo_data );
Wy_pdevinfo-> wy_uldevnamelen;
Wy_pdevinfo-> wy_devid = wy_dwdevid;
Guidcopy (wy_devinfodata.classguid, & wy_pdevinfo-> wy_devinfodata.classguid );
Wy_pdevinfo-> wy_devinfodata.devinst = wy_devinfodata.devinst;
Wy_pdevinfo-> wy_devinfodata.reserved = wy_devinfodata.reserved;
// There is a problem here, if the string pointer in the device information block structure is used
// The setupdigetdeviceregistyproperty function returns invalid data and does not know why.
// You can only copy it again.
Constructdevicename (wy_hdevinfo, & wy_devinfodata, & wy_devicename, & wy_ulnamelen );
Wy_pdevinfo-> wy_uldevnamelen = wy_ulnamelen;
Wy_pdevinfo-> wy_lpszdevname = (tchar *) malloc (wy_ulnamelen * sizeof (tchar ));
Memcpy (wy_pdevinfo-> wy_lpszdevname, wy_devicename, wy_ulnamelen); wy_pdevinfo-> wy_isdisableable = isdisableable (wy_dwdevid, wy_hdevinfo );
Wy_pdevinfo-> wy_isdisabled = isdisabled (wy_dwdevid, wy_hdevinfo); wy_ppredevinfo-> wy_pnextdevinfo = wy_pdevinfo;
Wy_ppredevinfo = wy_pdevinfo;
}
Return false;
} Bool getregistryproperty (hdevinfo wy_hdevinfo, psp_devinfo_data wy_devinfodata, ulong wy_ulproperty, pvoid wy_pbuffer, Pulong wy_pulbuflen)
{
// Read the registry information of the device.
While (! Setupdigetdeviceregistryproperty (wy_hdevinfo,
Wy_devinfodata,
Wy_ulproperty, // Information Field
Null,
(Pvoid) * (tchar **) wy_pbuffer,
* Wy_pulbuflen,
Wy_pulbuflen
))
{
If (getlasterror () = error_insufficient_buffer)
{// Re-allocate the buffer if the length is not enough

If (* (lptstr *) wy_pbuffer)
Localfree (* (lptstr *) wy_pbuffer );
* (Lptstr *) wy_pbuffer = localalloc (lptr, * wy_pulbuflen );
}
Else
{

If (getlasterror ()! = Error_invalid_data)
Outputdebugstring (text ("getdeviceregistryproperty "));
Return false;
}
} Return (* (lptstr *) wy_pbuffer) [0];
} Bool constructdevicename (hdevinfo wy_devinfoset, psp_devinfo_data wy_devinfodata, pvoid wy_pbuffer, Pulong length)
{
// Try to get the device name through various device descriptions
If (! Getregistryproperty (wy_devinfoset,
Wy_devinfodata,
Spdrp_friendlyname, // actual device name
Wy_pbuffer,
Length ))
{
If (! Getregistryproperty (wy_devinfoset,
Wy_devinfodata,
Spdrp_devicedesc, // device description
Wy_pbuffer,
Length ))
{
If (! Getregistryproperty (wy_devinfoset,
Wy_devinfodata,
Spdrp_class, // device Class Name
Wy_pbuffer,
Length ))
{
If (! Getregistryproperty (wy_devinfoset,
Wy_devinfodata,
Spdrp_classguid, // guid of the device
Wy_pbuffer,
Length ))
{// Unknowndevice is returned if nothing is found
* Length = (_ tcslen (unknowndevice) + 1) * sizeof (tchar );
* (Lptstr *) wy_pbuffer = localalloc (lptr, * length );
_ Tcscpy (* (lptstr *) wy_pbuffer, unknowndevice );
}
}
}
}
Return true;
} Bool enabledevice (DWORD wy_dwdevid, hdevinfo wy_hdevinfo)
{
Return statechange (dics_enable, wy_dwdevid, wy_hdevinfo );
} Bool disabledevice (DWORD wy_dwdevid, hdevinfo wy_hdevinfo)
{
Return statechange (dics_disable, wy_dwdevid, wy_hdevinfo );
} Bool isdisableable (DWORD wy_dwdevid, hdevinfo wy_hdevinfo)
{
Sp_devinfo_data wy_devinfodata = {sizeof (sp_devinfo_data )};
DWORD wy_dwdevstatus, wy_dwproblem; If (! Setupdienumdeviceinfo (wy_hdevinfo, wy_dwdevid, & wy_devinfodata ))
{
Outputdebugstring ("setupdienumdeviceinfo failed ");
Return false;
}
// Query the device status
If (cm_get_devnode_status (& wy_dwdevstatus, & wy_dwproblem, wy_devinfodata.devinst, 0 )! = Cr_success)
{
Outputdebugstring ("cm_get_devnode_status failed ");
Return false;
} Return (wy_dwdevstatus & dn_disableable) & (wy_dwproblem! = Cm_prob_hardware_disabled ));
} Bool isdisabled (DWORD wy_dwdevid, hdevinfo wy_hdevinfo)
{
Sp_devinfo_data wy_devinfodata = {sizeof (sp_devinfo_data )};
DWORD wy_dwdevstatus, wy_dwproblem; If (! Setupdienumdeviceinfo (wy_hdevinfo, wy_dwdevid, & wy_devinfodata ))
{
Outputdebugstring ("setupdienumdeviceinfo failed ");
Return false;
}
// Query the device status
If (cm_get_devnode_status (& wy_dwdevstatus, & wy_dwproblem, wy_devinfodata.devinst, 0 )! = Cr_success)
{
Outputdebugstring ("cm_get_devnode_status failed ");
Return false;
} Return (wy_dwdevstatus & dn_has_problem) & (wy_dwproblem = cm_prob_disabled ));
} Void guidcopy (guid wy_guidsrc, guid * wy_guiddest)
{
Wy_guiddest-> data1 = wy_guidsrc.data1;
Wy_guiddest-> data2 = wy_guidsrc.data2;
Wy_guiddest-> data3 = wy_guidsrc.data3;
Memcpy (wy_guiddest-> data4, wy_guidsrc.data4, 8 );
} Bool controldevice (DWORD wy_dwdevid, hdevinfo wy_hdevinfo)
{
Bool wy_iscandisable; wy_iscandisable = (isdisableable (wy_dwdevid, wy_hdevinfo )&&(! Isdisabled (wy_dwdevid, wy_hdevinfo )));
If (wy_iscandisable)
Return disabledevice (wy_dwdevid, wy_hdevinfo );
Else
Return enabledevice (wy_dwdevid, wy_hdevinfo );
}/* -------------------------------------- Source file (all ddks of the makefile file are the same) -------------------------------- */targetname = devctrl
Targettype = programumtype = Windows
Umbase = 0x1000000
Umentry = winmaintargetpath = objtargetlibs = $ (sdk_lib_path)/setupapi. lib/
$ (Sdk_lib_path)/comctl32.lib/
$ (Sdk_lib_path)/cfgmgr32.lib/Includes =
 
Sources =/
Devctrl. c

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.