win32 获取本机网卡信息(MAC地址,IP地址等)
由于一个需求需要获取网卡的MAC地址,就搜了一下,大部分都是COPY来COPY去的一些代码,有很多甚至不能直接运行或有还有内存泄漏。自己查了一下MSDN然后封装了一下;
需要注意,一个机器可能有多个网卡(包含虚拟网卡),一个网卡可能有多个IP
主要用到了 GetAdaptersInfo 这个API,可以自己去MSDN查一下
下面是我的封装,可以直接用,也可以按照你的需求改,我这里只需要获取MAC地址,IP,和描述就够了,通过描述可以筛选掉VMware和VPN生成的虚拟网卡
// 头文件包含
#include "stdafx.h"
#include <WinSock2.h>
#include <Iphlpapi.h>
#include <iostream>
#include <string>
#include <map>
#include <vector> #pragma comment(lib,"iphlpapi.lib") #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) typedef struct _MyAdpterInfo
{
std::vector<std::string> Ip;
std::string MacAddress;
std::string Description;
std::string Name;
UINT Type;
}MyAdpterInfo; int MyGetAdptersInfo(std::vector<MyAdpterInfo>& adpterInfo)
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = ;
UINT i; /* variables used to print DHCP time info */
struct tm newtime;
char buffer[];
errno_t error; ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL)
{
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return -;
}
// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
{
FREE(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(ulOutBufLen);
if (pAdapterInfo == NULL)
{
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return -; // error data return
}
} if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
{
pAdapter = pAdapterInfo;
while (pAdapter)
{
MyAdpterInfo info;
info.Name = std::string(pAdapter->AdapterName);
info.Description = std::string(pAdapter->Description);
info.Type = pAdapter->Type;
char buffer[];
sprintf_s(buffer, "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x", pAdapter->Address[],
pAdapter->Address[], pAdapter->Address[], pAdapter->Address[],
pAdapter->Address[], pAdapter->Address[]);
info.MacAddress = std::string(buffer);
IP_ADDR_STRING *pIpAddrString = &(pAdapter->IpAddressList);
do
{
info.Ip.push_back(std::string(pIpAddrString->IpAddress.String));
pIpAddrString = pIpAddrString->Next;
} while (pIpAddrString);
pAdapter = pAdapter->Next;
adpterInfo.push_back(info);
}
if (pAdapterInfo)
FREE(pAdapterInfo);
return ; // normal return
}
else
{
if (pAdapterInfo)
FREE(pAdapterInfo);
printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);
return ; // null data return
}
} int _tmain(int argc, _TCHAR* argv[])
{
std::vector<MyAdpterInfo> AdptersInfo;
int ret = MyGetAdptersInfo(AdptersInfo);
for (int i = ; i < AdptersInfo.size(); ++i)
{
std::cout << "Adpter name: " << AdptersInfo[i].Name << std::endl;
std::cout << "Adpter description: " << AdptersInfo[i].Description << std::endl;
std::cout << "Adpter MAC: " << AdptersInfo[i].MacAddress << std::endl;
std::cout << "Adpter IP: ";
for (int j = ; j < AdptersInfo[i].Ip.size(); ++j)
{
if (j != )
{
std::cout << ", ";
}
std::cout << AdptersInfo[i].Ip[j];
}
std::cout << std::endl << std::endl << std::endl;
}
return ;
}
运行结果

win32 获取本机网卡信息(MAC地址,IP地址等)的更多相关文章
- C++获取本机用于连接的IP地址
最近写个程序需要获取本机用于连接的IP地址,经过很多的尝试后,最终使用的方法如下: 使用cmd命令 netstat | findstr “192.168.6.66:3333” > D:\ ...
- JAVA获取本机的MAC地址
/** * 获取本机的Mac地址 * @return */ public String getMac() { InetAddress ia; byte[] mac = null; try { // 获 ...
- Delphi获取本机的MAC地址
Delphi获取本机的MAC地址: uses NB30; function GetAdaPterInfo(lana: Char): string; var Adapter: TAdapterS ...
- JAVA获取客户端请求的当前网络ip地址(附:Nginx反向代理后获取客户端请求的真实IP)
1. JAVA获取客户端请求的当前网络ip地址: /** * 获取客户端请求的当前网络ip * @param request * @return */ public static String get ...
- 【liunx】使用xshell连接虚拟机上的CentOS 7,使用xhell连接本地虚拟机上的Ubuntu, 获取本地虚拟机中CentOS 7的IP地址,获取本地虚拟机中Ubuntu 的IP地址,Ubuntu开启22端口
注意,如果想用xshell去连接本地虚拟机中的linux系统,需要本地虚拟机中的系统是启动的才能连接!!!!! ============================================ ...
- Java 获取当前项目所在服务器的 IP 地址
java中获取当前服务器地址主要使用到InetAddress这个类 public static void main(String[] args) { try { //用 getLocalHost() ...
- javascript获取当前用户访问的宽带IP地址
javascript获取当前用户访问的宽带IP地址 <script src="http://pv.sohu.com/cityjson?ie=utf-8"></sc ...
- 彻底明白IP地址——IP地址的介绍
彻底明白IP地址——IP地址的介绍 [ 作者:担子 转贴自:赛迪网 点击数:9692 更新时间:2004-12-22 ] IP地址的介绍 1.IP地址的表示方法 IP地址 = ...
- 获取本机正在使用的ipv4地址(访问互联网的IP)
[转]原文地址:http://www.cnblogs.com/lijianda/p/6604651.html 1.一个电脑有多个网卡,有线的.无线的.还有vmare虚拟的两个网卡.2.就算只有一个网卡 ...
随机推荐
- iOS_触摸事件与手势识别
目 录: 一.触摸事件 1.1iOS的输入事件 1.2 触摸事件的处理 1.3 UITouch类中包含五个属性 1.4 UITouch类中包含两个成员函数 1.5响应者链 二.手势识别 2.1使用手 ...
- Python 循环的综合应用
# 循环综合应用1. # str = "hello,world" 把字符串给反转显示 str = "hello,world" temp = "&quo ...
- 2015年蓝桥杯C/C++ B组题目题解
1. 输入一个字符串,求它包含多少个单词.单词间以一个或者多个空格分开. 第一个单词前,最后一个单词后也可能有0到多个空格.比如:" abc xyz" 包含两个单词,"a ...
- jq点击切换按钮最简洁代码
<div id="swphoto"> <img src="1.jpg"> <img src="2.jpg&q ...
- centos下安装python2.7.9和pip以及数据科学常用的包
以前一直用ubantu下的python,ubantu比较卡.自己倾向于使用centos,但默认的python版本太低,所以重新装了一个python和ipython centos6.5安装python2 ...
- MapReduce-输入分片与记录
一个输入分片(split)就是一个由单个map操作来处理的输入块.每一个map操作只处理一个输入分片.每个分片被划分为若干个记录,每条记录就是一个键值对,map一个接一个地处理记录.输入分片和记录都是 ...
- 字符在内存中最终的表示形式是什么?是某种字符编码还是码位(Code Point)?
字符在内存中最终的表示形式是什么?是某种字符编码还是码位(Code Point)? 根据我的了解,编码中有三个核心概念:1. 字符集(Character Set),可以说是一个抽象概念,字符的合集2. ...
- WINSOCK 传送文件
SERVER: // send_server.cpp : Defines the entry point for the console application. // #include " ...
- Linux嵌入式 -- 内核 (arm) & 文件系统
1. make distclean 2. 配置内核 ----> 生成 .config文件 make menuconfig ARCH=arm Makefile文件分析: 3. 编译内核 mak ...
- Maven基础配置
重要网址 Maven主页:http://maven.apache.org/ Maven central repository:http://search.maven.org/ Maven aliyun ...