如何通过C语言获取主机WLAN下的IPv4地址,MAC地址,局域网网关IPv4地址

KilluaZoldyck 2019-10-25

#include "stdio.h" 
#include "windows.h"

void GetHostWLAN_IPv4_AND_MAC(char IPv4[], char MAC[],char GateWay[]); //获取WLAN下的IPv4和MAC地址,局域网网关IPv4地址(基于win10中文版编写)

int main()
{
    char IPv4[16] = { 0 };
    char MAC[18] = { 0 };
    char Gway[16] = { 0 };
    GetHostWLAN_IPv4_AND_MAC(IPv4,MAC,Gway);
    printf("%s %d\n", Gway,strlen(Gway));
    printf("%s %d\n", IPv4, strlen(IPv4));
    printf("%s %d\n", MAC, strlen(MAC));
    return 0;
}

void GetHostWLAN_IPv4_AND_MAC(char IPv4[],char MAC[],char GateWay[])
{
    memset(IPv4, 0, sizeof(IPv4));
    memset(MAC, 0, sizeof(MAC));
    system("ipconfig/all > E:\\ipconfig.txt");
    FILE *fp = fopen("E:\\ipconfig.txt", "r");
    if (!fp)
    {
        printf("failed to open file!");
        exit(-1);
    }
    char strInfo[128] = { 0 };
    bool find_wlan = false;
    bool find_mac = false;
    bool find_ipv4 = false;
    bool find_gateway = false;
    int i, pos;
    while (fgets(strInfo, 128, fp) != NULL)
    {
        if (strstr(strInfo, "WLAN") != NULL)
            find_wlan = true;

        if (find_wlan)
        {
            if (strstr(strInfo, "物理地址") != NULL) //获取MAC地址
            {
                pos = 16;
                for (i = strlen(strInfo);; i--)
                {
                    if (strInfo[i - 2] != ‘\0‘&&pos >= 0)
                    {
                        if (strInfo[i - 2] != ‘-‘)
                            MAC[pos--] = strInfo[i - 2];
                        else
                            MAC[pos--] = ‘:‘;
                    }
                    else
                    {
                        find_mac = true;
                        break;
                    }
                }
            }

            if (strstr(strInfo, "IPv4") != NULL) //获取IPv4地址
            {
                for (i = 10;; i++)
                {
                    if (strInfo[i] == ‘:‘)
                        break;
                }
                pos = i+2;
                for (i=0;; i++,pos++)
                {
                    if (strInfo[pos] != ‘\0‘&&strInfo[pos] != ‘(‘)
                        IPv4[i] = strInfo[pos];
                    else
                    {
                        find_ipv4 = true;
                        break;
                    }
                }
            }

            if (strstr(strInfo, "默认网关") != NULL) //获取默认网关IPv4地址
            {
                memset(strInfo, 0, 128);
                fgets(strInfo, 128, fp); //获取下一行
                for (i = 0,pos = 0;; pos++)
                {
                    if (strInfo[pos] == ‘\n‘)
                    {
                        find_gateway = true;
                        break;
                    }
                    if (strInfo[pos]>=‘0‘&&strInfo[pos]<=‘9‘ || strInfo[pos]==‘.‘)
                        GateWay[i++] = strInfo[pos];
                }
            }

        }
        if (find_mac && find_ipv4 && find_gateway)
            break;
        memset(strInfo, 0, 128);
    }
}

相关推荐

NewActivity / 0评论 2020-05-08