char  name[20];
  char* LocalIP = "IPERROR&NameERROR";   
  hostent * hostinfo=NULL;   
  if ( gethostname(name, sizeof(name))==0 )   
  {   
     hostinfo = gethostbyname(name);  //这行报错!异常 
   if(hostinfo != NULL)   
   {   
     LocalIP = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);   
   } 
     sprintf(LocalIP, "%s&ComputerName:%s", LocalIP, name) ; 
  }
  return LocalIP;兄弟们帮解决下呀,

解决方案 »

  1.   

    char hostname[20];
    gethostname(hostname,20);

    //get ipv4 address.
    hostent *pHost = ::gethostbyname(hostname);
    in_addr addr;
    char *p = pHost->h_addr_list[0];
    memcpy(&addr.S_un.S_addr,p,pHost->h_length);
    CString v4IP = ::inet_ntoa(addr);
      

  2.   

    我是做了个循环,第一次没有报错,过了5秒后再获取一次就报错了
    Unhandled exception in XXX.exe(WS2_32.DLL):0xC0000005:Access Violation为什么,第一次没问题,第2次就不行呢?
      

  3.   

    to fulingwei
    hostent *pHost = ::gethostbyname(hostname);
    还是报错!~
      

  4.   

    char* LocalIP = "IPERROR&NameERROR";  
      ........ 
      sprintf(LocalIP, "%s&ComputerName:%s", LocalIP, name) ; 这样使用好像会出现 LocalIP 缓冲区溢出。
      

  5.   

    我也觉得是没问题,可是...的确有问题,
    在我的XP下没问题,到了2000机器下问题就来了...
    单步调试第3次循环到::gethostbyname(hostname);过不去,没想明白
      

  6.   

    luohongming(lhm)说对了, 你乱往别人缓冲里面写东西...
      

  7.   

    to luohongming&jasonshark谢谢,指出错误,
    但是,报错的地方并不是这里,而是gethostbyname(hostname);
      

  8.   

    缓冲区溢出的话就可能引起连锁反应了,这个地方你改过来还是报错吗?
    出错的时候hostname里面的值是不是对的?