vc 获取网络连接的ip,mac地址,dns,dhcp等详细信息

// NetWorkConnectionInfos.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdlib.h>
#include<iostream>
#include<windows.h>
#include<stdio.h>
//#include<Ras.h>
//#include<Raserror.h>

#include <winsock2.h>
#include <iphlpapi.h>
#pragma comment(lib,"Iphlpapi.lib")
//#pragma comment(lib,"WS2_32.LIB")

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 //EnumAllNetWorkContents();

 /*WSADATA wsaData;
 int Ret;

 // Initialize Winsock version 2.2
 if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
 {
   // NOTE: Since Winsock failed to load we cannot use
   // WSAGetLastError to determine the specific error for
   // why it failed. Instead we can rely on the return
   // status of WSAStartup.

   printf("WSAStartup failed with error %d/n", Ret);
   return 0;
 }*/
   
 // Setup Winsock communication code here

    PIP_ADAPTER_INFO pAdapterInfo;
    PIP_ADAPTER_INFO pAdapter = NULL;
 ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
 pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulOutBufLen);
 DWORD dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen);
 // 第一次调用GetAdapterInfo获取ulOutBufLen大小
 if (dwRetVal == ERROR_BUFFER_OVERFLOW)
 {
  free(pAdapterInfo);
  pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
  dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen);
 }
 if (dwRetVal == NO_ERROR)
 {
  pAdapter = pAdapterInfo;

  //DNS,pIpAdapterInfo为网卡适配器信息结构PIP_ADAPTER_INFO
  IP_PER_ADAPTER_INFO* pPerAdapt = NULL;
  ULONG ulLen = 0;
  int err = 0;
  while (pAdapter)
  {
   err = GetPerAdapterInfo(pAdapter->Index,pPerAdapt,&ulLen);
   if(ERROR_BUFFER_OVERFLOW == err)
   {
    printf("*************relloac memory***********\n");
       pPerAdapt = (IP_PER_ADAPTER_INFO*) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,ulLen);
                err = GetPerAdapterInfo(pAdapter->Index,pPerAdapt,&ulLen);
   }

   printf("Adapter Name: \t%s\n", pAdapter->AdapterName);
   printf("Adapter Desc: \t%s\n", pAdapter->Description);
   printf("MAC Addr: \t%02x-%02x-%02x-%02x-%02x-%02x\n",
   pAdapter->Address[0],
   pAdapter->Address[1],
   pAdapter->Address[2],
   pAdapter->Address[3],
   pAdapter->Address[4],
   pAdapter->Address[5]);
   printf("IP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String);
   printf("IP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);
   printf("Gateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);

   printf("Dhcp Address: \t%s\n", pAdapter->DhcpServer.IpAddress.String);
   
   //DNS
   if(err == ERROR_SUCCESS)
   {
    IP_ADDR_STRING* pNext = &(pPerAdapt->DnsServerList);
   
    if(pNext && strcmp(pNext->IpAddress.String,"") != 0)
    {
     
     printf("Dns Address: \t%s\n", pNext->IpAddress.String);
     if(pNext = pNext->Next)
     {
         printf("\t\t%s\n",pNext->IpAddress.String);
     }   
    }
   }

   //比较wins ip值是否为空
   if(strcmp(pAdapter->PrimaryWinsServer.IpAddress.String,"0.0.0.0") != 0)
   {
       printf("Wins Address: \t%s\n", pAdapter->PrimaryWinsServer.IpAddress.String);
   }
   else
   {
                printf("Wins Address: \t\t\n");
   }
   
   printf("\n");
           
   pAdapter = pAdapter->Next;
  }// end while

  //释放内存
  HeapFree(GetProcessHeap(),HEAP_ZERO_MEMORY,pPerAdapt);
 }
 else
 {
  printf("Call to GetAdaptersInfo failed.\n");
 }


   // When your application is finished call WSACleanup
   /*if (WSACleanup() == SOCKET_ERROR)
   {
      printf("WSACleanup failed with error %d/n", WSAGetLastError());
   }*/

 system("pause");
 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值