How to get hostname?

Windows:

 1 #include <windows.h>
 2 #include <stdio.h>
 3 
 4 void PrintHostName(void) {
 5     wchar_t hostname[80];
 6     memset(hostname,0,sizeof(hostname));
 7     DWORD  i= 80;
 8     GetComputerName(hostname,&i);
 9     printf("%s",hostname);
10 }

Mac /Linux:

#include <stdio.h>
#include <unistd.h>

void PrintHostName(void) {
    char name[80] = {0};
    gethostname(name, sizeof(name));
    printf("hostname: %s\n", name);
}

 

posted @ 2013-03-22 23:45  PenguinChi  阅读(145)  评论(0)    收藏  举报