c语言是没有办法的用API吧GetComputerName/GetComputerNameEx

解决方案 »

  1.   

    char a[128]
    gethostname(a,128)就ok了还可以取得ip
    char szhostname[128];
    if( gethostname(szhostname, 128) == 0 )
    {
    // get host adresses
    struct hostent * phost;
    int i;
      phost = gethostbyname(szhostname);
             m_hostname=szhostname;
    i=0;
    int j;
             int h_length=4;
    for( j = 0; j<h_length; j++ )
    {
     CString addr;
    if( j > 0 )
    str += "."; addr.Format("%u", (unsigned int)((unsigned
      char*)phost->h_addr_list[i])[j]);
    str += addr;
    }
      

  2.   

    char pLocalIP[32], pHostname[256];
    PHOSTENT hostinfo;if( gethostname ( pHostname, sizeof(pHostname)) == 0)
    {
    if((hostinfo = gethostbyname(pHostname)) != NULL)
    {
    strcpy(pLocalIP,inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list));
    strcpy(pHostname,hostinfo->h_name);
    }
    }