硬件设备的监控处理之二——(网卡的禁用和启用)

思路:获取IfTable表,得到设备当前的状态是MIB_IF_ADMIN_STATUS_UP还是MIB_IF_ADMIN_STATUS_DOWN

实现: 1)先检测网卡的状态checkNetStatus()

2)根据网卡的当前状态来决定启用和禁用

检测网卡的状态checkNetStatus()

包含头文件和库 #include #include #pragma comment(lib, "iphlpapi.lib") bool checkNetStatus() { bool netStatus = true;//默认状态为已联网 PMIB_IFTABLE ifTable; DWORD dwSize = 0; DWORD dwRetVal = 0; MIB_IFROW IfRow; // Allocate memory for our pointers. ifTable = (MIB_IFTABLE*) malloc(sizeof(MIB_IFTABLE)); //试探获取,获取足够大的空间 if (GetIfTable(ifTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) { //释放ifTable前边申请的空间,未处理 ifTable = (MIB_IFTABLE *) malloc (dwSize); } //真正获取 if ((dwRetVal = GetIfTable(ifTable, &dwSize, 0)) == NO_ERROR) { for (int i = 0; i < (int)ifTable->dwNumEntries; i++) { IfRow = ifTable->table[i]; char* ptr = (char*)(IfRow.bDescr); wToLower(ptr); if(strstr(ptr, "usb") || strstr(ptr, "loopback")) continue; dwRetVal = GetIfEntry(&IfRow); if(IfRow.dwAdminStatus == MIB_IF_ADMIN_STATUS_DOWN) netStatus = false; else netStatus = true; } } else { const char * notice11 = "GetIfTable failed.!"; log(notice11, strlen(notice11)); } free(ifTable); return netStatus;  

 

网卡禁用

 

void  DisableNetWork()

void DisableNetWork() { if(!checkNetStatus()) { const char * notice = "网络已处于禁用状态,无需再次禁用"; log(notice, strlen(notice)); return; } // TODO: 在此添加控件通知处理程序代码 PMIB_IFTABLE ifTable; DWORD dwSize = 0; DWORD dwRetVal = 0; MIB_IFROW IfRow; // Allocate memory for our pointers. ifTable = (MIB_IFTABLE*) malloc(sizeof(MIB_IFTABLE)); //试探获取,获取足够大的空间 if (GetIfTable(ifTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) { //释放ifTable前边申请的空间,未处理 ifTable = (MIB_IFTABLE *) malloc (dwSize); } //真正获取 if ((dwRetVal = GetIfTable(ifTable, &dwSize, 0)) == NO_ERROR) { for (int i = 0; i < (int)ifTable->dwNumEntries; i++) { //重置interface状态 IfRow = ifTable->table[i]; char* ptr = (char*)(IfRow.bDescr); wToLower(ptr); if(strstr(ptr, "usb") || strstr(ptr, "loopback"))//不同型号的设备不同 continue; char notice00[MAX_PATH] = "Description:"; ZeroMemory(¬ice00[strlen("Description:")+1],MAX_PATH-strlen("Description:")-1); strcat(notice00, ptr); log(notice00, strlen(notice00));//写入日志文件 IfRow.dwAdminStatus = MIB_IF_ADMIN_STATUS_DOWN; if ((dwRetVal = SetIfEntry(&IfRow)) == NO_ERROR) { //printf("SetIfEntry MIB_IF_ADMIN_STATUS_DOWN./n"); char notice00[MAX_PATH] = "Disable Success:"; ZeroMemory(¬ice00[strlen("Disable Success:")+1],MAX_PATH-strlen("Disable Success:")-1); strcat(notice00, ptr); log(notice00, strlen(notice00));//写入日志文件 } } } else { const char * notice11 = "GetIfTable failed.!"; log(notice11, strlen(notice11)); } free(ifTable); //释放ifTable前边申请的空间,未处理 } 

 

 

//网卡启用

 

void EnableNetWork()

void EnableNetWork() { if(checkNetStatus()) { const char * notice = "网络已处于启用状态,无需再次启用"; log(notice, strlen(notice)); return; } PMIB_IFTABLE ifTable; DWORD dwSize = 0; DWORD dwRetVal = 0; MIB_IFROW IfRow; // Allocate memory for our pointers. ifTable = (MIB_IFTABLE*) malloc(sizeof(MIB_IFTABLE)); //试探获取,获取足够大的空间 if (GetIfTable(ifTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) { //释放ifTable前边申请的空间,未处理 ifTable = (MIB_IFTABLE *) malloc (dwSize); } //真正获取 if ((dwRetVal = GetIfTable(ifTable, &dwSize, 0)) == NO_ERROR) { for (int i = 0; i < (int)ifTable->dwNumEntries; i++) { //重置interface状态 IfRow = ifTable->table[i]; char* ptr = (char*)(IfRow.bDescr); wToLower(ptr); if(strstr(ptr, "usb") || strstr(ptr, "loopback")) continue; char notice00[MAX_PATH] = "Description:"; ZeroMemory(¬ice00[strlen("Description:")+1],MAX_PATH-strlen("Description:")-1); strcat(notice00, ptr); log(notice00, strlen(notice00));//写入日志文件 IfRow.dwAdminStatus = MIB_IF_ADMIN_STATUS_UP; if ((dwRetVal = SetIfEntry(&IfRow)) == NO_ERROR) { //printf("SetIfEntry MIB_IF_ADMIN_STATUS_UP./n"); char notice00[MAX_PATH] = "Enable Sucess:"; ZeroMemory(¬ice00[strlen("Enable Sucess:")+1],MAX_PATH-strlen("Enable Sucess:")-1); strcat(notice00, ptr); log(notice00, strlen(notice00));//写入日志文件 } } } else { const char * notice11 = "GetIfTable failed.!"; log(notice11, strlen(notice11)); } free(ifTable); }  

 

 

你可能感兴趣的:(Wince,开发笔记,interface,buffer,path,网络,up)