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;
}
//
#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;
}
相关文章推荐
- 获取路由器网络信息(mac,ip,router,dns,dhcp clients)
- 怎么查询电脑mac地址,IP,DNS等详细信息
- C# System.Net.NetworkInformation 命名空间学习之NetworkInterface 类获取IP,DNS,Gateway,Mask, MAC等网络相关信息
- C# 获取适配器网络连接IP地址,子网掩码,DNS,数据包等信息
- linux 系统获取网络ip, mask, gateway, dns信息小程序
- linux 系统获取网络ip, mask, gateway, dns信息小程序
- 使用WMI控制网络适配器的类(获取所有启用的网络适配器及其信息。设置IP、子网掩码,网关,DNS)
- linux 系统获取网络ip, mask, gateway, dns信息小程序
- 计算机硬件通用功能类:硬件信息控制器(主机名,cpu编号,网卡地址,MAC地址,主硬盘编号,ip地址,获取最大线程数,验证服务IP)
- Android系统信息获取 之六:网络连接状态信息获取
- 活用Ping命令 轻松获取网络连接与系统信息
- IOS获取自身网络信息的方法,包括自身ip,路由地址等
- 获取wifi的ip,子网掩码,网关,dns之类的信息
- 原来无线可用,重装机子后,无线网络连接一直提示正在获取IP!
- java代码 调用dos的ipconfig /all 命令,获取网卡详细信息
- CentOS IP丢失,切换了网络连接导致的vmnet8未启用dhcp
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第十七节 设置TCP连接状态
- 获取计算机网络信息,包含IP,MAC
- C#如何使用GetTcpTable API获取TCP/IP连接信息
- VC++获取本机所有IP和掩码信息