用什么函数能够得到主机名呢?还有一个小问题,现在登录的用户名怎么取得呢?

解决方案 »

  1.   

    专门为PB,VB程序获取本机IP而制作的动态库(2008.06.02更新)http://www.cnblogs.com/SummerHeart/archive/2008/05/17/1201330.html
      

  2.   

    int gethostname(char *name, size_t len)这个函数,调用后,会将主机名保存在name里面。而len是name的大小。以下是例程,编译后只需要运行就知道自己的主机名字了。知道自己名字后,我再调用了一下gethostbyname()来得到主机的一些其他信息。#include <netdb.h>
    #include <sys/socket.h>int main(int argc, char **argv)
    {
     struct hostent *hptr;
     char **pptr;
     char hostname[32];
     char str[32];
     
     if( gethostname(hostname,sizeof(hostname)) )
     {
      printf("gethostname calling error\n");
      return 1;
     }
     printf("localhost name:%s\n",hostname);
     if( (hptr = gethostbyname(hostname)) == NULL)
     {
      printf("gethostbyname calling error\n");
      return 1;
     }
     pptr=hptr->h_addr_list;
     for(;*pptr!=NULL;pptr++)
      printf("  address:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
     
     return 0;
    }
      

  3.   

    int DllExport __stdcall  Gethost(char *name)
    {
    WORD wVersionRequested; 
    WSADATA wsaData; 
    int err; 
    wVersionRequested = MAKEWORD( 2, 2 ); 
    err = WSAStartup( wVersionRequested, &wsaData ); 
    if ( err != 0 ) 

    return -1; 

    char szhn[256]; 
    int nStatus = gethostname(szhn, sizeof(szhn)); 
    if (nStatus == SOCKET_ERROR ) 

    return -1; 

    WSACleanup(); 
    strcpy(name,szhn) ;
    return 0;
    }
      

  4.   

    调用了这个函数后,程序说找不到<netdb.h> 和<sys/socket.h> ,这两个头文件不存在
      

  5.   

    调用后,会报出很多的错,是不是需要头文件呢?MFC中有什么方法可以得到主机名称呢?
    谢谢答复!
      

  6.   

    哦,解决了。
    原来是需要 Winsock2.h 这个头文件。
    谢谢大家
    散分!