/ ************************************************** *****************************
Demonstrate how to program the implementation to disable the network card
Mady by Zwell
2004.7.29
Zwell@sohu.com
*********************************************************** *********************************** /
#include
#include
#include
#include
#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 & DV_8139 & SUBSYS_813910EC";
DWORD newState;
IF (BStatus)
{
NEWSTATE = DICS_DISABLE;
// Disable
}
Else
{
NEWSTATE = DICS_ENABLE;
// Enable
}
// Call the DDK function to disable the network card
DWORD I, ERR;
Bool Found = false;
HDEVINFO 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 ("Access System Hardware Error!");
Return False;
}
// Enumerate the hardware, get the needed interface
SPDEVINFODATA.CBSIZE = SizeOf (sp_devinfo_data);
For (i = 0; SetupdienumDeviceInfo (HDEVINFO, I, & SPDEVINFODATA); i )
{
DWORD DATAT;
LPTSTR P, BUFFER = NULL;
DWORD BUFFERSIZE = 0;
/ / Get the attribute value of the hardware
While (! setupdiGetDeviceRegistryProperty)
HDevinfo,
& spdevinfodata,
SPDRP_HARDWAREID,
& Datat,
(Pbyte) buffer,
Buffersize,
& buffersize))
{
IF (getLastError () == error_INVALID_DATA)
{
// There is no HardwareID. Continue.
Break;
}
ELSE IF (getLastError () == error_insuffect_buffer
{
// buffer size is wrong.
IF (buffer)
Localfree (buffer);
Buffer = (char *) Localalloc (LPTR, BUFFERSIZE);}
Else
{
//unknown mistake
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 equally
IF (Found)
{
// Disable the equipment
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, DICS_ENABLE 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 ("successfully disabled network!");
Break;
Case DICS_ENABLE:
Printf ("successfully enabled network!");
Break;
}
Break;
}
}
// When you quit, clean your work environment
Cleanup_DeviceInfo:
Err = getLastError ();
SetupdideStroyDeviceInfolist (HDevinfo);
SetLastError (ERR);
Return True;
}
Void usage (char * evefile)
{
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 ("Failure to NIC!"); Return 0;
}