Thread: Internal IP address program

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    7

    Internal IP address program

    Hi, through looking at these forums I found a program that gets the internal machine name and IP address.

    Problem is I only need the IP.

    How would I modify this code to only get the IP?

    #include <winsock.h> /* for socket(),... */
    #include <windows.h>

    Code:
    char* get_local_ip()
    {
        char abc[MAX_PATH];
        
        WSADATA wsaData;
        SOCKET sockfd;          
        WSAStartup(MAKEWORD(2, 0),&wsaData);   
        sockfd=socket(AF_INET,SOCK_STREAM,0);
        
        struct sockaddr_in sa;
        struct hostent *hp;
        gethostname(abc, sizeof(abc));
        puts(abc);
        
        hp = gethostbyname(abc);
        sa.sin_family = hp->h_addrtype;
        memcpy(&sa.sin_addr, hp->h_addr, sizeof(sa.sin_addr));
        
        return inet_ntoa(sa.sin_addr);
    }
    cheers

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    That code fragment is dependent on the hostname. So, you must query the hostname in order to get the IP address. If you're determined to only get the IP address, then use the iphlpapi.lib helper lib.

    Code:
    #pragma comment( lib, "iphlpapi.lib" )
    
    #include <windows.h>
    #include <stdio.h>
    #include <iphlpapi.h>
    
    int main(void)
    {
    	ULONG dwSize = 0;
    	DWORD dwIP;
    
    	// We just get the table size here
    	if(GetIpAddrTable(NULL, &dwSize, FALSE) == ERROR_INSUFFICIENT_BUFFER)
    	{
    		// Allocate a pointer based on table size
    		MIB_IPADDRTABLE *ipAddress = (MIB_IPADDRTABLE *) new BYTE[dwSize];
    		// Now here's where we retrieve the actual info
    		if(GetIpAddrTable(ipAddress, &dwSize, TRUE) == NO_ERROR)
    		{
    			// We may have multiple interfaces.  Let's list them all
    			for(DWORD dwIndex = 0; dwIndex < ipAddress->dwNumEntries; dwIndex++)
    			{
    				//The IP address
    				dwIP = ipAddress->table[dwIndex].dwAddr;
    				// Assuming Windows, we'll use a macro conversion
    				// You can also use inet_ntoa here to address endianess
    				int octet1 = LOBYTE(LOWORD(dwIP));
    				int octet2 = HIBYTE(LOWORD(dwIP));
    				int octet3 = LOBYTE(HIWORD(dwIP));
    				int octet4 = HIBYTE(HIWORD(dwIP));
    				printf("%d.%d.%d.%d\n",octet1, octet2, octet3, octet4); 
    			}
    		}
    	}
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    Post

    hey dear.............
    I am not able to understand your solution...........
    I think it will not run on VC++6 because it don't have iphlpapi.h file........

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    2
    i use MS Visual Studio 8 ( 2005 ) and i found the file IPHlpApi.h in C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include . Well ... i guess you could download it, along with all the dependencies. Or move on to Ms Visual Studio 8 .

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by msmihai View Post
    i use MS Visual Studio 8 ( 2005 ) and i found the file IPHlpApi.h in C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include . Well ... i guess you could download it, along with all the dependencies. Or move on to Ms Visual Studio 8 .
    Or just get the platform SDK itself, perhaps? It should be compatible with older versions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IP address discovery
    By abinash in forum Networking/Device Communication
    Replies: 6
    Last Post: 04-29-2007, 02:49 PM
  2. IP Address Control Text Freeing?
    By MitchellH in forum Windows Programming
    Replies: 2
    Last Post: 05-22-2005, 11:49 PM
  3. How to tell when internal program is done running
    By PJYelton in forum Windows Programming
    Replies: 5
    Last Post: 03-21-2005, 10:09 PM
  4. how to get memory address applied in other program?
    By wu7up in forum C Programming
    Replies: 1
    Last Post: 03-01-2003, 01:44 AM
  5. MSN Vital Information
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-22-2001, 08:55 PM