The Code is as follows:
Bool netconnect (bool bconnect)
{
Coinitialize (null );
Bool breturn = false;
Hresult hr;
Ccomptr <ishelldispatch> spshell;
// Search "shell object" in msdn
HR = spshell. cocreateinstance (clsid_shell, null, clsctx_inproc_server );
If (hR = s_ OK)
{
Ccomptr <folder> spfolder;
Ccomvariant vardir (49 );
Spshell-> namespace (vardir, & spfolder );
If (spfolder)
{
Ccomptr <folderitems> spfolderitems;
Spfolder-> items (& spfolderitems );
If (spfolderitems)
{
Long length = 0;
HR = spfolderitems-> get_count (& length );
For (INT I = 0; I <length; I ++)
{
Ccomvariant VAR (I );
Ccomptr <folderitem> spfolderitem;
Spfolderitems-> item (VAR, & spfolderitem );
If (spfolderitem)
{
Ccombstr BSTR;
Spfolderitem-> get_name (& BSTR );
// Traverse Network Connections
// Consider the system version.
If (wcsncmp (BSTR, l "Local Connection", 4) = 0)
{
Ccomptr <folderitemverbs> spfolderitemverbs;
Spfolderitem-> verbs (& spfolderitemverbs );
If (spfolderitemverbs)
{
// Traverse menu items
Long imenu = 0;
HR = spfolderitemverbs-> get_count (& imenu );
For (INT m = 0; m <imenu; m ++)
{
Ccomvariant VAR (m );
Ccomptr <folderitemverb> spfolderitemverb;
Spfolderitemverbs-> item (VAR, & spfolderitemverb );
If (spfolderitemverb)
{
Ccombstr bstrmenuname;
Lpwstr pwzmenuname = bconnect? L "enable": l "Disable ";
Spfolderitemverb-> get_name (& bstrmenuname );
If (wcsncmp (bstrmenuname, pwzmenuname, 2) = 0)
{
// Execute the menu command
Breturn = spfolderitemverb-> doit () = s_ OK;
}
}
}
}
}
}
}
}
}
}
Couninitialize ();
Return breturn;
}
The above code is tested on XP SP2 + vc6 + 200408-psdk.
This method is not translated by a piece of VB code I found on the VB Forum of the csdn forum. In this case, more functions can be implemented. The code is still flawed. The program uses the name to find the command to be executed, so it cannot be compatible on platforms in all languages. Also, valid connections may not be called "local connections" and cannot be used under 98.
In addition, the Code implemented using setupapi is attached. This method is more universal and is recommended.
# Include <setupapi. h>
# Include <cfgmgr32.h> // cfgmgr32.h in Microsoft Windows 2000 DDK.
# Pragma comment (Lib, "setupapi. lib ")
Bool isdisableable (DWORD dwdevid, hdevinfo );
Bool isdisabled (DWORD dwdevid, hdevinfo );
Bool statechange (DWORD dwnewstate, DWORD dwdevid, hdevinfo );
//////////////////////////////////////// //////////////////////////////////
// Obtain the content in the device Registry
//////////////////////////////////////// //////////////////////////////////
Bool getregistryproperty (hdevinfo deviceinfoset,
Psp_devinfo_data deviceinfodata,
Ulong property,
Pvoid buffer,
Pulong length)
{
While (! Setupdigetdeviceregistryproperty (deviceinfoset,
Deviceinfodata,
Property,
Null,
(Byte *) * (tchar **) buffer,
* Length,
Length ))
{
// Re-allocate the buffer if the length is not enough
If (getlasterror () = error_insufficient_buffer)
{
If (* (lptstr *) buffer)
Localfree (* (lptstr *) buffer );
* (Lptstr *) buffer = (pchar) localalloc (lptr, * length );
}
Else
{
Return false;
}
}
Return (bool) (* (lptstr *) buffer) [0];
}
Bool enabledevice (DWORD dwdevid, hdevinfo)
{
Return statechange (dics_enable, dwdevid, hdevinfo );
}
Bool disabledevice (DWORD dwdevid, hdevinfo)
{
Return statechange (dics_disable, dwdevid, hdevinfo );
}
Bool controldevice (DWORD dwdevid, hdevinfo)
{
Bool bcandisable;
Bcandisable = (isdisableable (dwdevid, hdevinfo )&&(! Isdisabled (dwdevid, hdevinfo )));
If (bcandisable)
Return disabledevice (dwdevid, hdevinfo );
Else
Return enabledevice (dwdevid, hdevinfo );
}
Void enumnetcards ()
{
DWORD status, problem;
Lptstr buffer = NULL;
DWORD bufsize = 0;
// Return all device information
Hdevinfo = setupdigetclassdevs (null, null, 0, digcf_present | digcf_allclasses );
If (invalid_handle_value = hdevinfo)
Return;
Sp_devinfo_data deviceinfodata = {sizeof (sp_devinfo_data )};
//////////////////////////////////////// //////////////////////////////////
// Enumerate Devices
//////////////////////////////////////// //////////////////////////////////
For (DWORD DeviceID = 0;
Setupdienumdeviceinfo (hdevinfo, DeviceID, & deviceinfodata );
DeviceID ++)
{
// Obtain the device status
If (cm_get_devnode_status (& status, & problem, deviceinfodata. devinst, 0 )! = Cr_success)
Continue;
// Obtain the device Class Name
Tchar szdevname [max_path] = _ T ("");
If (getregistryproperty (hdevinfo, & deviceinfodata, spdrp_class, & buffer, (Pulong) & bufsize ))
{
Lstrcpyn (szdevname, buffer, max_path );
}
If (lstrcmp (szdevname, _ T ("Net") = 0)
{
Tchar szname [max_path] = _ T ("");
If (getregistryproperty (hdevinfo, & deviceinfodata, spdrp_enumerator_name, & buffer, (Pulong) & bufsize ))
{
Lstrcpyn (szname, buffer, max_path );
}
If (lstrcmp (szname, _ T ("root "))! = 0)
{
If (getregistryproperty (hdevinfo, & deviceinfodata, spdrp_driver, & buffer, (Pulong) & bufsize ))
{
Lstrcpyn (szname, buffer, max_path );
// Obtain the device description
If (getregistryproperty (hdevinfo, & deviceinfodata, spdrp_devicedesc, & buffer, (Pulong) & bufsize ))
{
Lstrcpyn (szname, buffer, max_path );
If (controldevice (DeviceID, hdevinfo ))
{
Printf ("successful/N ");
}
Else
{
Printf ("failed/N ");
}
}
}
}
}
}
Setupdidestroydeviceinfolist (hdevinfo );
}
Bool statechange (DWORD dwnewstate, DWORD dwdevid, hdevinfo)
{
Sp_propchange_params propchangeparams;
Sp_devinfo_data devinfodata = {sizeof (sp_devinfo_data )};
Sp_devinstall_params devparams;
// Query device information
If (! Setupdienumdeviceinfo (hdevinfo, dwdevid, & devinfodata ))
{
Outputdebugstring ("setupdienumdeviceinfo failed ");
Return false;
}
// Set parameters for device property changes
Propchangeparams. classinstallheader. cbsize = sizeof (sp_classinstall_header );
Propchangeparams. classinstallheader. installfunction = dif_propertychange;
Propchangeparams. Scope = dics_flag_global; // Save the modified attributes to all hardware attribute files.
Propchangeparams. statechange = dwnewstate;
Propchangeparams. hwprofile = 0;
// Change device Properties
If (! Setupdisetclassinstallparams (hdevinfo,
& Devinfodata,
(Sp_classinstall_header *) & propchangeparams,
Sizeof (propchangeparams )))
{
Outputdebugstring ("setupdisetclassinstallparams failed ");
Return false;
}
Propchangeparams. classinstallheader. cbsize = sizeof (sp_classinstall_header );
Propchangeparams. classinstallheader. installfunction = dif_propertychange;
Propchangeparams. Scope = dics_flag_configspecific; // Save the modified attribute to the specified attribute file.
Propchangeparams. statechange = dwnewstate;
Propchangeparams. hwprofile = 0;
// Change device properties and call the installation service
If (! Setupdisetclassinstallparams (hdevinfo,
& Devinfodata,
(Sp_classinstall_header *) & propchangeparams,
Sizeof (propchangeparams) |
! Setupdicallclassinstaller (dif_propertychange, hdevinfo, & devinfodata ))
{
Outputdebugstring ("setupdisetclassinstallparams or setupdicallclassinstaller failed ");
Return true;
}
Else
{
// Determine whether to restart
Devparams. cbsize = sizeof (devparams );
If (! Setupdigetdeviceinstallparams (hdevinfo, & devinfodata, & devparams ))
{
Outputdebugstring ("setupdigetdeviceinstallparams failed ");
Return false;
}
If (devparams. Flags & (di_needrestart | di_needreboot ))
{
Outputdebugstring ("need Restart computer ");
Return true;
}
Return true;
}
}
Bool isdisableable (DWORD dwdevid, hdevinfo)
{
Sp_devinfo_data devinfodata = {sizeof (sp_devinfo_data )};
DWORD dwdevstatus, dwproblem;
If (! Setupdienumdeviceinfo (hdevinfo, dwdevid, & devinfodata ))
{
Outputdebugstring ("setupdienumdeviceinfo failed ");
Return false;
}
// Query the device status
If (cm_get_devnode_status (& dwdevstatus, & dwproblem, devinfodata. devinst, 0 )! = Cr_success)
{
Outputdebugstring ("cm_get_devnode_status failed ");
Return false;
}
Return (dwdevstatus & dn_disableable) & (dwproblem! = Cm_prob_hardware_disabled ));
}
Bool isdisabled (DWORD dwdevid, hdevinfo)
{
Sp_devinfo_data devinfodata = {sizeof (sp_devinfo_data )};
DWORD dwdevstatus, dwproblem;
If (! Setupdienumdeviceinfo (hdevinfo, dwdevid, & devinfodata ))
{
Outputdebugstring ("setupdienumdeviceinfo failed ");
Return false;
}
// Query the device status
If (cm_get_devnode_status (& dwdevstatus, & dwproblem, devinfodata. devinst, 0 )! = Cr_success)
{
Outputdebugstring ("cm_get_devnode_status failed ");
Return false;
}
Return (dwdevstatus & dn_has_problem) & (dwproblem = cm_prob_disabled ));
}
Int main (INT argc, char * argv [])
{
Enumnetcards ();
Return 0;
}