Train of Thought: Get the iftable table and get whether the current status of the device is mib_if_admin_status_up or mib_if_admin_status_down.
Implementation:
1) first check the NIC status checknetstatus ()
2) Enable and disable the NIC based on its current status.
Checknetstatus ()
Including header files and libraries <br/> # include <PM. h> <br/> # include <iphlpapi. h> <br/> # pragma comment (Lib, "iphlpapi. lib ") <br/> bool checknetstatus () <br/> {<br/> bool netstatus = true; // The default status is networked. <br/> pmib_iftable iftable; <br/> DWORD dwsize = 0; <br/> DWORD dwretval = 0; <br/> mib_ifrow ifrow; <br/> // allocate memory for our pointers. <br/> iftable = (mib_iftable *) malloc (sizeof (mib_iftable); <br/> // obtain the test and obtain enough space <br/> If (get Iftable (iftable, & dwsize, 0) = error_insufficient_buffer) {<br/> // release the space requested before the iftable, not processed <br/> iftable = (mib_iftable *) malloc (dwsize); <br/>}< br/> // obtain <br/> If (dwretval = getiftable (iftable, & dwsize, 0 )) = no_error) <br/>{< br/> for (INT I = 0; I <(INT) iftable-> dwnumentries; I ++) <br/> {<br/> ifrow = iftable-> table [I]; <br/> char * PTR = (char *) (ifrow. bdescr); <br/> wtolower (PTR); <br/> If (strst R (PTR, "USB") | strstr (PTR, "loopback") <br/> continue; <br/> dwretval = getifentry (& ifrow ); <br/> If (ifrow. dwadminstatus = mib_if_admin_status_down) <br/> netstatus = false; <br/> else <br/> netstatus = true; <br/>}< br/> else <br/> {<br/> const char * notice11 = "getiftable failed .! "; <Br/> log (notice11, strlen (notice11); <br/>}< br/> free (iftable); <br/> return netstatus; <br/>
Disable Nic
Void disablenetwork ()
Void disablenetwork () <br/>{< br/> If (! Checknetstatus () <br/>{< br/> const char * notice = "the network is already disabled and you do not need to disable it again"; <br/> log (notice, strlen (Notice); <br/> return; <br/>}< br/> // todo: add the control notification handler code here <br/> pmib_iftable iftable; <br/> DWORD dwsize = 0; <br/> DWORD dwretval = 0; <br/> mib_ifrow ifrow; <br/> // allocate memory for our pointers. <br/> iftable = (mib_iftable *) malloc (sizeof (mib_iftable); <br/> // obtain the test result, obtain enough space <br/> If (getiftable (iftable, & Dwsize, 0) = error_insufficient_buffer) {<br/> // release the space requested before the iftable, not processed <br/> iftable = (mib_iftable *) malloc (dwsize); <br/>}< br/> // obtain <br/> If (dwretval = getiftable (iftable, & dwsize, 0 )) = no_error) <br/>{< br/> for (INT I = 0; I <(INT) iftable-> dwnumentries; I ++) <br/> {<br/> // reset interface status <br/> ifrow = iftable-> table [I]; <br/> char * PTR = (char *) (ifrow. bdescr); <br/> wtolower (PTR); <br/> If (Strstr (PTR, "USB") | strstr (PTR, "loopback") // devices of different models are different <br/> continue; <br/> char notice00 [max_path] = "Description:"; <br/> zeromemory (¬ ice00 [strlen ("Description:") + 1], MAX_PATH-strlen ("Description: ")-1); <br/> strcat (notice00, PTR); <br/> log (notice00, strlen (notice00 )); // write the log file <br/> ifrow. dwadminstatus = mib_if_admin_status_down; <br/> If (dwretval = setifentry (& ifrow) = no_error) <br/>{< br/>/ /Printf ("setifentry mib_if_admin_status_down. /n "); <br/> char notice00 [max_path] =" Disable success: "; <br/> zeromemory (paiice00 [strlen (" Disable success :") + 1], MAX_PATH-strlen ("Disable success:")-1); <br/> strcat (notice00, PTR); <br/> log (notice00, strlen (notice00 )); // write the log file <br/>}< br/> else <br/>{< br/> const char * notice11 =" getiftable failed .! "; <Br/> log (notice11, strlen (notice11); <br/>}< br/> free (iftable ); <br/> // release the space requested before the iftable, not processed <br/>}
// Enable the NIC
Void enablenetwork ()
Void enablenetwork () <br/>{< br/> If (checknetstatus () <br/>{< br/> const char * notice = "the network is enabled, no need to enable "; <br/> log (Notice, strlen (Notice); <br/> return; <br/>}< br/> pmib_iftable iftable; <br/> DWORD dwsize = 0; <br/> DWORD dwretval = 0; <br/> mib_ifrow ifrow; <br/> // allocate memory for our pointers. <br/> iftable = (mib_iftable *) malloc (sizeof (mib_iftable); <br/> // obtain the test and obtain enough space <br/> If (getiftable (Iftable, & dwsize, 0) = error_insufficient_buffer) {<br/> // release the space requested before the iftable, not processed <br/> iftable = (mib_iftable *) malloc (dwsize); <br/>}< br/> // obtain <br/> If (dwretval = getiftable (iftable, & dwsize, 0 )) = no_error) <br/>{< br/> for (INT I = 0; I <(INT) iftable-> dwnumentries; I ++) <br/> {<br/> // reset interface status <br/> ifrow = iftable-> table [I]; <br/> char * PTR = (char *) (ifrow. bdescr); <br/> wtolower (PTR); <Br/> If (strstr (PTR, "USB") | strstr (PTR, "loopback") <br/> continue; <br/> char notice00 [max_path] = "Description:"; <br/> zeromemory (¬ ice00 [strlen ("Description:") + 1], MAX_PATH-strlen ("Description: ")-1); <br/> strcat (notice00, PTR); <br/> log (notice00, strlen (notice00 )); // write the log file <br/> ifrow. dwadminstatus = mib_if_admin_status_up; <br/> If (dwretval = setifentry (& ifrow) = no_error) <br/>{< br/>/print F ("setifentry mib_if_admin_status_up. /n "); <br/> char notice00 [max_path] =" enable sucess: "; <br/> zeromemory (paiice00 [strlen (" enable sucess :") + 1], MAX_PATH-strlen ("enable sucess:")-1); <br/> strcat (notice00, PTR); <br/> log (notice00, strlen (notice00 )); // write the log file <br/>}< br/> else <br/>{< br/> const char * notice11 =" getiftable failed .! "; <Br/> log (notice11, strlen (notice11); <br/>}< br/> free (iftable); <br/>}< br/>