Thread: Gateway Address

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    35

    Question Gateway Address

    Hi,

    I am writing a newtowrk application that needs to obtain the local machines IP address, Gateway address and subnet mask.

    I can get the locl IP using gethostbyname(), but I need help, with examples if possible.

    1) How can I get the gateway address?

    2) How can I get the Subnet Mask?

    I have used netsh in the past, If this is the answer then how do I get the addresses into variables that I can use to display the addresses in controls.

    Many Thanks

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can use GetAdaptersInfo. Here is a sample, error checking is omitted.
    Code:
    #include <windows.h>
    #include <Iphlpapi.h>
    #include <stdio.h>
    #include <stdlib.h>
    #pragma comment(lib, "Iphlpapi.lib")
    
    int main(void)
    {
    	ULONG            cbBuf    = 0;
    	PIP_ADAPTER_INFO pAdapter = NULL;
    	PIP_ADAPTER_INFO pMemory  = NULL;
    	DWORD            dwResult = 0;
    
    	dwResult = GetAdaptersInfo(NULL, &cbBuf);
    
    	pMemory = pAdapter = (PIP_ADAPTER_INFO) malloc(cbBuf);
    
    	dwResult = GetAdaptersInfo(pAdapter, &cbBuf);
    
    	while (pAdapter)
    	{
    		printf("Name: %s\nDescription: %s\nGateway: %s\nSubnet Mask: %s\n",
    	               pAdapter->AdapterName,
    		       pAdapter->Description,
    		       pAdapter->GatewayList.IpAddress.String,
    		       pAdapter->IpAddressList.IpMask.String);
    
    		pAdapter = pAdapter->Next;
    	}
    
    	free(pMemory);
    
    	getchar();
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    35
    Many Thanks, but

    I am using MS Visual C++ 6.0

    The function GetAdaptersInfo is not included nor is the header file Iphlpapi.h

    Is there a VC++ way or where can I get the required Iphlpapi.h file?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You need to download the latest Platform SDK.

    http://www.microsoft.com/msdownload/...sdk/sdkupdate/

    Click on "Core SDK" on the left then "Install this SDK" on the right.

    Once installed, you'll need to add new "Include files" and "Library files" directories in VC++ 6.0: Tools -> Options -> Directories tab

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM