2009-05-12 4 views
0

시스템의 현재 IP 주소가있는 응용 프로그램에서 전자 메일을 보내려합니다.응용 프로그램 내에서 현재 IP 주소 가져 오기 (VC++ 2005)

이메일 코드가 제대로 작동하며 작동합니다. 난 그냥 이메일 본문에 ipaddress를 추가해야합니다 (즉, 프로그래밍 방식으로 IP 주소를 사용하지 않고 있습니다).

시스템 명령을 통해 ipconfig를 실행하고 결과 텍스트를 얻는 것과 같은 간단한 방법이 있었으면합니다.

소켓을 열 필요가 없습니다.

감사합니다,

답변

1

이 코드는 모든 어댑터를 반복하여 첫 번째 어댑터가 있는지 확인합니다.

#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 
} 
0

난 당신이 winsock2.h에

1

사용 중 gethostbyname (사용되지 않음) 또는 getaddrinfo 새를 볼 필요가 있습니다 생각합니다. MSDN 링크에는 예제도 포함되어 있습니다.

1

편집 : 난 당신이

그래서 당신이 내 대답을 무시하거나, 또는 어쩌면 가이드로 사용할 수 있습니다 ..... C#을 VC++을 사용하고 있지 않은지 읽으면 음, ...

하는 데 도움이 여기

나중에 가서 :

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's 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(); 

     } 
    } 

} 

이 당신에게 당신이 필요로하는 모든 정보를 제공하고 일부는, 당신은 당신이 필요하지 않아도 무엇을 구문 분석 할 수 있습니다.

0

은 물론 귀하의 경우 작품을 getaddrinfo는,하지만 난 당신이 모든보다 더 원하는 경우에 당신이에서 ipconfig 유틸리티가하는 일의 대부분을 수행 할 창 IP Helper api를 사용할 수있는 IP를 읽어 언급 거라고 생각했다.

1

"Jason Heine"(이름을 태그하는 방법을 모른다) 코드를 C++에 updeted했습니다. 이제는 효과가있다. 그에게 thnx 방법에 의해.

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's 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(); 
} 

내가 당신을 도울 것입니다 희망 : 그래서 이것은 내가 무엇을 얻을 수 있습니다.

테스트 됨 내 PC에서 위피와 유선 LAN을 연결하고 "VMware Player"에 대한 가상 카스는 거의 없으며 4 개의 IPv6를 얻은 다음 4 개의 IPv4로 포기합니다. IPv4 만 필요한 경우 다음을 사용할 수 있습니다.

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's 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(); 
} 

그런 다음 IPv4 만 얻을 수 있습니다. 단지 숫자는 4에서 시작됩니다.하지만 나에게는 괜찮습니다. 번호 매기기를 다시 계산하기 위해 새 변수를 추가 할 수 있습니다.

관련 문제