MSDN例子 获取网卡、ip等信息
来源:互联网 发布:个人淘宝信誉怎么提升 编辑:程序博客网 时间:2024/04/30 02:33
#include <winsock2.h>#include <iphlpapi.h>#include <stdio.h>#include <stdlib.h>#pragma comment(lib, "IPHLPAPI.lib")#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))/* Note: could also use malloc() and free() */int __cdecl main(){ /* Declare and initialize variables */// It is possible for an adapter to have multiple// IPv4 addresses, gateways, and secondary WINS servers// assigned to the adapter. //// Note that this sample code only prints out the // first entry for the IP address/mask, and gateway, and// the primary and secondary WINS server for each adapter. PIP_ADAPTER_INFO pAdapterInfo; PIP_ADAPTER_INFO pAdapter = NULL; DWORD dwRetVal = 0; UINT i;/* variables used to print DHCP time info */ struct tm newtime; char buffer[32]; 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 1; }// 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 1; } } if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) { pAdapter = pAdapterInfo; while (pAdapter) { printf("\tComboIndex: \t%d\n", pAdapter->ComboIndex); printf("\tAdapter Name: \t%s\n", pAdapter->AdapterName); printf("\tAdapter Desc: \t%s\n", pAdapter->Description); printf("\tAdapter Addr: \t"); for (i = 0; i < pAdapter->AddressLength; i++) { if (i == (pAdapter->AddressLength - 1)) printf("%.2X\n", (int) pAdapter->Address[i]); else printf("%.2X-", (int) pAdapter->Address[i]); } printf("\tIndex: \t%d\n", pAdapter->Index); printf("\tType: \t"); switch (pAdapter->Type) { case MIB_IF_TYPE_OTHER: printf("Other\n"); break; case MIB_IF_TYPE_ETHERNET: printf("Ethernet\n"); break; case MIB_IF_TYPE_TOKENRING: printf("Token Ring\n"); break; case MIB_IF_TYPE_FDDI: printf("FDDI\n"); break; case MIB_IF_TYPE_PPP: printf("PPP\n"); break; case MIB_IF_TYPE_LOOPBACK: printf("Lookback\n"); break; case MIB_IF_TYPE_SLIP: printf("Slip\n"); break; default: printf("Unknown type %ld\n", pAdapter->Type); break; } printf("\tIP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String); printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String); printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String); printf("\t***\n"); if (pAdapter->DhcpEnabled) { printf("\tDHCP Enabled: Yes\n"); printf("\t DHCP Server: \t%s\n", pAdapter->DhcpServer.IpAddress.String); printf("\t Lease Obtained: "); /* Display local time */ error = _localtime32_s(&newtime, (__time32_t*) &pAdapter->LeaseObtained); if (error) printf("Invalid Argument to _localtime32_s\n"); else { // Convert to an ASCII representation error = asctime_s(buffer, 32, &newtime); if (error) printf("Invalid Argument to asctime_s\n"); else /* asctime_s returns the string terminated by \n\0 */ printf("%s", buffer); } printf("\t Lease Expires: "); error = _localtime32_s(&newtime, (__time32_t*) &pAdapter->LeaseExpires); if (error) printf("Invalid Argument to _localtime32_s\n"); else { // Convert to an ASCII representation error = asctime_s(buffer, 32, &newtime); if (error) printf("Invalid Argument to asctime_s\n"); else /* asctime_s returns the string terminated by \n\0 */ printf("%s", buffer); } } else printf("\tDHCP Enabled: No\n"); if (pAdapter->HaveWins) { printf("\tHave Wins: Yes\n"); printf("\t Primary Wins Server: %s\n", pAdapter->PrimaryWinsServer.IpAddress.String); printf("\t Secondary Wins Server: %s\n", pAdapter->SecondaryWinsServer.IpAddress.String); } else printf("\tHave Wins: No\n"); pAdapter = pAdapter->Next; printf("\n"); } } else { printf("GetAdaptersInfo failed with error: %d\n", dwRetVal); } if (pAdapterInfo) FREE(pAdapterInfo); return 0;}
阅读全文
0 0
- MSDN例子 获取网卡、ip等信息
- c#获取网卡的ip等信息
- c#获取和设置网卡ip/dns等信息
- Qt获取IP地址、MAC地址等网卡信息,区分本地网卡、无线网卡和虚拟网卡
- 获取本机所有网卡的网卡名、网卡描述、网卡MAC地址、网卡IP、网卡类型等信息及网线是否插入状态
- 如何获取网卡相关信息(包括mac, ip, mask, gateway等)?---利用GetAdaptersInfo
- VB6获取本机网络适配器信息(网卡名称、IP地址、子网掩码、默认网关等)
- 网络公共函数(大小端字节转换、网卡/ip信息获取等。)
- 获取IP地址及网卡信息
- 获取ip地址及网卡信息
- TCP/IP实验获取主机网卡信息
- C#获取CPU网卡等硬件信息
- MSDN例子获取操作系统版本信息Getting the System Version
- Qt: 取得网卡的MAC地址, IP等信息
- Qt: 取得网卡的MAC地址, IP等信息
- C# 如何获取IP用户名等信息
- 获取本机IP等信息
- linux下获取IP等信息函数
- hibernate-关系映射(一对一(单向、双向))-8
- EasyDarwin开源流媒体服务器中一种实现对作用域内new对象自动释放的方法(值得借鉴)
- lowlevel_init.S 分析
- IMWeb提升营Day5 | 训练题27:字符串的排列
- L1-009. N个数求和
- MSDN例子 获取网卡、ip等信息
- Android adb命令介绍
- MySQLTuner--使用方法
- Raid 2.0 及重构原理举例
- Uboot启动分析笔记-----Stage1(start.S与lowlevel_init.S详解)
- [LeetCode]108. Convert Sorted Array to Binary Search Tree(升序数组转化为平衡二叉树)
- 毫秒转换分钟和秒
- java开发系统内核:像Linux一样使用中断实现内核API
- uboot-的start.S详细注解及分析