Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. IP Address

IP Address

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mehrdadov
    wrote on last edited by
    #1

    I want to get my Computer IP.How can I do it? Agh

    I G D T 4 Replies Last reply
    0
    • M mehrdadov

      I want to get my Computer IP.How can I do it? Agh

      I Offline
      I Offline
      includeh10
      wrote on last edited by
      #2

      go to www.whatismyipaddress.com to see it at once. includeh10

      P J 2 Replies Last reply
      0
      • I includeh10

        go to www.whatismyipaddress.com to see it at once. includeh10

        P Offline
        P Offline
        Prakash Nadar
        wrote on last edited by
        #3

        includeh10 wrote:

        go to www.whatismyipaddress.com to see it at once.

        This will give you the public ip if you are behind a proxy.


        -Prakash

        1 Reply Last reply
        0
        • M mehrdadov

          I want to get my Computer IP.How can I do it? Agh

          G Offline
          G Offline
          Gavin Taylor
          wrote on last edited by
          #4

          The best way to grab a list of all the ip addresses attached to your local system is using the IPHelper[^] library. You'll need to include the IPHlpApi.h and winsock2.h header files, then add iphlpapi.lib and ws2_32.lib to your list of import libraries.

          MIB_IPADDRTABLE *pIPAddrTable = NULL;
          DWORD dwSize = 0;
          DWORD dwRetVal = 0;

          if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) != ERROR_INSUFFICIENT_BUFFER )
          {
          printf( "Error getting buffer size." );
          }
          else
          {
          pIPAddrTable = ( MIB_IPADDRTABLE * ) malloc ( dwSize );

          if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, & dwSize, 0 ) ) != NO\_ERROR )
          {
          	printf("GetIpAddrTable failed.\\n");
          }
          
          for( int i = 0; i < pIPAddrTable->dwNumEntries; i ++ )
          {
          	in\_addr add;
          	in\_addr subnet;
          
          	ZeroMemory( & add, sizeof( in\_addr ) );
          	ZeroMemory( & subnet, sizeof( in\_addr ) );
          	add.S\_un.S\_addr = pIPAddrTable->table\[ i \].dwAddr;
          	subnet.S\_un.S\_addr = pIPAddrTable->table\[i\].dwMask;
          
          	printf("IP Address:         %s\\n", inet\_ntoa( add ) );
          	printf("IP Mask:            %s\\n\\n", inet\_ntoa( subnet ) );
          }
          
          free( pIPAddrTable );
          

          }

          Gavin Taylor w: http://www.gavspace.com -- modified at 12:11 Sunday 1st January, 2006

          J 1 Reply Last reply
          0
          • M mehrdadov

            I want to get my Computer IP.How can I do it? Agh

            D Offline
            D Offline
            dipuks
            wrote on last edited by
            #5

            Open a command window. go to Start->Run, type in "cmd" and click OK In Command window type in "ipconfig" click ENTER key You can see the IP address here.

            J Z 2 Replies Last reply
            0
            • I includeh10

              go to www.whatismyipaddress.com to see it at once. includeh10

              J Offline
              J Offline
              Jorgen Sigvardsson
              wrote on last edited by
              #6

              He posted his question in the Visual C++ forum. -- Pictures[^] from my Japan trip. -- modified at 16:09 Sunday 1st January, 2006

              1 Reply Last reply
              0
              • D dipuks

                Open a command window. go to Start->Run, type in "cmd" and click OK In Command window type in "ipconfig" click ENTER key You can see the IP address here.

                J Offline
                J Offline
                Jorgen Sigvardsson
                wrote on last edited by
                #7

                He posted his question in the Visual C++ forum. -- Pictures[^] from my Japan trip. -- modified at 16:10 Sunday 1st January, 2006

                1 Reply Last reply
                0
                • D dipuks

                  Open a command window. go to Start->Run, type in "cmd" and click OK In Command window type in "ipconfig" click ENTER key You can see the IP address here.

                  Z Offline
                  Z Offline
                  zpeng
                  wrote on last edited by
                  #8

                  I think he would like to get ipaddress using Visual C++ code instead of go cmd line. :laugh:

                  1 Reply Last reply
                  0
                  • G Gavin Taylor

                    The best way to grab a list of all the ip addresses attached to your local system is using the IPHelper[^] library. You'll need to include the IPHlpApi.h and winsock2.h header files, then add iphlpapi.lib and ws2_32.lib to your list of import libraries.

                    MIB_IPADDRTABLE *pIPAddrTable = NULL;
                    DWORD dwSize = 0;
                    DWORD dwRetVal = 0;

                    if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) != ERROR_INSUFFICIENT_BUFFER )
                    {
                    printf( "Error getting buffer size." );
                    }
                    else
                    {
                    pIPAddrTable = ( MIB_IPADDRTABLE * ) malloc ( dwSize );

                    if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, & dwSize, 0 ) ) != NO\_ERROR )
                    {
                    	printf("GetIpAddrTable failed.\\n");
                    }
                    
                    for( int i = 0; i < pIPAddrTable->dwNumEntries; i ++ )
                    {
                    	in\_addr add;
                    	in\_addr subnet;
                    
                    	ZeroMemory( & add, sizeof( in\_addr ) );
                    	ZeroMemory( & subnet, sizeof( in\_addr ) );
                    	add.S\_un.S\_addr = pIPAddrTable->table\[ i \].dwAddr;
                    	subnet.S\_un.S\_addr = pIPAddrTable->table\[i\].dwMask;
                    
                    	printf("IP Address:         %s\\n", inet\_ntoa( add ) );
                    	printf("IP Mask:            %s\\n\\n", inet\_ntoa( subnet ) );
                    }
                    
                    free( pIPAddrTable );
                    

                    }

                    Gavin Taylor w: http://www.gavspace.com -- modified at 12:11 Sunday 1st January, 2006

                    J Offline
                    J Offline
                    Jetli Jerry
                    wrote on last edited by
                    #9

                    Hi, This works for me, CString sLocalIp; HOSTENT *hs = NULL; UCHAR ch[4] = {0}; CHAR szHostName[50]={0}; // get name of machine if ( SOCKET_ERROR == ::gethostname((LPSTR)(LPCTSTR)szHostName, 50) ) { return GetLastError(); } hs = gethostbyname((LPSTR)(LPCTSTR)szHostName); if ( hs == NULL ) { return GetLastError(); } // h_addr contains ip address memcpy(ch, hs->h_addr,4); // form string sLocalIp.Format("%d.%d.%d.%d", ch[0], ch[1], ch[2], ch[3]); Hope this is also correct method.(((??))) Jetli Constant Thing In World Is Change.

                    G 1 Reply Last reply
                    0
                    • M mehrdadov

                      I want to get my Computer IP.How can I do it? Agh

                      T Offline
                      T Offline
                      ThatsAlok
                      wrote on last edited by
                      #10

                      mehrdadov wrote:

                      I want to get my Computer IP.How can I do it?

                      gethostbyaddr

                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                      cheers, Alok Gupta VC Forum Q&A :- I/ IV

                      1 Reply Last reply
                      0
                      • J Jetli Jerry

                        Hi, This works for me, CString sLocalIp; HOSTENT *hs = NULL; UCHAR ch[4] = {0}; CHAR szHostName[50]={0}; // get name of machine if ( SOCKET_ERROR == ::gethostname((LPSTR)(LPCTSTR)szHostName, 50) ) { return GetLastError(); } hs = gethostbyname((LPSTR)(LPCTSTR)szHostName); if ( hs == NULL ) { return GetLastError(); } // h_addr contains ip address memcpy(ch, hs->h_addr,4); // form string sLocalIp.Format("%d.%d.%d.%d", ch[0], ch[1], ch[2], ch[3]); Hope this is also correct method.(((??))) Jetli Constant Thing In World Is Change.

                        G Offline
                        G Offline
                        Gavin Taylor
                        wrote on last edited by
                        #11

                        The problem with that method is that it will only pullback one IP, the question was how do you retrieve the IP address for my local computer, it's quite possible to have several NIC's all with several IP addresses. The server sitting under my desk has 3 different IP's for instance. But yeah, your way does work aswell :) Gavin Taylor w: http://www.gavspace.com -- modified at 7:59 Monday 2nd January, 2006

                        J 1 Reply Last reply
                        0
                        • G Gavin Taylor

                          The problem with that method is that it will only pullback one IP, the question was how do you retrieve the IP address for my local computer, it's quite possible to have several NIC's all with several IP addresses. The server sitting under my desk has 3 different IP's for instance. But yeah, your way does work aswell :) Gavin Taylor w: http://www.gavspace.com -- modified at 7:59 Monday 2nd January, 2006

                          J Offline
                          J Offline
                          Jetli Jerry
                          wrote on last edited by
                          #12

                          Thats True Many thanks Gavin. :-D Jetli Constant Thing In World Is Change.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups