CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2006
    Posts
    148

    Capturing IP Address

    How do i capture ip address of a machine having more than one ip?

    Thanx for any kind of help.

    Regards,
    shail2k4

  2. #2
    Join Date
    Jun 2005
    Location
    Chennai , India
    Posts
    1,375

    Thumbs up Re: Capturing IP Address

    Quote Originally Posted by shail2k4
    How do i capture ip address of a machine having more than one ip?

    Thanx for any kind of help.

    Regards,
    shail2k4
    How to get the local IP address(es)
    It takes seconds for rating…that actually compensates the minutes taken for giving answers
    The biggest guru-mantra is: Never share your secrets with anybody. It will destroy you.
    Regards, Be generous->Rate people
    Jayender!!

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Capturing IP Address

    Doesn't GetIpAddrTable work?
    GetIpAddrTable

  4. #4
    Join Date
    Nov 2005
    Posts
    281

    Re: Capturing IP Address

    Do you want the internal or external IP's?

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Capturing IP Address

    Also see GetAdaptersInfo and GetAdaptersAddresses.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Jun 2006
    Posts
    46

    Re: Capturing IP Address

    the code below is partly based on various internet resources, partly my own. simply call the methods, passing them parameters to store return values.
    link: iphlpapi.lib
    stdafx.h: #include <afxsock.h> // MFC socket extensions
    global: #include <iphlpapi.h> // internet
    #include <afxinet.h>
    Code:
    // determine all LAN IP addresses of this machine into CStringArray reference parameter
    void FindLANIPAddresses( CStringArray & stringArray )
    {
    	const DWORD loopbackAddress = 16777343; // 127.0.0.1 - to be ignored
    	struct in_addr  ipAddress;
    	PMIB_IPADDRTABLE ipAddressTable = new MIB_IPADDRTABLE;
    	ULONG size = 0;
    
    	// determine the necessary size of ipAddressTable into 'size'
    	if( GetIpAddrTable( ipAddressTable, &size, 0 ) == ERROR_INSUFFICIENT_BUFFER )
    	{
    		delete [] ipAddressTable;
    		ipAddressTable = ( PMIB_IPADDRTABLE ) malloc( size );
    	}
    
    	// now 'size' is correct, so fill in ipAddressTable
    	if(  GetIpAddrTable( ipAddressTable, &size, TRUE )  != NO_ERROR )
    		return; // error
    	
    	// convert ip to string , get rid of blank and loopback entries
    	DWORD tempIP;
    	for( int i = 0; i < ( int )ipAddressTable->dwNumEntries; i++ )
    	{
    		tempIP = ipAddressTable->table[ i ].dwAddr;
    		if( tempIP != 0 && tempIP != loopbackAddress )
    		{			
    			memmove ( &ipAddress, &tempIP, 4 );
    			stringArray.Add( inet_ntoa ( ipAddress ) );
    		}
    	}
    
    	delete [] ipAddressTable;
    
    } // end FindLANIPAddresses
    
    
    // determine WAN IP address of this machine or router/gateway into CString parameter
    void FindWANIPAddress( CString & string )
    {
    	DWORD queryReturnValue = 0;
    	CInternetSession *pSession = new CInternetSession(_T("GetIP number") );
    	CHttpFile * pFile;
    
    	try
    	{
    		pFile = ( CHttpFile * ) pSession->OpenURL( "http://checkip.dyndns.org/" );
    	}
    
    	catch (CInternetException* pEx)
    	{
    		pEx->Delete();
    		pFile->Close();
    		delete pFile;		
    		delete pSession;	 	
    		return;						// could not open URL
    	}
    
    	pFile->QueryInfoStatusCode(queryReturnValue);	
    
    	if( queryReturnValue == 200 )	// Yes! - success status code: 200
    	{			
    		char * fileBuffer = new char[ 1024 ];
    		char * originalFileBuffer = fileBuffer;
    		char ipBuffer[ 16 ]; // holds extracted IP address
    		pFile->Read( fileBuffer, 1024 ); // read first 1024 bytes of HTML.
    		if ( fileBuffer )
    		{
    			int start = strcspn( fileBuffer, "1234567890" );
    			fileBuffer += start;
    			int end = strspn( fileBuffer, "0123456789." );
    			strncpy( ipBuffer, fileBuffer, end );
    			ipBuffer[ end ] = '\0';
    			string = ipBuffer; // place IP number in passed parameter
    		}
    		delete [] originalFileBuffer;
    	}
    
    	pFile->Close();
    	delete pFile;	
    	delete pSession;
    	
    } // end FindWANIPAddress
    Last edited by Cielak; July 17th, 2006 at 09:10 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured