欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

获取本机网卡信息

发布时间:2025/7/25 编程问答 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 获取本机网卡信息 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

/*
获取本地ip地址,子网掩码,默认网关,mac地址,网络适配器信息,hostname,域名dns,网络接口信息*/ #include<WinSock2.h> #include<IPHlpApi.h> #include<stdio.h>#pragma comment(lib,"ws2_32.lib") #pragma comment(lib,"iphlpapi.lib")void main() {/*typedef struct _IP_ADAPTER_INFO {struct _IP_ADAPTER_INFO* Next;DWORD ComboIndex;char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];UINT AddressLength;BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];DWORD Index;UINT Type;UINT DhcpEnabled;PIP_ADDR_STRING CurrentIpAddress;IP_ADDR_STRING IpAddressList;IP_ADDR_STRING GatewayList;IP_ADDR_STRING DhcpServer;BOOL HaveWins;IP_ADDR_STRING PrimaryWinsServer;IP_ADDR_STRING SecondaryWinsServer;time_t LeaseObtained;time_t LeaseExpires; } IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;*/IP_ADAPTER_INFO* pAdapterInfo;PIP_ADAPTER_INFO pAdapter;ULONG bufferLength;DWORD status;pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));bufferLength = sizeof(IP_ADAPTER_INFO);//根据该函数先故意失败获取,得到链表真正大小if (GetAdaptersInfo(pAdapterInfo, &bufferLength)!=ERROR_SUCCESS){free(pAdapterInfo);pAdapterInfo = (IP_ADAPTER_INFO*)malloc(bufferLength);}//获取网卡信息结构体第一个节点.if (GetAdaptersInfo(pAdapterInfo, &bufferLength) != ERROR_SUCCESS){printf("error\n");}//因为网卡之间链接成一个单链表,通过一个指针遍历该链表pAdapter = pAdapterInfo;while (pAdapter){printf("网卡名: %s\n", pAdapter->AdapterName);printf("网卡描述: %s\n", pAdapter->Description);printf("网卡MAC地址: ");for (int i = 0; i < pAdapter->AddressLength; i++){if (i == (pAdapter->AddressLength - 1))printf("%.2x\n", (int)pAdapter->Address[i]);elseprintf("%.2x-", (int)pAdapter->Address[i]);}printf("ip地址: %s\n", pAdapter->IpAddressList.IpAddress.String);printf("子网掩码: %s\n", pAdapter->IpAddressList.IpMask.String);printf("网关: %s\n", pAdapter->GatewayList.IpAddress.String);if (pAdapter->DhcpEnabled){printf("启用DHCP: 是\n");printf("DHCP服务器地址: %s\n\n", pAdapter->DhcpServer.IpAddress.String);}else{printf("启用DHCP: 否\n\n");}pAdapter = pAdapter->Next;}if (pAdapterInfo)free(pAdapterInfo);getchar();}

 

 

 

获取dns相关信息:

#include<WinSock2.h> #include<IPHlpApi.h> #include<stdio.h>#pragma comment(lib,"ws2_32.lib") #pragma comment(lib,"iphlpapi.lib")void main() { ULONG bufferLength;DWORD status;FIXED_INFO* fixedInfo;IP_ADDR_STRING* pIpAddr;/*typedef struct {char HostName[MAX_HOSTNAME_LEN + 4] ;char DomainName[MAX_DOMAIN_NAME_LEN + 4];PIP_ADDR_STRING CurrentDnsServer;IP_ADDR_STRING DnsServerList;UINT NodeType;char ScopeId[MAX_SCOPE_ID_LEN + 4];UINT EnableRouting;UINT EnableProxy;UINT EnableDns;} FIXED_INFO_W2KSP1, *PFIXED_INFO_W2KSP1;*/fixedInfo = (FIXED_INFO*)GlobalAlloc(GPTR, sizeof(FIXED_INFO));bufferLength = sizeof(FIXED_INFO);if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(fixedInfo, &bufferLength)){GlobalFree(fixedInfo);fixedInfo = (FIXED_INFO*)GlobalAlloc(GPTR, sizeof(FIXED_INFO));}if (GetNetworkParams(fixedInfo, &bufferLength) != ERROR_SUCCESS){printf("error\n");}else{printf("主机名: %s\n", fixedInfo->HostName);printf("域名: %s\n", fixedInfo->HostName);char* nodeType;switch (fixedInfo->NodeType){case BROADCAST_NODETYPE:nodeType = "broadcase node";break;case PEER_TO_PEER_NODETYPE:nodeType = "peertopeer node";break;case MIXED_NODETYPE:nodeType = "mixed node";break;case HYBRID_NODETYPE:nodeType = "hybrid node";break;default:nodeType = "unkowntype node";break;}printf("节点类型 %d - %s\n", fixedInfo->NodeType, nodeType);printf("是否启用路由功能 %s\n", fixedInfo->EnableRouting ? "" : "");printf("是否启用arp代理功能 %s\n", fixedInfo->EnableProxy ? "" : "");printf("是否启用dns服务器 %s\n", fixedInfo->EnableDns ? "" : "");printf("dns服务器列表\n");printf("%s\n", fixedInfo->DnsServerList.IpAddress.String);pIpAddr = fixedInfo->DnsServerList.Next;while (pIpAddr){printf("%s\n", pIpAddr->IpAddress.String);pIpAddr = pIpAddr->Next;}}getchar();}

 

获取网络接口数量API: GetNumberOfInterfaces(pdwNumIf)

转载于:https://www.cnblogs.com/freesec/p/6193957.html

总结

以上是生活随笔为你收集整理的获取本机网卡信息的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。