Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Vista Forums > Network & Sharing

Vista - IPHelper functions - to get IP and subnet mask

Reply
 
Old 05-29-2009   #1 (permalink)


Vista 32bit
 
 

IPHelper functions - to get IP and subnet mask

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 System SpecsSystem Spec
Old 05-29-2009   #2 (permalink)


Vista 32bit
 
 

Re: IPHelper functions - to get IP and subnet mask

I can get IP now, but subnet is not found.
My System SpecsSystem Spec
Old 05-29-2009   #3 (permalink)


Win7x64
 
 

Re: IPHelper functions - to get IP and subnet mask

Quote  Quote: Originally Posted by atong99 View Post
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 System SpecsSystem Spec
Old 05-30-2009   #4 (permalink)


Vista 32bit
 
 

Re: IPHelper functions - to get IP and subnet mask

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 System SpecsSystem Spec
Old 06-12-2009   #5 (permalink)


Vista 32bit
 
 

Re: IPHelper functions - to get IP and subnet mask

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[i];
IpTemp = IpTemp%nbase[i];

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

Thread Tools


Similar Threads
Thread Forum
setting subnet mask and gateway Vista networking & sharing
Iphelper, host process, appcrash, facebook, myspace Vista General
IPHelper Error in Vista x64 General Discussion
Vista VPN gets wrong subnet mask Vista networking & sharing
Where can I see my IP and net mask? Vista networking & sharing


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46