Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i implement the coding to get the subnet mask and the default gateway of the system..

following coding:
int main(int argc, char* argv[])
{
    PIP_ADAPTER_INFO pAdapterInfo;
    PIP_ADAPTER_INFO pAdapter=NULL;
    printf("Hello World!\n");
    DWORD dwRetVal=0;
    UINT i;
    WORD wVersionRequested;
    WSADATA wsaData;
    struct in_addr addr;
    int err;
    wVersionRequested = MAKEWORD(2, 2);
    err = WSAStartup(wVersionRequested, &wsaData);
    struct tm newtime;
    char buffer[32];
    ULONG ulOutBufLen=sizeof(PIP_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;
    }
    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;
    pAdapter = pAdapterInfo;
    while(pAdapter)
    {
            printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);
            printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);
            Sleep(3000);
            pAdapter = pAdapter->Next;
    }
        }
        return 0;
}


problem is:


it return only one subnet mask and the default gateway of the system..
but my system having 3 subnet mask address..

i want to display these three ..
can you help me...
Posted
Updated 8-Jul-11 3:36am
v4
Comments
Prerak Patel 8-Jul-11 2:14am    
Use pre tags for code section.
@BangIndia 9-Jul-11 1:58am    
<pre lang="midl">IP_ADDR_STRING *p;
p=(pAdapter->IpAddressList.Next);
printf("final\t%s",p->IpMask.String);
p=p->Next;
printf("final\t%s",p->IpMask.String);
p=p->Next;</pre>


that is the soultion. myself i found.. its working good..
ThatsAlok 8-Jul-11 2:57am    
could you debug this piece of code and check what pAdapter->next returning
while(pAdapter)
{
printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);
printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);
Sleep(3000);
pAdapter = pAdapter->Next;
}
@BangIndia 9-Jul-11 1:58am    
<pre lang="midl">IP_ADDR_STRING *p;
p=(pAdapter->IpAddressList.Next);
printf("final\t%s",p->IpMask.String);
p=p->Next;
printf("final\t%s",p->IpMask.String);
p=p->Next;</pre>


that is the soultion. myself i found.. its working good..

MSDN contains a lot of info on your problem. This[^] article offers you to use GetAdaptersAddresses()[^] function on Windows XP and later, this[^] page tells you IpAddressList is actually a linked list (you can use Next member) and this[^] article describes function that can query loopback interface parameters, if needed
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jul-11 3:22am    
A 5. I know a good tutorial and a nice article on subnetting, please see my solution.
--SA
Timberbird 8-Jul-11 7:20am    
Thank you. I had to perform task almost similar to OP's a several years ago; wish I read these articles back then :)
@BangIndia 8-Jul-11 8:43am    
please can you help for that..
i read the article.. good..
Timberbird 8-Jul-11 9:34am    
In your while(pAdapter) cycle, enumerate through pAdapter->IpAddressList and pAdapter->GatewayList using their Next member, just the same way you do it with pAdapter
@BangIndia 8-Jul-11 10:03am    
sir where i want to store that element sir..
i can use the pAdapter->IpAddressList.Next .

but if i try to store
pAdapter->IpAddressList=pAdapter->IpAddressList.Next ;
it provide error sir.. what can i do now.. please sir help me
You can find pretty good detailed tutorial and a short and clear article on subnetting and the subnet masks here:

IP Address Subnetting Tutorial:
http://www.ralphb.net/IPSubnet/[^];

IP subnetting made easy:
http://www.techrepublic.com/article/ip-subnetting-made-easy/6089187[^].

—SA
 
Share this answer
 
MIDL
IP_ADDR_STRING *p;
                  p=(pAdapter->IpAddressList.Next);
                  printf("final\t%s",p->IpMask.String);
                    p=p->Next;
                    printf("final\t%s",p->IpMask.String);
                    p=p->Next;



that is the soultion. myself i found.. its working good..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900