IPHelper functions - to get IP and subnet mask

atong99

New Member
Hi there,

From IPHelper documentation it says that I need to use GetAdaptersAddress for Windows XP, Vista or later. It seems GetAdaptersAddress does not give me the IP and subnet mask info of the adapter.

I used to use GetAdapterInfo() to get IP and Subnet mask. It looks like that GetAdapterInfo is still working on Vista, XP, but Microsoft IPHelper Doc says that I should use GetAdapterAddress instead. It is really confusing.

So what are the proper API functions I should use for Vista or XP?

Please help.
 

My Computer

Hi there,

From IPHelper documentation it says that I need to use GetAdaptersAddress for Windows XP, Vista or later. It seems GetAdaptersAddress does not give me the IP and subnet mask info of the adapter.

I used to use GetAdapterInfo() to get IP and Subnet mask. It looks like that GetAdapterInfo is still working on Vista, XP, but Microsoft IPHelper Doc says that I should use GetAdapterAddress instead. It is really confusing.

So what are the proper API functions I should use for Vista or XP?

Please help.

Use GetAdaptersAddress(). If you have old code that you don't particularly want to convert, GetAdapterInfo() should still work, but it's "deprecated", meaning that it's considered obsolescent and it shouldn't be used in new code.
 

My Computer

Thanks. However, can I get the subnet mask (for an ip) from GetAdaptersAddress() (the similar way like in GetAdaptersInfo) ?

I know that we can get subnet info from Winsock. It would be nice to get the subnet from GetAdaptersAddress().
 

My Computer

GetAdaptersAddress() can get Network ID and Broadcast IP with
Flags = GAA_FLAG_INCLUDE_PREFIX;


From PIP_ADAPTER_PREFIX we should be able to get all IPs. and calculate the subnet mask.

int nRet = WSAStartup(MAKEWORD(2, 2), &wsaData);
if(nRet != 0)
{
m_pLogger->WriteLog(_T(
"%s, WSStartup() failed. Eorror: %d"), sMethodName, nRet);
return ret;
}
//Get all the IP Address (Network IP, Adapter IP, Braodcast IP,
for (i = 0, pIpAdapterPrefix = pCurrAddresses->FirstPrefix;
pIpAdapterPrefix; i++, pIpAdapterPrefix = pIpAdapterPrefix->Next)
{
char szAddress[NI_MAXHOST];
char servInfo[NI_MAXSERV];
DWORD dwRet = getnameinfo(pIpAdapterPrefix->Address.lpSockaddr,
pIpAdapterPrefix->Address.iSockaddrLength,
szAddress,
sizeof(szAddress), servInfo, NI_MAXSERV,
NI_NUMERICHOST);
if (dwRet != 0)
{
//"can't convert network format to presentation format");

}

// szAddress will get IP in 0.0.0.0 format.
if(i == 0)
ipStartAddr = inet_addr(szAddress);
if(i == 2)
ipLastAddr = inet_addr(szAddress);

}
WSACleanup();

IPAddr ipMask = ntohl(ntohl(ipStartAddr) - ntohl(ipLastAddr) - 1);
adapterInfo.m_sSubnetMask = ConvertIPAddrToIPString(ipMask);

****
CString ConvertIPAddrToIPString(IPAddr ipAddress)
{
CString sIpString, sTemp;
IPAddr IpTemp;
int nbase[4] = {1, 256, 256*256, 256*256*256};
int nLen = 4;
IpTemp = ntohl(ipAddress);
for(int i = nLen-1; i >= 0; i--)
{
int nSubnetNumber;
nSubnetNumber = IpTemp/nbase;
IpTemp = IpTemp%nbase;

if(i ==0)
sTemp.Format(_T(
"%d"),nSubnetNumber);
else
sTemp.Format(_T("%d."),nSubnetNumber);
sIpString += sTemp;
}
return sIpString;
}
 

My Computer

Back
Top