1 #include "stdio.h" 2 #include "windows.h" 3 4 void GetHostWLAN_IPv4_AND_MAC(char IPv4[], char MAC[],char GateWay[]); //获取WLAN下的IPv4和MAC地址,局域网网关IPv4地址(基于win10中文版编写) 5 6 int main() 7 { 8 char IPv4[16] = { 0 }; 9 char MAC[18] = { 0 }; 10 char Gway[16] = { 0 }; 11 GetHostWLAN_IPv4_AND_MAC(IPv4,MAC,Gway); 12 printf("%s %d\n", Gway,strlen(Gway)); 13 printf("%s %d\n", IPv4, strlen(IPv4)); 14 printf("%s %d\n", MAC, strlen(MAC)); 15 return 0; 16 } 17 18 void GetHostWLAN_IPv4_AND_MAC(char IPv4[],char MAC[],char GateWay[]) 19 { 20 memset(IPv4, 0, sizeof(IPv4)); 21 memset(MAC, 0, sizeof(MAC)); 22 system("ipconfig/all > E:\\ipconfig.txt"); 23 FILE *fp = fopen("E:\\ipconfig.txt", "r"); 24 if (!fp) 25 { 26 printf("failed to open file!"); 27 exit(-1); 28 } 29 char strInfo[128] = { 0 }; 30 bool find_wlan = false; 31 bool find_mac = false; 32 bool find_ipv4 = false; 33 bool find_gateway = false; 34 int i, pos; 35 while (fgets(strInfo, 128, fp) != NULL) 36 { 37 if (strstr(strInfo, "WLAN") != NULL) 38 find_wlan = true; 39 40 if (find_wlan) 41 { 42 if (strstr(strInfo, "物理地址") != NULL) //获取MAC地址 43 { 44 pos = 16; 45 for (i = strlen(strInfo);; i--) 46 { 47 if (strInfo[i - 2] != ‘\0‘&&pos >= 0) 48 { 49 if (strInfo[i - 2] != ‘-‘) 50 MAC[pos--] = strInfo[i - 2]; 51 else 52 MAC[pos--] = ‘:‘; 53 } 54 else 55 { 56 find_mac = true; 57 break; 58 } 59 } 60 } 61 62 if (strstr(strInfo, "IPv4") != NULL) //获取IPv4地址 63 { 64 for (i = 10;; i++) 65 { 66 if (strInfo[i] == ‘:‘) 67 break; 68 } 69 pos = i+2; 70 for (i=0;; i++,pos++) 71 { 72 if (strInfo[pos] != ‘\0‘&&strInfo[pos] != ‘(‘) 73 IPv4[i] = strInfo[pos]; 74 else 75 { 76 find_ipv4 = true; 77 break; 78 } 79 } 80 } 81 82 if (strstr(strInfo, "默认网关") != NULL) //获取默认网关IPv4地址 83 { 84 memset(strInfo, 0, 128); 85 fgets(strInfo, 128, fp); //获取下一行 86 for (i = 0,pos = 0;; pos++) 87 { 88 if (strInfo[pos] == ‘\n‘) 89 { 90 find_gateway = true; 91 break; 92 } 93 if (strInfo[pos]>=‘0‘&&strInfo[pos]<=‘9‘ || strInfo[pos]==‘.‘) 94 GateWay[i++] = strInfo[pos]; 95 } 96 } 97 98 } 99 if (find_mac && find_ipv4 && find_gateway) 100 break; 101 memset(strInfo, 0, 128); 102 } 103 }
原文地址:https://www.cnblogs.com/lyj-blogs/p/11739828.html
时间: 2024-10-08 10:19:21