widnows下利用api获取IP地址,mac地址等网络信息

#include<Iphlpapi.h>
#pragma comment(lib, "Iphlpapi.lib")

void GetAdpaterAddress()
{	
    PIP_ADAPTER_INFO pAdapterInfo;
    PIP_ADAPTER_INFO pAdapter = NULL;
    DWORD dwRetVal = 0;
    int i = 0;

    pAdapterInfo = (IP_ADAPTER_INFO *)malloc( sizeof(IP_ADAPTER_INFO) );
    ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);

    if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
    {
        free(pAdapterInfo);
        pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
    }

    if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
    {
        pAdapter = pAdapterInfo;
        while (pAdapter)
        {
            for(i=0;i<6;i++)
            {
                if(pAdapter->Address[i] < 0x10)
                {
                    printf("0%x ", pAdapter->Address[i]);
                }
                else
                {
                    printf("%x ", pAdapter->Address[i]);
                }
            }
            printf("\n\tIP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String);
            printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);
            printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);

            if (pAdapter->DhcpEnabled)
            {
                printf("\tDHCP Enabled: \tYes\n");
                printf("\t\tDHCP Server: \t%s\n", pAdapter->DhcpServer.IpAddress.String);
                printf("\tLease Obtained: \t%ld\n", pAdapter->LeaseObtained);
            }
            else
            {
                printf("\tDHCP Enabled: \tNo\n");
            }

            if (pAdapter->HaveWins)
            {
                printf("\tHave Wins: \tYes\n");
                printf("\t\tPrimary Wins Server: \t%s\n", pAdapter->PrimaryWinsServer.IpAddress.String);
                printf("\t\tSecondary Wins Server: \t%s\n", pAdapter->SecondaryWinsServer.IpAddress.String);
            }
            else
            {
                printf("\tHave Wins: \tNo\n");
            }

            printf("\n");

            pAdapter = pAdapter->Next;
        }
    }
    else {
        printf("Call to GetAdaptersInfo failed.\n");
    }
}

 

下面利用windows API GetAdaptersAddresses来获取

#include<WinSock2.h>
#include<WS2tcpip.h>
#include<iostream>
#include<IPHlpApi.h>
using namespace std;

int main()
{
	PIP_ADAPTER_ADDRESSES pAddresses = nullptr;
	IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer = nullptr;
	ULONG outBufLen = 0;
	DWORD dwRetVal = 0;
	char buff[100];
	DWORD bufflen=100;
	int i;
	// 获取outBufLen的长度然后动态分配
	GetAdaptersAddresses(AF_UNSPEC,0, NULL, pAddresses,&outBufLen);

	pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(outBufLen);

	if ((dwRetVal = GetAdaptersAddresses(AF_INET,GAA_FLAG_SKIP_ANYCAST,NULL,pAddresses,&outBufLen)) == NO_ERROR) {

			while (pAddresses) {
				printf("%S, %.2x-%.2x-%.2x-%.2x-%.2x-%.2x: \n",
					pAddresses->FriendlyName,
					pAddresses->PhysicalAddress[0],pAddresses->PhysicalAddress[1],
					pAddresses->PhysicalAddress[2],pAddresses->PhysicalAddress[3],
					pAddresses->PhysicalAddress[4],pAddresses->PhysicalAddress[5]);
	
				PIP_ADAPTER_UNICAST_ADDRESS pUnicast = pAddresses->FirstUnicastAddress;
				pDnServer = pAddresses->FirstDnsServerAddress;
	
				if(pDnServer)
				{
					sockaddr_in *sa_in = (sockaddr_in *)pDnServer->Address.lpSockaddr;
					printf("DNS:%s\n",inet_ntop(AF_INET,&(sa_in->sin_addr),buff,bufflen));
				}
				if (pAddresses->OperStatus == IfOperStatusUp)
				{
					printf("Status: active\n");
				}
				else
				{
					printf("Status: deactive\n");
				}
	
				
				for (i = 0; pUnicast != NULL; i++)
				{
					if (pUnicast->Address.lpSockaddr->sa_family == AF_INET)
					{
						sockaddr_in *sa_in = (sockaddr_in *)pUnicast->Address.lpSockaddr;
						printf("IPV4 Unicast Address:%s\n",inet_ntop(AF_INET,&(sa_in->sin_addr),buff,bufflen));
					}
					else if (pUnicast->Address.lpSockaddr->sa_family == AF_INET6)
					{
						sockaddr_in6 *sa_in6 = (sockaddr_in6 *)pUnicast->Address.lpSockaddr;
						printf("IPV6:%s\n",inet_ntop(AF_INET6,&(sa_in6->sin6_addr),buff,bufflen));
					}
					else
					{
						printf("\tUNSPEC");
					}
					pUnicast = pUnicast->Next;
				}
				printf("Number of Unicast Addresses: %d\n", i);
				pAddresses = pAddresses->Next;
			}
	}
	else {
		LPVOID lpMsgBuf;
		printf("Call to GetAdaptersAddresses failed.\n");
		if (FormatMessage(
			FORMAT_MESSAGE_ALLOCATE_BUFFER |
			FORMAT_MESSAGE_FROM_SYSTEM |
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			dwRetVal,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
			(LPTSTR) &lpMsgBuf,
			0,
			NULL )) {
				printf("\tError: %s", lpMsgBuf);
		}
		LocalFree( lpMsgBuf );
	}
	free(pAddresses);

	return 0;
}



 

你可能感兴趣的:(widnows下利用api获取IP地址,mac地址等网络信息)