为什么我的IP_ADAPTER_INFO.CurrentIpAddress为空?

The_facE 2009-05-20 08:53:37
条件:单网卡,网络连接无误,Windows XP SP2系统

因为是单网卡,我就没考虑pIpInfo->next,因为它是NULL
但是这个程序很正常的崩了,调试结果CurrentIpAddress为空。

请问这个CurrentIpAddress到底代表什么?不是当前激活状态的ip地址吗?

为什么我看NetCfg的开源码可以这样获得呢?


//这几个宏定义先放这里
#define ALLOCATE_FROM_PROCESS_HEAP( bytes ) ::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, bytes )
#define DEALLOCATE_FROM_PROCESS_HEAP( ptr ) if( ptr ) ::HeapFree( ::GetProcessHeap(), 0, ptr )
#define REALLOC_FROM_PROCESS_HEAP( ptr, bytes ) ::HeapReAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, ptr, bytes )

//主要代码在这里
PIP_ADAPTER_INFO pIpInfo = NULL;
ULONG ulSize = 0;
CString strIpAddress;

DWORD dResult = ::GetAdaptersInfo( pIpInfo, &ulSize ); //检测大小

if( ERROR_BUFFER_OVERFLOW == dResult )
{
pIpInfo = ALLOCATE_FROM_PROCESS_HEAP( ulSize ); //申请空间
dResult = ::GetAdaptersInfo( pIpInfo, &ulSize ); //获取数据
}

if( ERROR_SUCCESS == dResult )
{
strIpAddress = pIpInfo->CurrentIpAddress->IpAddress.String; //获取current IP
}

::AfxMessageBox( strIpAddress ); //输出

...全文
269 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
blackcat242 2009-05-20
  • 打赏
  • 举报
回复
IP_ADAPTER_INFO
The IP_ADAPTER_INFO structure contains information about a particular network adapter on the local computer.

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;
Members
Next
Pointer to the next adapter in the list of adapters.
ComboIndex
Reserved.
AdapterName
Name of the adapter.
Description
Description for the adapter.
AddressLength
Length of the hardware address for the adapter.
Address
Hardware address for the adapter.
Index
Adapter index. The adapter index may change when an adapter is disabled and then enabled, or under other circumstances, and should not be considered persistent.
Type
Adapter type. The type must be of the following values:

MIB_IF_TYPE_OTHER
MIB_IF_TYPE_ETHERNET
MIB_IF_TYPE_TOKENRING
MIB_IF_TYPE_FDDI
MIB_IF_TYPE_PPP
MIB_IF_TYPE_LOOPBACK
MIB_IF_TYPE_SLIP
These values are defined in the header file IPIfCons.h.

DhcpEnabled
Specifies whether dynamic host configuration protocol (DHCP) is enabled for this adapter.
CurrentIpAddress
Reserved.

msdn上写的是保留字段,没有任何作用
The_facE 2009-05-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 oyljerry 的回复:]
先看 if( ERROR_SUCCESS == dResult )
是否成功,然后CurrentIpAddress 是否不为空等,多做一些判断
[/Quote]

谢谢两位的回答,我再说几句

1楼的师兄,ERROR_SUCCESS == dResult 当然成立了,不然我调试的时候就看出来了。我问的是CurrentIpAddress为什么是空,而不是程序为什么崩。因为我的网络没有任何问题,自然有IP在使用。我就想弄明白这个CurrentIpAddress是怎么用的。

2楼的师兄,你写的这段没提到CurrentIpAddress的问题呀,只是能兼容多网卡多IP。我不是要看所有IP,我只想明白这个CurrentIpAddress到底什么时候用,怎么用,干什么用。
Wenxy1 2009-05-20
  • 打赏
  • 举报
回复
请检查你的网卡是否配置了IP地址。
判断pIpInfo 是否为NULL,用指针时,要特别注意!
Wenxy1 2009-05-20
  • 打赏
  • 举报
回复
Listing Addresses
In the case where an IP Helper function returns a list of adapters, addresses, statistics, etc., step through the linked list to retrieve the information present.

// Get Adapters Info
PIP_ADAPTER_INFO pAdapterInfo = NULL;
PIP_ADAPTER_INFO pOriginalPtr;
ULONG ulSizeAdapterInfo = 0;
DWORD dwStatus;

// Find out how big our buffer needs to be to hold the data
dwStatus = GetAdaptersInfo(pAdapterInfo, &ulSizeAdapterInfo);
if (dwStatus == ERROR_BUFFER_OVERFLOW) {
// Allocate a buffer of the appropriate size
if (!(pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulSizeAdapterInfo))) {
printf(“\n Insufficient Memory ");
return(1);
}

// Obtain the Adapter Info
dwStatus = GetAdaptersInfo(pAdapterInfo, &ulSizeAdapterInfo);
}

if (dwStatus != ERROR_SUCCESS) {
return(1);
}

pOriginalPtr = pAdapterInfo;

if (pAdapterInfo == NULL)
printf("\n No Interfaces Present.\n");
// Step through the adapter list
while (pAdapterInfo != NULL) {

// Print the Ip Addresses
printf(" \n\n\t IpAddressList:: ");
PIP_ADDR_STRING pAddressList = &(pAdapterInfo->IpAddressList);

do {
printf("\n\t IpAddress = %hs", pAddressList->IpAddress.String);
pAddressList = pAddressList->Next;
} while (pAddressList != NULL);

// And so on with other members of the Adapter info structure…..

pAdapterInfo = pAdapterInfo->Next;
} // while(pAdapterInfo!=NULL){
}
oyljerry 2009-05-20
  • 打赏
  • 举报
回复
先看 if( ERROR_SUCCESS == dResult )
是否成功,然后CurrentIpAddress 是否不为空等,多做一些判断
The_facE 2009-05-20
  • 打赏
  • 举报
回复
呃,我刚也查了,msdn主页上写的是Reserved,可是我的msdn上写的是Specifies the current IP address for this adapter.

我又看了一下主页上的WinCE API,写的和我的msdn一样,真该死。

谢谢各位了,稍后分数送上。

18,356

社区成员

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

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