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. Get IP address of computer

Get IP address of computer

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
17 Posts 3 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.
  • O Owner drawn

    I was asking whether the code given by Laxman is MFC or not. It's SDK.:^)

    Jesus Loves:rose:

    --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

    W Offline
    W Offline
    waxie
    wrote on last edited by
    #6

    Yeah. It's in .NET. I just want to retrieve IP address and then store it in a string. waxie

    O 1 Reply Last reply
    0
    • W waxie

      Yeah. It's in .NET. I just want to retrieve IP address and then store it in a string. waxie

      O Offline
      O Offline
      Owner drawn
      wrote on last edited by
      #7

      waxie wrote:

      It's in .NET

      It's not in .Net. It's in SDK. Anyway it will work smoothly so don't worry. Anyway why are you reposting the same question again. I guess Laxman already answered you.

      Jesus Loves:rose:

      --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

      W 2 Replies Last reply
      0
      • O Owner drawn

        waxie wrote:

        It's in .NET

        It's not in .Net. It's in SDK. Anyway it will work smoothly so don't worry. Anyway why are you reposting the same question again. I guess Laxman already answered you.

        Jesus Loves:rose:

        --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

        W Offline
        W Offline
        waxie
        wrote on last edited by
        #8

        I've tried using the code but it still does not work. I'm making some tweaks. Hopefully this will work. waxie

        1 Reply Last reply
        0
        • O Owner drawn

          waxie wrote:

          It's in .NET

          It's not in .Net. It's in SDK. Anyway it will work smoothly so don't worry. Anyway why are you reposting the same question again. I guess Laxman already answered you.

          Jesus Loves:rose:

          --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

          W Offline
          W Offline
          waxie
          wrote on last edited by
          #9

          I'm getting these errors when linking: error LNK2001: unresolved external symbol __imp__gethostbyname@4 error LNK2001: unresolved external symbol __imp__gethostname@8 you have any idea what causes these errors? I guess the linker is looking for a lib file or something.. waxie

          O 1 Reply Last reply
          0
          • W waxie

            I'm getting these errors when linking: error LNK2001: unresolved external symbol __imp__gethostbyname@4 error LNK2001: unresolved external symbol __imp__gethostname@8 you have any idea what causes these errors? I guess the linker is looking for a lib file or something.. waxie

            O Offline
            O Offline
            Owner drawn
            wrote on last edited by
            #10

            Have you added support for WinSock in your project. This happens if you don't have support for winsock in your project. You need to include Winsock2.h And which version of Visual Studio are you using.

            Jesus Loves:rose:

            --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

            W 1 Reply Last reply
            0
            • O Owner drawn

              Have you added support for WinSock in your project. This happens if you don't have support for winsock in your project. You need to include Winsock2.h And which version of Visual Studio are you using.

              Jesus Loves:rose:

              --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

              W Offline
              W Offline
              waxie
              wrote on last edited by
              #11

              Yes I have included winsock.h in my project - else it would produce symbol not found errors. I'm using VC6.

              O 1 Reply Last reply
              0
              • W waxie

                Yes I have included winsock.h in my project - else it would produce symbol not found errors. I'm using VC6.

                O Offline
                O Offline
                Owner drawn
                wrote on last edited by
                #12

                Did you link to the Winsock library.

                Jesus Loves:rose:

                --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                W 1 Reply Last reply
                0
                • O Owner drawn

                  Did you link to the Winsock library.

                  Jesus Loves:rose:

                  --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                  W Offline
                  W Offline
                  waxie
                  wrote on last edited by
                  #13

                  Yes, I have. I don't have errors now but it really doesn't output anything. If it's alright with you, can you check if this code works fine in your pc and outputs your IP ad? Thanks so much. waxie

                  O 1 Reply Last reply
                  0
                  • W waxie

                    Yes, I have. I don't have errors now but it really doesn't output anything. If it's alright with you, can you check if this code works fine in your pc and outputs your IP ad? Thanks so much. waxie

                    O Offline
                    O Offline
                    Owner drawn
                    wrote on last edited by
                    #14

                    Try this one.... You may have to modify it a bit to suit your needs....

                    void CHostNameAndIpDlg::OnBnClickedFind()
                    {
                    WSADATA WSAData;
                    ::ZeroMemory(&WSAData, sizeof(WSAData));
                    //init winsock
                    ::WSAStartup(MAKEWORD(1,0), &WSAData);

                    char szHostName\[MAX\_PATH\];
                    ::gethostname(szHostName, MAX\_PATH);
                    //SetDlgItemText(IDC\_HOSTNAME, szHostName);	
                         AfxMessageBox(szHostName);
                    char szIps\[128\];
                    
                    struct sockaddr\_in sckAddr;
                    struct hostent \*pHost;
                    
                    pHost = ::gethostbyname(szHostName);
                    
                    for(int index=0; pHost->h\_addr\_list\[index\] != NULL;index++)
                    {
                    	memcpy(&sckAddr.sin\_addr, pHost->h\_addr\_list\[index\], pHost->h\_length);
                    	strcpy(szIps, inet\_ntoa(sckAddr.sin\_addr));
                    	strcat(szIps, ", ");
                    	//SetDlgItemText(IDC\_HOSTIP, szIps);
                                  AfxMessageBox(szIps);
                    	szIps\[0\] = 0;
                    }
                    //close winsock
                    ::WSACleanup();	
                    

                    }

                    AfxMessageBox denotes output. You can replace it with your own output procedure. I have commented my output procedure. First it will display a message box containing host name, second it will display message boxes for each IP found(If there are more that one).

                    Jesus Loves:rose:

                    --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                    W 1 Reply Last reply
                    0
                    • O Owner drawn

                      Try this one.... You may have to modify it a bit to suit your needs....

                      void CHostNameAndIpDlg::OnBnClickedFind()
                      {
                      WSADATA WSAData;
                      ::ZeroMemory(&WSAData, sizeof(WSAData));
                      //init winsock
                      ::WSAStartup(MAKEWORD(1,0), &WSAData);

                      char szHostName\[MAX\_PATH\];
                      ::gethostname(szHostName, MAX\_PATH);
                      //SetDlgItemText(IDC\_HOSTNAME, szHostName);	
                           AfxMessageBox(szHostName);
                      char szIps\[128\];
                      
                      struct sockaddr\_in sckAddr;
                      struct hostent \*pHost;
                      
                      pHost = ::gethostbyname(szHostName);
                      
                      for(int index=0; pHost->h\_addr\_list\[index\] != NULL;index++)
                      {
                      	memcpy(&sckAddr.sin\_addr, pHost->h\_addr\_list\[index\], pHost->h\_length);
                      	strcpy(szIps, inet\_ntoa(sckAddr.sin\_addr));
                      	strcat(szIps, ", ");
                      	//SetDlgItemText(IDC\_HOSTIP, szIps);
                                    AfxMessageBox(szIps);
                      	szIps\[0\] = 0;
                      }
                      //close winsock
                      ::WSACleanup();	
                      

                      }

                      AfxMessageBox denotes output. You can replace it with your own output procedure. I have commented my output procedure. First it will display a message box containing host name, second it will display message boxes for each IP found(If there are more that one).

                      Jesus Loves:rose:

                      --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                      W Offline
                      W Offline
                      waxie
                      wrote on last edited by
                      #15

                      Thank you very much. I think you deserve a 10. :cool:

                      O 1 Reply Last reply
                      0
                      • W waxie

                        Thank you very much. I think you deserve a 10. :cool:

                        O Offline
                        O Offline
                        Owner drawn
                        wrote on last edited by
                        #16

                        Remove this from the loop... szIps[0] = 0; or else you won't get all the ip addresses. And put the messagebox in the loop, outside the loop. This will display all the ip addresses together.

                        Jesus Loves:rose:

                        --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                        W 1 Reply Last reply
                        0
                        • O Owner drawn

                          Remove this from the loop... szIps[0] = 0; or else you won't get all the ip addresses. And put the messagebox in the loop, outside the loop. This will display all the ip addresses together.

                          Jesus Loves:rose:

                          --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                          W Offline
                          W Offline
                          waxie
                          wrote on last edited by
                          #17

                          Okiems. Thank you very much Owner drawn! I really appreciate your help. :) waxie

                          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