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. How to get IP Address of the system using MFC

How to get IP Address of the system using MFC

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
8 Posts 6 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.
  • P Offline
    P Offline
    PankajB
    wrote on last edited by
    #1

    Hi there. I can find the IP address of current system using below code but that's C++ code...

    char* CLogger::GetIPAdd()
    {
    WORD wVersionRequested;
    WSADATA wsaData;
    char Name[255];
    PHOSTENT HostInfo;
    wVersionRequested = MAKEWORD( 1, 1 );
    char *IPAdd;
    if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
    {
    if( gethostname ( Name, sizeof(Name)) == 0)
    {
    //printf("Host name: %s\n", name);
    if((HostInfo = gethostbyname(Name)) != NULL)
    {
    int nCount = 0;
    while(HostInfo->h_addr_list[nCount])
    {
    IPAdd = inet_ntoa(*(struct in_addr *)HostInfo->h_addr_list[nCount]);

    				++nCount;
    			}
    		}
    	}
    }
    

    return IPAdd;
    }

    I want to get an IP Address using MFC. Can any one please help me out to get the same using MFC? Thanks PanB

    _ S D E 4 Replies Last reply
    0
    • P PankajB

      Hi there. I can find the IP address of current system using below code but that's C++ code...

      char* CLogger::GetIPAdd()
      {
      WORD wVersionRequested;
      WSADATA wsaData;
      char Name[255];
      PHOSTENT HostInfo;
      wVersionRequested = MAKEWORD( 1, 1 );
      char *IPAdd;
      if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
      {
      if( gethostname ( Name, sizeof(Name)) == 0)
      {
      //printf("Host name: %s\n", name);
      if((HostInfo = gethostbyname(Name)) != NULL)
      {
      int nCount = 0;
      while(HostInfo->h_addr_list[nCount])
      {
      IPAdd = inet_ntoa(*(struct in_addr *)HostInfo->h_addr_list[nCount]);

      				++nCount;
      			}
      		}
      	}
      }
      

      return IPAdd;
      }

      I want to get an IP Address using MFC. Can any one please help me out to get the same using MFC? Thanks PanB

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      MFC is just a wrapper over C++ classes functions, but what is the problem with the code above. Even if you do find a class in MFC to get the IP address it must be using a similar sort of mechanism to get the IP Address of the local machine

      You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

      P 1 Reply Last reply
      0
      • _ _AnsHUMAN_

        MFC is just a wrapper over C++ classes functions, but what is the problem with the code above. Even if you do find a class in MFC to get the IP address it must be using a similar sort of mechanism to get the IP Address of the local machine

        You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

        P Offline
        P Offline
        PankajB
        wrote on last edited by
        #3

        Why MFC? I am getting lots of Windows.h already included related error messages. But if we can get IP Address in MFC then that will server my requirement. Thanks

        _ 1 Reply Last reply
        0
        • P PankajB

          Hi there. I can find the IP address of current system using below code but that's C++ code...

          char* CLogger::GetIPAdd()
          {
          WORD wVersionRequested;
          WSADATA wsaData;
          char Name[255];
          PHOSTENT HostInfo;
          wVersionRequested = MAKEWORD( 1, 1 );
          char *IPAdd;
          if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
          {
          if( gethostname ( Name, sizeof(Name)) == 0)
          {
          //printf("Host name: %s\n", name);
          if((HostInfo = gethostbyname(Name)) != NULL)
          {
          int nCount = 0;
          while(HostInfo->h_addr_list[nCount])
          {
          IPAdd = inet_ntoa(*(struct in_addr *)HostInfo->h_addr_list[nCount]);

          				++nCount;
          			}
          		}
          	}
          }
          

          return IPAdd;
          }

          I want to get an IP Address using MFC. Can any one please help me out to get the same using MFC? Thanks PanB

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          PankajB wrote:

          I can find the IP address of current system using below code but that's C++ code...

          PankajB wrote:

          I want to get an IP Address using MFC

          MFC doesn't have functionality covering IP address resolution - if it works, your C++ code is as good as anything. Don't get too hung up about things being in MFC or not in MFC - MFC is, for the most part, a thin veneer over Win32 functions and, overall, has a very old fashioned design , a lot of which was constrained by the deficiencies of C++ compilers in use when MFC was developed.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          1 Reply Last reply
          0
          • P PankajB

            Why MFC? I am getting lots of Windows.h already included related error messages. But if we can get IP Address in MFC then that will server my requirement. Thanks

            _ Offline
            _ Offline
            _AnsHUMAN_
            wrote on last edited by
            #5

            PankajB wrote:

            But if we can get IP Address in MFC then that will server my requirement.

            do you apply ointment on your feet if you have an headache :)

            PankajB wrote:

            I am getting lots of Windows.h already included related error messages.

            figure out why these errors are popping and resolve them. May be you are missing a library to link to or you didn't include a header file. BTW what are the errors?

            You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

            1 Reply Last reply
            0
            • P PankajB

              Hi there. I can find the IP address of current system using below code but that's C++ code...

              char* CLogger::GetIPAdd()
              {
              WORD wVersionRequested;
              WSADATA wsaData;
              char Name[255];
              PHOSTENT HostInfo;
              wVersionRequested = MAKEWORD( 1, 1 );
              char *IPAdd;
              if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
              {
              if( gethostname ( Name, sizeof(Name)) == 0)
              {
              //printf("Host name: %s\n", name);
              if((HostInfo = gethostbyname(Name)) != NULL)
              {
              int nCount = 0;
              while(HostInfo->h_addr_list[nCount])
              {
              IPAdd = inet_ntoa(*(struct in_addr *)HostInfo->h_addr_list[nCount]);

              				++nCount;
              			}
              		}
              	}
              }
              

              return IPAdd;
              }

              I want to get an IP Address using MFC. Can any one please help me out to get the same using MFC? Thanks PanB

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              PankajB wrote:

              ...but that's C++ code...

              Nothing in your code is C++.

              PankajB wrote:

              return IPAdd;

              You are potentially returning an uninitialized pointer.

              PankajB wrote:

              I want to get an IP Address using MFC.

              Then use the same code.

              PankajB wrote:

              Can any one please help me out to get the same using MFC?

              MFC offers no such class.

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              1 Reply Last reply
              0
              • P PankajB

                Hi there. I can find the IP address of current system using below code but that's C++ code...

                char* CLogger::GetIPAdd()
                {
                WORD wVersionRequested;
                WSADATA wsaData;
                char Name[255];
                PHOSTENT HostInfo;
                wVersionRequested = MAKEWORD( 1, 1 );
                char *IPAdd;
                if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
                {
                if( gethostname ( Name, sizeof(Name)) == 0)
                {
                //printf("Host name: %s\n", name);
                if((HostInfo = gethostbyname(Name)) != NULL)
                {
                int nCount = 0;
                while(HostInfo->h_addr_list[nCount])
                {
                IPAdd = inet_ntoa(*(struct in_addr *)HostInfo->h_addr_list[nCount]);

                				++nCount;
                			}
                		}
                	}
                }
                

                return IPAdd;
                }

                I want to get an IP Address using MFC. Can any one please help me out to get the same using MFC? Thanks PanB

                E Offline
                E Offline
                Esaias Pech
                wrote on last edited by
                #7

                Thanks for the code! I just had to add the library Ws2_32.lib and the header Windows.h, works perfectly! Thanks!

                B 1 Reply Last reply
                0
                • E Esaias Pech

                  Thanks for the code! I just had to add the library Ws2_32.lib and the header Windows.h, works perfectly! Thanks!

                  B Offline
                  B Offline
                  Bemali
                  wrote on last edited by
                  #8

                  Yeah it works perfect. Thanks a lot for the code :)

                  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