获取网卡信息
2012-03-19 22:12
330 查看
在Windows中获取网络适配器相关信息是很方便的,可以通过GetAdaptersInfo函数获取并保存在IP_ADAPTER_INFO结构体中,再对结构休进行解析得到适配器信息。有关函数和结构体的详细说明可以参考MSDN,这里就不再复述。下面直接给出具体示例程序。
结构体中还有其他适配器信息在上述程序中没有解析,但是都可以根据需要做相应解析。
//引入头文件 #include <winsock2.h> #include <stdio.h> #include <IPHlpApi.h> #pragma comment(lib, "Iphlpapi.lib") #pragma comment(lib, "WS2_32.lib") int main() { PIP_ADAPTER_INFO pAdapterInfo = NULL; ULONG uLen = 0; //为适配器结构申请内存 GetAdaptersInfo(pAdapterInfo, &uLen); pAdapterInfo = (PIP_ADAPTER_INFO)GlobalAlloc(GPTR, uLen); //取得本地适配器结构信息 DWORD dwRes = GetAdaptersInfo(pAdapterInfo, &uLen); if(ERROR_SUCCESS == dwRes) //正确获取适配器信息 { //解析适配器结构体输出适配器信息 while(pAdapterInfo) { //适配器名称 printf("The name of the adapter : %s\n", pAdapterInfo->AdapterName); //适配器描述信息 printf("The description for the adapter : %s\n", pAdapterInfo->Description); //硬件地址长度 printf("The length of the hardware address for the adapter : %d\n", pAdapterInfo->AddressLength); //硬件地址 printf("The hardware address of adapter : "); for(UINT i = 0; i < pAdapterInfo->AddressLength; i++) { if(i == pAdapterInfo->AddressLength - 1) { printf("%02x", pAdapterInfo->Address[i]); } else { printf("%02x-", pAdapterInfo->Address[i]); } } IP_ADDR_STRING *pAddrString = &(pAdapterInfo->IpAddressList); while(pAddrString) { //适配器配置的IP地址 printf("The IP address for this adapter : %s\n", pAddrString->IpAddress.String); //适配器配置的子网掩码地址 printf("The mask address for this adapter : %s\n", pAddrString->IpMask.String); pAddrString = pAddrString->Next; } pAddrString = &(pAdapterInfo->GatewayList); while(pAddrString) { //适配器配置的默认网关地址 printf("The default gateway for this adapter : %s\n", pAddrString->IpAddress.String); pAddrString = pAddrString->Next; } pAdapterInfo = pAdapterInfo->Next; } } //未能正确获取适配器信息时的返回值,打印的内容是对应的错误信息描述 else if(ERROR_BUFFER_OVERFLOW == dwRes) { printf("The buffer size indicated by the pOutBufLen parameter is too \n"); printf("small to hold the adapter information. The pOutBufLen parameter points to the required size.\n"); } else if(ERROR_INVALID_DATA == dwRes) { printf("The pOutBufLen parameter is NULL, or the calling process does not "); printf("have read/write access to the memory pointed to by pOutBufLen, or "); printf("the calling process does not have write access to the memory pointed to by the pAdapterInfo parameter.\n"); } else if(ERROR_NO_DATA == dwRes) { printf("No adapter information exists for the local computer.\n"); } else if(ERROR_NOT_SUPPORTED == dwRes) { printf("GetAdaptersInfo is not supported by the operating system running on the local computer.\n"); } else { printf("Can't get information about adapter.\n"); } return 0; }
结构体中还有其他适配器信息在上述程序中没有解析,但是都可以根据需要做相应解析。
相关文章推荐
- 网络信息获取函数和网卡信息获取
- java获取本地网卡信息
- GetAdaptersInfo 获取网卡配置和Ip地址信息
- Linux下获取所有可用网卡信息
- windows平台下 c++获取 系统版本 网卡 内存 CPU 硬盘 显卡信息<转>
- 使用PDH性能计数器获取CPU使用率网卡信息等
- 驱动下完美获取网卡信息方法
- delphi 获取网卡信息(支持多网卡)
- wince中获取有线网卡和无线网卡ip信息的实例
- windows获取网卡信息并判断是否是物理网卡 网络适配器的判断
- C++ 中通过GetAdaptersInfo获取网卡配置和Ip地址信息
- VC++获取串口设备信息,CPU ID,硬盘ID,网卡MAC等机器硬件信息,用于加密
- 网络信息获取函数和网卡信息获取
- VS2012 C++ 中通过GetAdaptersInfo获取多网卡,多IP地址信息
- Windows SDK获取网卡信息
- C++ 中通过GetAdaptersInfo获取网卡配置和Ip地址信息
- java 获取本机硬件信息 cpu 内存 硬盘 网卡等
- 获取网卡信息
- 一个专门获取网卡信息的 没有Iphlpapi.h与Iphlpapi.lib,怎么处理?
- c# 获取所有网卡的信息(IP。MAC)