您的位置:首页 > 理论基础 > 计算机网络

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

2013-05-30 15:40 691 查看
// 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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐