查到的本身就是一个列表,通常取第一个,使用gethostname ,gethostbyname

解决方案 »

  1.   

    GetAdaptersAddresses()
    不知道你要做什么,这个函数可以获取全部IP,但是会包含进虚拟设备的IP请留意。
      

  2.   

    GetAdaptersAddresses() 怎么用?
    有具体的实现吗?谢谢。帮个忙吧。
      

  3.   

    有个msn的例子
    PIP_ADAPTER_ADDRESSES pAddresses;
    pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(sizeof(IP_ADAPTER_ADDRESSES));
    ULONG outBufLen = 0;
    DWORD dwRetVal = 0;// Make an initial call to GetAdaptersAddresses to get the 
    // size needed into the outBufLen variable
    if (GetAdaptersAddresses(AF_INET, 
      0, 
      NULL, 
      pAddresses, 
      &outBufLen) == ERROR_BUFFER_OVERFLOW) {
      GlobalFree(pAddresses);
      pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(outBufLen);
    }// Make a second call to GetAdapters Addresses to get the
    // actual data we want
    if ((dwRetVal = GetAdaptersAddresses(AF_INET, 
      0, 
      NULL, 
      pAddresses, 
      &outBufLen)) == NO_ERROR) {
      // If successful, output some information from the data we received
      while (pAddresses) {
        printf("\tFriendly name: %S\n", pAddresses->FriendlyName);
        printf("\tDescription: %S\n", pAddresses->Description);
        pAddresses = pAddresses->Next;
      }
    }
    else { 
      printf("Call to GetAdaptersAddresses failed.\n");
      LPVOID lpMsgBuf;
      if (FormatMessage( 
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM | 
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dwRetVal,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
        (LPTSTR) &lpMsgBuf,
        0,
        NULL )) {
        printf("\tError: %s", lpMsgBuf);
      }
      LocalFree( lpMsgBuf );
    }

      

  4.   

    to:xujianlane
    我尝试了这段代码,为什么我把winsock.h之类的文件include近来后,还是找不到一些变量和方法呢?比如PIP_ADAPTER_ADDRESSES就找不到啊?
      

  5.   

    #include <winsock2.h>
    #include <Ws2tcpip.h>
      

  6.   

    这个我已经添加了。而且新建了个工程,单纯添加了这个功能。
    但还是报错。错误如下:
    D:\vcproject\iptest\iptest.cpp(10) : error C2065: 'PIP_ADAPTER_ADDRESSES' : undeclared identifier
    D:\vcproject\iptest\iptest.cpp(10) : error C2146: syntax error : missing ';' before identifier 'pAddresses'
    D:\vcproject\iptest\iptest.cpp(10) : error C2065: 'pAddresses' : undeclared identifier
    D:\vcproject\iptest\iptest.cpp(11) : error C2065: 'IP_ADAPTER_ADDRESSES' : undeclared identifier
    D:\vcproject\iptest\iptest.cpp(11) : error C2059: syntax error : ')'
    D:\vcproject\iptest\iptest.cpp(17) : error C2065: 'GetAdaptersAddresses' : undeclared identifier
    D:\vcproject\iptest\iptest.cpp(23) : error C2059: syntax error : ')'
    D:\vcproject\iptest\iptest.cpp(35) : error C2227: left of '->FriendlyName' must point to class/struct/union
    D:\vcproject\iptest\iptest.cpp(36) : error C2227: left of '->Description' must point to class/struct/union
    D:\vcproject\iptest\iptest.cpp(37) : error C2227: left of '->Next' must point to class/struct/union
      

  7.   

    这种情况下,如果你用的是.net环境,只要在代码中的这个类型上点右键,在快捷菜单中就可选择  转到定义   这项,接着你就应该怎么都知道了。
      

  8.   

    嗯,我用的是VC6.0。这位GG,有没有办法帮忙呀?
      

  9.   

    我查了一下MSDN,这个函数GetAdaptersAddresses要求有点高:
    Client Requires Windows Vista or Windows XP. 
    Server Requires Windows Server 2008 or Windows Server 2003. 
    Header Declared in Iphlpapi.h.
     
    Library Use Iphlpapi.lib.
     
    DLL Requires Iphlpapi.dll.我的VC6是安装到WIN98上的,没有要求的.h和.lib及相应的.dll文件。我以前是用如下方法做的,当然也是在CSDN上学别人的,你不妨试一下。 char szHostName[128];
    CString ipaddr=""; WSADATA WsaData;
    WSAStartup(MAKEWORD(2,2), &WsaData);


    int rslt = gethostname(szHostName, 128);
    if( rslt == 0 )
    {
    struct hostent * pHost;
    pHost = gethostbyname(szHostName);
    hostent* mpHost = gethostbyaddr(*(pHost->h_addr_list), 4 ,AF_INET);
    for( int i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
    {
    ipaddr = ipaddr + inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]) + "\r\n";
      }
    MessageBox(ipaddr);
    }
    else
    {
    int nError = WSAGetLastError();
    ipaddr.Format("%d",nError);
    MessageBox("出错了:" + ipaddr);
    } WSACleanup();
    这个代码只要有winsock2.h和ws2_32.lib就行了。