关于GetAdaptersInfo获得当前IP的问题?

yuqangy 2011-07-05 10:09:44
我想用GetAdaptersInfo获得网卡的当前IP。但是IP_ADAPTER_INFO参数中的成员都能获得正确的网卡描述,而CurrentIpAddress这一个成员却获取到的却是0而不是正确的IP地址,为什么 ?请大牛们帮我看一下,谢谢

代码如下:


#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;
...全文
387 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven 2011-07-14
  • 打赏
  • 举报
回复
MSDN上的例子程序:

#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;
}
信阳毛尖 2011-07-14
  • 打赏
  • 举报
回复

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 ;
}
xf_21 2011-07-12
  • 打赏
  • 举报
回复
你是什么系统?XP及后来系统的话,要用getadapteraddress 函数。
MSDN http://msdn.microsoft.com/en-us/library/aa365915%28VS.85%29.aspx

unicast address 才是网卡用于通信的IP
oyljerry 2011-07-07
  • 打赏
  • 举报
回复
API返回的结果,并不一定通过变量名称就一定是对的,可能最后实现时就是返回到链表了
GoForSky 2011-07-07
  • 打赏
  • 举报
回复

//加上这一句
if (ERROR_BUFFER_OVERFLOW==GetAdaptersInfo(pAdapterInfo,&ulOutBufLen)) {
//pAdapterInf所指的buffer太小,重新分配
free(pAdapterInfo);
pAdapterInfo=(IP_ADAPTER_INFO *)malloc(ulOutBufLen);

}
if (ERROR_SUCCESS==GetAdaptersInfo(pAdapterInfo,&ulOutBufLen))
{


‘....
}
GoForSky 2011-07-07
  • 打赏
  • 举报
回复


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);

GoForSky 2011-07-07
  • 打赏
  • 举报
回复


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);

西湖秀才 2011-07-07
  • 打赏
  • 举报
回复
1、 An adapter can have multiple IPv4 addresses assigned to it.
2、CurrentIpAddress 作为保留字,是个指针,使用时还得注意存储空间,而IpAddressList则不用管,使用简单
yuqangy 2011-07-05
  • 打赏
  • 举报
回复
...
IP不在CurrentIpAddress里面 ,而在IpAddressList静态链表里 ? ,,为啥不再CurrentIpAddress里放着。。。 搞不懂,哪位高人帮忙讲解一下
分先来先得哈。

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧