/*************************************** **************************************
Demonstrate how to enable and disable NICs by programming
Mady by Zwell
2004.7.29
Zwell@sohu.com
**************************************** *************************************/
# Include <windows. h>
# Include <setupapi. h>
# Include <tchar. h>
# Include <stdio. h>
# Pragma comment (Lib, "ws2_32.lib ")
# Pragma comment (Lib, "setupapi. lib ")
Bool disablenetinterface (bool bstatus)
{
In lptstr hardwareid;
// Hardware componentid, Registry address: System/CurrentControlSet/class/{4d36e972-e325-11ce-bfc1-08002be10318}/0000
Hardwareid = "PCI // ven_10ec & dev_8139 & subsys_813910ec ";
DWORD newstate;
If (bstatus)
{
Newstate = dics_disable;
// Disable
}
Else
{
Newstate = dics_enable;
// Enable
}
// Call the DDK function to disable the NIC
Dword I, err;
Bool found = false;
Hdevinfo;
Sp_devinfo_data spdevinfodata;
// Access the hardware library of the system
Hdevinfo = setupdigetclassdevs (null, "PCI", null, digcf_allclasses | digcf_present );
If (hdevinfo = invalid_handle_value)
{
Printf ("An error occurred while accessing system hardware! ");
Return false;
}
// Enumerate hardware to obtain the required interface
Spdevinfodata. cbsize = sizeof (sp_devinfo_data );
For (I = 0; setupdienumdeviceinfo (hdevinfo, I, & spdevinfodata); I ++)
{
DWORD datat;
Lptstr P, buffer = NULL;
DWORD buffersize = 0;
// Obtain the property value of the hardware.
While (! Setupdigetdeviceregistryproperty (
Hdevinfo,
& Spdevinfodata,
Spdrp_hardwareid,
& Datat,
(Pbyte) buffer,
Buffersize,
& Buffersize ))
{
If (getlasterror () = error_invalid_data)
{
// Hardwareid. Continue. does not exist.
Break;
}
Else if (getlasterror () = error_insufficient_buffer)
{
// The buffer size is incorrect.
If (buffer)
Localfree (buffer );
Buffer = (char *) localalloc (lptr, buffersize );
}
Else
{
// Unknown error
Goto cleanup_deviceinfo;
}
}
If (getlasterror () = error_invalid_data)
Continue;
// Compare, find the same item as the NIC ID
For (P = buffer; * P & (P <& buffer [buffersize]); P + = lstrlen (p) + sizeof (tchar ))
{
If (! _ Tcscmp (hardwareid, p ))
{
// Find the NIC
Found = true;
Break;
}
}
If (buffer)
Localfree (buffer );
// If equal
If (found)
{
// Disable this device
Sp_propchange_params sppropchangeparams;
Sppropchangeparams. classinstallheader. cbsize = sizeof (sp_classinstall_header );
Sppropchangeparams. classinstallheader. installfunction = dif_propertychange;
Sppropchangeparams. Scope = dics_flag_global;
Sppropchangeparams. statechange = newstate;
// Disable: dics_disable and dics_enable
//
If (! Setupdisetclassinstallparams (hdevinfo, & spdevinfodata, (sp_classinstall_header *) & sppropchangeparams, sizeof (sppropchangeparams )))
{
DWORD errorcode = getlasterror ();
}
If (! Setupdicallclassinstaller (dif_propertychange, hdevinfo, & spdevinfodata ))
{
DWORD errorcode = getlasterror ();
}
Switch (newstate)
{
Case dics_disable:
Printf ("Network disabled successfully! ");
Break;
Case dics_enable:
Printf ("Network enabled successfully! ");
Break;
}
Break;
}
}
// Clear the work environment when exiting
Cleanup_deviceinfo:
Err = getlasterror ();
Setupdidestroydeviceinfolist (hdevinfo );
Setlasterror (ERR );
Return true;
}
Void usage (char * exefile)
{
Printf ("Usage: % s [-E |-D]/R/N", exefile );
Printf ("/t-E: enable the network card./R/N ");
Printf ("/t-D: Disable the network card./R/N ");
Exit (0 );
}
Int main (INT argc, char ** argv)
{
If (argc <2)
Usage (argv [0]);
If (! Disablenetinterface (strstr (argv [1], "-d")> 0? True: false )))
Printf ("Nic operation failed! ");
Return 0;
}
---
Welcome to my blog: