18,356
社区成员
发帖
与我相关
我的任务
分享
#include "Iphlpapi.h"
#pragma comment(lib,"Iphlpapi.lib")
PIP_ADAPTER_INFO info = new IP_ADAPTER_INFO;
memset(info,0,sizeof(IP_ADAPTER_INFO));
ULONG infoSize = 0;
GetAdaptersInfo(info,&infoSize);
info = (PIP_ADAPTER_INFO )new char[infoSize];
GetAdaptersInfo(info,&infoSize);
do
{ // 遍历所有适配器
if(info-> Type == MIB_IF_TYPE_ETHERNET) // 判断是否为以太网接口
{
// pAdapterInfo-> Description 是适配器描述
// pAdapterInfo-> AdapterName 是适配器名称
}
info = info-> Next;
}while(info);
delete info;
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#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: \t5d\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, &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, &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;
}
void GetMAC_IP_Getway_address(char* NIC_Name,BYTE NIC_MAC[6],char *NIC_IP,char *NIC_Getway)
{
IP_ADAPTER_INFO pNIC_Info[10]; //IP_ADAPTER_INFO为网卡信息结构体,其中AdapterName表示网卡名,Address(BYTE[8])表示MAC地址,IpAddressList.IpAddress表示ip地址
DWORD BufLen = sizeof(IP_ADAPTER_INFO)*10;
DWORD dw = GetAdaptersInfo(
pNIC_Info, // [output] 指向接收数据缓冲指针
&BufLen);// [input] 缓冲区大小
PIP_ADAPTER_INFO pNIC = NULL;
DWORD dwRetVal = 0;
pNIC = pNIC_Info;
//char *AdapterGetway=new char();
while (pNIC) {
if(strcmp(NIC_Name,pNIC->AdapterName)==0)
{
memcpy(NIC_MAC,pNIC->Address,sizeof(pNIC->Address));
strcpy(NIC_IP,pNIC->IpAddressList.IpAddress.String);
strcpy(NIC_Getway,pNIC->GatewayList.IpAddress.String);
//CString s ="";
//s.Format("%s",AdapterGetway);
//::AfxMessageBox(s);
return ;
}
pNIC = pNIC->Next;
}
return ;
}
//加上这一句
if (ERROR_BUFFER_OVERFLOW==GetAdaptersInfo(pAdapterInfo,&ulOutBufLen)) {
//pAdapterInf所指的buffer太小,重新分配
free(pAdapterInfo);
pAdapterInfo=(IP_ADAPTER_INFO *)malloc(ulOutBufLen);
}
if (ERROR_SUCCESS==GetAdaptersInfo(pAdapterInfo,&ulOutBufLen))
{
‘....
}
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter=NULL;
pAdapterInfo=(IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
ULONG ulOutBufLen=sizeof(IP_ADAPTER_INFO);
//加上这一句
if (ERROR_BUFFER_OVERFLOW==GetAdaptersInfo(pAdapterInfo,&ulOutBufLen))
{
//pAdapterInf所指的buffer太小,重新分配
free(pAdapterInfo);
pAdapterInfo=(IP_ADAPTER_INFO *)malloc(ulOutBufLen);
} if (ERROR_SUCCESS==GetAdaptersInfo(pAdapterInfo,&ulOutBufLen))
{
//函数执行成功
//获得子网掩码
pAdapter=pAdapterInfo;
CString strSubNetMask;
strSubNetMask.Format("子网掩码:%s\n",pAdapter->IpAddressList.IpMask.String);
m_show.InsertString(-1,strSubNetMask);
//获得ip地址
CString strIpAddress;
strIpAddress.Format("Ip地址:%s",pAdapter->IpAddressList.IpAddress.String);
m_show.InsertString(-1,strIpAddress);
//获得MAC地址
CString strMacAddress;
strMacAddress.Format("Mac地址:%02x-%02x-%02x-%02x-%02x-%02x",
pAdapter->Address[0],pAdapter->Address[1],
pAdapter->Address[2],pAdapter->Address[3],
pAdapter->Address[4],pAdapter->Address[5]
);
//获得网关Gateway
CString strGateway;
strGateway.Format("网关:%s",pAdapter->GatewayList.IpAddress.String);
m_show.InsertString(-1,strGateway);
m_show.InsertString(-1,"----------------------------------");
}
free(pAdapterInfo);
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter=NULL;
pAdapterInfo=(IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
ULONG ulOutBufLen=sizeof(IP_ADAPTER_INFO);
//加上这一句
if (ERROR_BUFFER_OVERFLOW==GetAdaptersInfo(pAdapterInfo,&ulOutBufLen))
{
//pAdapterInf所指的buffer太小,重新分配
free(pAdapterInfo);
pAdapterInfo=(IP_ADAPTER_INFO *)malloc(ulOutBufLen);
} if (ERROR_SUCCESS==GetAdaptersInfo(pAdapterInfo,&ulOutBufLen))
{
//函数执行成功
//获得子网掩码
pAdapter=pAdapterInfo;
CString strSubNetMask;
strSubNetMask.Format("子网掩码:%s\n",pAdapter->IpAddressList.IpMask.String);
m_show.InsertString(-1,strSubNetMask);
//获得ip地址
CString strIpAddress;
strIpAddress.Format("Ip地址:%s",pAdapter->IpAddressList.IpAddress.String);
m_show.InsertString(-1,strIpAddress);
//获得MAC地址
CString strMacAddress;
strMacAddress.Format("Mac地址:%02x-%02x-%02x-%02x-%02x-%02x",
pAdapter->Address[0],pAdapter->Address[1],
pAdapter->Address[2],pAdapter->Address[3],
pAdapter->Address[4],pAdapter->Address[5]
);
//获得网关Gateway
CString strGateway;
strGateway.Format("网关:%s",pAdapter->GatewayList.IpAddress.String);
m_show.InsertString(-1,strGateway);
m_show.InsertString(-1,"----------------------------------");
}
free(pAdapterInfo);