#include <winsock2.h>#pragma comment(lib,"ws2_32.lib")main()
{
      WORD wVersionRequested;
      WSADATA wsaData;
      char name[255];
      CString ip;
      PHOSTENT hostinfo;
      wVersionRequested = MAKEWORD( 2, 0 );      if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
      {            if( gethostname ( name, sizeof(name)) == 0)
            {
                  if((hostinfo = gethostbyname(name)) != NULL)
                  {
                        ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
                  }
            }
            
            WSACleanup( );
      } 
}

解决方案 »

  1.   

    #include <winsock.h>
    #pragma comment(lib,"ws2_32")
    BOOL CPlayerTypeDlg::GetLocalIP(BYTE &nField00, BYTE &nField01, BYTE &nField02, BYTE &nField03)
    {
        WORD wVersionRequested;
        WSADATA wsaData;
        char szHostName[128];
        wVersionRequested=MAKEWORD(2,0);    if(WSAStartup(wVersionRequested,&wsaData)==0)
        {
            if(gethostname(szHostName,128)==0)
            {
                struct hostent* pHost;
                int i;            pHost=gethostbyname(szHostName); 
                for(i=0;pHost!=NULL && pHost->h_addr_list[i]!=NULL;i++)
                {
                    nField00=(unsigned int)((unsigned char*)pHost->h_addr_list[i])[0];
    nField01=(unsigned int)((unsigned char*)pHost->h_addr_list[i])[1];
    nField02=(unsigned int)((unsigned char*)pHost->h_addr_list[i])[2];
    nField03=(unsigned int)((unsigned char*)pHost->h_addr_list[i])[3];
                }
            }
            WSACleanup();
        }
    if ((nField00==0) && (nField02==0) && (nField01==0) && (nField03==0))
    return false;
    else
    return true;
    }