获取本机网卡信息


/*
获取本地ip地址,子网掩码,默认网关,mac地址,网络适配器信息,hostname,域名 dns,网络接口信息 */ #include<WinSock2.h> #include<IPHlpApi.h> #include<stdio.h> #pragma comment(lib,"ws2_32.lib") #pragma comment(lib,"iphlpapi.lib") void main() { /* typedef struct _IP_ADAPTER_INFO { struct _IP_ADAPTER_INFO* Next; DWORD ComboIndex; char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4]; char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4]; UINT AddressLength; BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH]; DWORD Index; UINT Type; UINT DhcpEnabled; PIP_ADDR_STRING CurrentIpAddress; IP_ADDR_STRING IpAddressList; IP_ADDR_STRING GatewayList; IP_ADDR_STRING DhcpServer; BOOL HaveWins; IP_ADDR_STRING PrimaryWinsServer; IP_ADDR_STRING SecondaryWinsServer; time_t LeaseObtained; time_t LeaseExpires; } IP_ADAPTER_INFO, *PIP_ADAPTER_INFO; */ IP_ADAPTER_INFO* pAdapterInfo; PIP_ADAPTER_INFO pAdapter; ULONG bufferLength; DWORD status; pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO)); bufferLength = sizeof(IP_ADAPTER_INFO); //根据该函数先故意失败获取,得到链表真正大小 if (GetAdaptersInfo(pAdapterInfo, &bufferLength)!=ERROR_SUCCESS) { free(pAdapterInfo); pAdapterInfo = (IP_ADAPTER_INFO*)malloc(bufferLength); } //获取网卡信息结构体第一个节点. if (GetAdaptersInfo(pAdapterInfo, &bufferLength) != ERROR_SUCCESS) { printf("error\n"); } //因为网卡之间链接成一个单链表,通过一个指针遍历该链表 pAdapter = pAdapterInfo; while (pAdapter) { printf("网卡名: %s\n", pAdapter->AdapterName); printf("网卡描述: %s\n", pAdapter->Description); printf("网卡MAC地址: "); for (int i = 0; i < pAdapter->AddressLength; i++) { if (i == (pAdapter->AddressLength - 1)) printf("%.2x\n", (int)pAdapter->Address[i]); else printf("%.2x-", (int)pAdapter->Address[i]); } printf("ip地址: %s\n", pAdapter->IpAddressList.IpAddress.String); printf("子网掩码: %s\n", pAdapter->IpAddressList.IpMask.String); printf("网关: %s\n", pAdapter->GatewayList.IpAddress.String); if (pAdapter->DhcpEnabled) { printf("启用DHCP: 是\n"); printf("DHCP服务器地址: %s\n\n", pAdapter->DhcpServer.IpAddress.String); } else { printf("启用DHCP: 否\n\n"); } pAdapter = pAdapter->Next; } if (pAdapterInfo) free(pAdapterInfo); getchar(); }

 

 

 

获取dns相关信息:

#include<WinSock2.h>
#include<IPHlpApi.h>
#include<stdio.h>


#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"iphlpapi.lib")


void main()
{    
       ULONG bufferLength;
    DWORD status;

    FIXED_INFO* fixedInfo;
    IP_ADDR_STRING* pIpAddr;
    /*
    typedef struct {
    char HostName[MAX_HOSTNAME_LEN + 4] ;
    char DomainName[MAX_DOMAIN_NAME_LEN + 4];
    PIP_ADDR_STRING CurrentDnsServer;
    IP_ADDR_STRING DnsServerList;
    UINT NodeType;
    char ScopeId[MAX_SCOPE_ID_LEN + 4];
    UINT EnableRouting;
    UINT EnableProxy;
    UINT EnableDns;
    } FIXED_INFO_W2KSP1, *PFIXED_INFO_W2KSP1;
    */

    fixedInfo = (FIXED_INFO*)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
    bufferLength = sizeof(FIXED_INFO);
    if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(fixedInfo, &bufferLength))
    {
        GlobalFree(fixedInfo);
        fixedInfo = (FIXED_INFO*)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
    }
    if (GetNetworkParams(fixedInfo, &bufferLength) != ERROR_SUCCESS)
    {
        printf("error\n");
    }
    else
    {
        printf("主机名: %s\n", fixedInfo->HostName);
        printf("域名: %s\n", fixedInfo->HostName);
        char* nodeType;
        switch (fixedInfo->NodeType)
        {
        case BROADCAST_NODETYPE:
            nodeType = "broadcase node";
            break;
        case PEER_TO_PEER_NODETYPE:
            nodeType = "peertopeer node";
            break;
        case MIXED_NODETYPE:
            nodeType = "mixed node";
            break;
        case HYBRID_NODETYPE:
            nodeType = "hybrid node";
            break;
        default:
            nodeType = "unkowntype node";
            break;
        }
        printf("节点类型    %d - %s\n", fixedInfo->NodeType, nodeType);
        printf("是否启用路由功能     %s\n", fixedInfo->EnableRouting ? "" : "");
        printf("是否启用arp代理功能     %s\n", fixedInfo->EnableProxy ? "" : "");
        printf("是否启用dns服务器     %s\n", fixedInfo->EnableDns ? "" : "");
        printf("dns服务器列表\n");
        printf("%s\n", fixedInfo->DnsServerList.IpAddress.String);
        pIpAddr = fixedInfo->DnsServerList.Next;
        while (pIpAddr)
        {
            printf("%s\n", pIpAddr->IpAddress.String);
            pIpAddr = pIpAddr->Next;
        }

    }
    getchar();





}

 

获取网络接口数量API: GetNumberOfInterfaces(pdwNumIf)

转载于:https://www.cnblogs.com/freesec/p/6193957.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值