Getting the current IP address from an application (VC ++ 2005)

I would like to send an email from an application that contains the current IP address of the device.

I have my email code in place and it works. I just need to add ipaddress to the body of the email (i.e. I am not doing anything programmatically with the IP address).

I was hoping there was a very simple way to run ipconfig using a system command and grab the resulting text.

I would rather not open a socket.

Thanks,

0


a source to share


6 answers


This code will loop through all adapters and make sure the first one is up.



#include <afxtempl.h>
#include <afxsock.h>
#include <iphlpapi.h>

u_long GetFirstIpAddressUp(SOCKET s)
{
#define MAX_ADAPTERS    30
#pragma comment(lib, "Iphlpapi.lib")
    IP_ADAPTER_ADDRESSES AdapterAddresses[MAX_ADAPTERS];           
    PIP_ADAPTER_ADDRESSES pAdapterAddresses = AdapterAddresses;                                              
    DWORD dwBufLen = sizeof(AdapterAddresses);
    if (GetAdaptersAddresses(AF_INET, 0, NULL, AdapterAddresses,&dwBufLen) == ERROR_SUCCESS)
    {
        do {
            if ((pAdapterAddresses->OperStatus == IfOperStatusUp))
            {
                sockaddr_in* pAdr = (sockaddr_in*)pAdapterAddresses->FirstUnicastAddress->Address.lpSockaddr;
                return pAdr->sin_addr.S_un.S_addr;
            }
        pAdapterAddresses = pAdapterAddresses->Next;
        } while(pAdapterAddresses);
    }
    return INADDR_ANY;  // No adapters are up
}

      

+1


a source


Use gethostbyname

(deprecated) or new getaddrinfo

. MSDN links also contain examples.



+1


a source


EDIT: Ok, it helps if I read that you are using VC ++ and not C # .....

So, you can ignore my answer or use it as a guide ...

here ya go:

using System;
using System.Net;


namespace testProgram
{
    class Program
    {
        static void Main()
        {
            String strHostName = string.Empty;
            // Getting Ip address of local machine...
            // First get the host name of local machine.
            strHostName = Dns.GetHostName();
            Console.WriteLine("Local Machine Host Name: " + strHostName);


            // Then using host name, get the IP address list..
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
            IPAddress[] addr = ipEntry.AddressList;

            for (int i = 0; i < addr.Length; i++)
            {
                Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
            }
            Console.ReadLine();

        }
    }

}

      

this will give you all the information you need, and then some, you can make out what you need and don't need.

+1


a source


I have attached the "Jason Heine" code (not sure how to tag the name) in C ++. it should now work. by the way, to him. So this is what I get:

using namespace System;
using namespace System::Net;

void main(){
  String ^strHostName = String::Empty;
  // Getting Ip address of local machine...
  // First get the host name of local machine.
  strHostName = Dns::GetHostName();
  Console::WriteLine("Local Machine Host Name: " + strHostName);

  // Then using host name, get the IP address list..
  IPHostEntry^ ipEntry = Dns::GetHostEntry(strHostName);
  array<IPAddress^> ^addr = ipEntry->AddressList;

  for (int i = 0; i < addr->Length; i++)
  {
      Console::WriteLine("IP Address {0}: {1} ", i, addr[i]->ToString());
  }
  Console::ReadKey();
}

      

Hope this helps you.

Tested On my computer with wifi and LAN connecten and multiple virtual machines for "VMware Player" and I get 4 IPv6 and then wrote 4 IPv4. if you only need IPv4 you can use:

using namespace System;
using namespace System::Net;

void main(){
  String ^strHostName = String::Empty;
  // Getting Ip address of local machine...
  // First get the host name of local machine.
  strHostName = Dns::GetHostName();
  Console::WriteLine("Local Machine Host Name: " + strHostName);

  // Then using host name, get the IP address list..
  IPHostEntry^ ipEntry = Dns::GetHostEntry(strHostName);
  array<IPAddress^> ^addr = ipEntry->AddressList;

  for (int i = 0; i < addr->Length; i++)
  {
      if(addr[i]->ToString()->Length < 20){
          Console::WriteLine("IP Address {0}: {1} ", i, addr[i]->ToString());
      }
  }
  Console::ReadKey();
}

      

Then I only get IPv4. just numbers start with 4. But that's ok for me. you can add a new variable to recalculate the numbering :)

+1


a source


I believe you might have to look into winsock2.h

0


a source


Of course getaddrinfo works in your case, but I thought I'd mention that if you all want more than just read ip, you can use the windows IP Helper api to execute most of the ipconfig utility.

0


a source







All Articles