我的方法是(应该是很多人都在用的方法)
char szHostName[128];
gethostname(szHostName, 128);
//...
pHost = gethostbyname(szHostName);
//...可以是可以,但是调试的时候 1 句 gethostname 就会导致输出"极有可能导致异常"的信息警告:
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).如果按F5 debug,经过获取IP地址的代码后,Output窗口里没有上述信息的,请告诉我,送上高分讨论:)

解决方案 »

  1.   

    char name[100],Hostaddress[200];
    gethostname(name,sizeof(kk));
    AfxMessageBox(kk);

     struct hostent *pHostEnt;
     pHostEnt = gethostbyname(name);
     if( pHostEnt != NULL ){
     sprintf( Hostaddress,"%d.%d.%d.%d",
    ( pHostEnt->h_addr_list[0][0]&0x00ff ),
    ( pHostEnt->h_addr_list[0][1]&0x00ff ),
    ( pHostEnt->h_addr_list[0][2]&0x00ff ),
    ( pHostEnt->h_addr_list[0][3]&0x00ff ) );
    }
          
     AfxMessageBox(Hostaddress);
      

  2.   

    #include <winsock.h>
    #include <wsipx.h>
    #include <wsnwlink.h>
    #include <stdio.h>int main()
    {
       ////////////////
       // 初始化 Windows sockets API. 要求版本为 version 1.1
       //
       WORD wVersionRequested = MAKEWORD(1, 1);
       WSADATA wsaData;
       if (WSAStartup(wVersionRequested, &wsaData)) {
          printf("WSAStartup failed %s\n", WSAGetLastError());
          return -1;
       }   //////////////////
       // 获得主机名.
       //
       char hostname[256];
       int res = gethostname(hostname, sizeof(hostname));
       if (res != 0) {
          printf("Error: %u\n", WSAGetLastError());
          return -1;
       }
       printf("hostname=%s\n", hostname);
       ////////////////
       // 根据主机名获取主机信息. 
       //
       hostent* pHostent = gethostbyname(hostname);
       if (pHostent==NULL) {
          printf("Error: %u\n", WSAGetLastError());
          return -1;
       }
       //////////////////
       // 解析返回的hostent信息.
       //
       hostent& he = *pHostent;
       printf("name=%s\naliases=%s\naddrtype=%d\nlength=%d\n",
          he.h_name, he.h_aliases, he.h_addrtype, he.h_length);
       
       sockaddr_in sa;
       for (int nAdapter=0; he.h_addr_list[nAdapter]; nAdapter++) {
          memcpy ( &sa.sin_addr.s_addr, he.h_addr_list[nAdapter],he.h_length);
          // 输出机器的IP地址.
          printf("Address: %s\n", inet_ntoa(sa.sin_addr)); // 显示地址串
       }
       //////////////////
       // 终止 Windows sockets API
       //
       WSACleanup();
       return 0;
    }
      

  3.   

    已经初始化过了
    WSAStartup(MAKEWORD(2,0), &wsaData);没有用,一样会有输出的,难道你们的不会吗?请仔细看一下Output窗口的调试信息
      

  4.   

    我试着运行了一下 zhucde(【风间苍月】) 的例子(F5),在调试窗口中一样会出现 First-chance exception 信息,如下所示:...
    Loaded 'C:\WINNT\system32\shlwapi.dll', no matching symbolic information found.
    Loaded 'C:\WINNT\system32\DHCPCSVC.DLL', no matching symbolic information found.
    First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
    First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
    Loaded 'C:\WINNT\system32\winrnr.dll', no matching symbolic information found.
    First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
    First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
    First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
    First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
    Loaded 'C:\WINNT\system32\rasadhlp.dll', no matching symbolic information found.
    ...我的问题并不在获取IP地址本身,而是在获取IP地址的时候引发的访问异常警告:(
      

  5.   

    xiao_potato(小土豆),能不能将你的调试输出窗口的结果复制粘贴出来让我看看
    就是有很多Loaded 'C:\WINNT\system32\rasadhlp.dll', no matching .... 的那些,谢谢了
      

  6.   

    拿去直接用就行,这个是取得本机计算机名,下一个是取得本地IP,都是独立的,直接把函数拿去帖上就可以了。
    CString GetLocalHostName()
    {
    WORD   wVersionRequested;
    WSADATA wsaData;
    int    err; 
    CString csSU;
    wVersionRequested = MAKEWORD( 2, 0 ); 
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) 
    {
    // Couldn't find a usable WinSock DLL. 
    csSU="没有找到可用的Winsock DLL!";    
    return csSU;


    // Confirm that the WinSock DLL supports 2.0.
    // Note that if the DLL supports versions greater
    // than 2.0 in addition to 2.0, it will still return
    // 2.0 in wVersion since that is the version we
    // requested. 
    if ( LOBYTE( wsaData.wVersion ) != 2 ||
            HIBYTE( wsaData.wVersion ) != 0 ) 
    {
    // Couldn't find a usable WinSock DLL.
    csSU="没有找到可用的Winsock DLL!"; 
    WSACleanup( );
    return csSU; 


    char szHostName[256];
    int  nRetCode;
    CString sHostName;
    nRetCode = gethostname(szHostName, sizeof(szHostName));

    if (nRetCode != 0)
    {
    // An error has occurred
    sHostName = _T("Not available");;
    return sHostName;
    }



    nRetCode = WSACleanup();
    if (nRetCode != 0)
    {
    // An error occured. 
    sHostName = _T("发生错误!");;
    return sHostName;
    }

    CString csHost=szHostName;

    return (csHost);

    }
    取得IP:
    CString GetLocalIP()
    { WORD   wVersionRequested;
    WSADATA wsaData;
    int    err; 
    CString csSU;
    wVersionRequested = MAKEWORD( 2, 0 ); 
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) 
    {
    // Couldn't find a usable WinSock DLL. 
    csSU="没有找到可用的Winsock DLL!";    
    return csSU;


    // Confirm that the WinSock DLL supports 2.0.
    // Note that if the DLL supports versions greater
    // than 2.0 in addition to 2.0, it will still return
    // 2.0 in wVersion since that is the version we
    // requested. 
    if ( LOBYTE( wsaData.wVersion ) != 2 ||
            HIBYTE( wsaData.wVersion ) != 0 ) 
    {
    // Couldn't find a usable WinSock DLL.
    csSU="没有找到可用的Winsock DLL!"; 
    WSACleanup( );
    return csSU; 




    char szHostName[256];
    int  nRetCode;
    CString sHostName;
    nRetCode = gethostname(szHostName, sizeof(szHostName));

    if (nRetCode != 0)
    {
    // An error has occurred
    sHostName = _T("Not available");;
    return sHostName;
    }

    CString sIPAddress;CString _HostName;
    struct hostent FAR *lpHostEnt = gethostbyname (szHostName);

    if (lpHostEnt == NULL)
    {
    // An error occurred. 
    sIPAddress = _T("");
    _HostName = _T("发生错误!");
    return _HostName;
    }

    LPSTR lpAddr = lpHostEnt->h_addr_list[0];
    if (lpAddr) 
    {
    struct in_addr  inAddr;
    memmove (&inAddr, lpAddr, 4);
    sIPAddress = inet_ntoa (inAddr);
    if (sIPAddress.IsEmpty())
    {
    _HostName = _T("Not available!");
    sIPAddress = _T("Not available");
    AfxMessageBox(_HostName);
    return _HostName;
    }
    }
    nRetCode = WSACleanup();
    if (nRetCode != 0)
    {
    // An error occured. 
    sHostName = _T("发生错误!");;
    return sHostName;
    }



    return (sIPAddress);
    }
      

  7.   

    好像没人看懂我的意思,我完全可以成功而没有任何错误地获取本地IP地址
                                    ………………………………………………
    我的问题是如何避免调试输出窗口的诸多警告信息
    First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).这些只是警告信息,这些代码本身的运行一点问题都没有,一点问题都没有,一切正常
                                       ……………………要不有人和我解释一下
    First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
    ^^^^^^^^^^^^^^^^^^^^^^                             ^^^^^^^^^^
    到底是什么回事也可以谢谢
      

  8.   

    取决于机器的编译环境,有的环境下编译的时候会出这个警告,有的环境不会。这是因为下面这样的语句:
    char szHostName[256];
    gethostname(szHostName, sizeof(szHostName));gethostname()用szHostName作为指针,在其指向的内存写入不定长的数据。有的编译环境认为这种写入内存的方法可能会导致异常,所以会提出First-chance exception的警告。