获取主机名(C ++)

时间:2012-08-06 12:20:29

标签: c++

  

可能重复:
  C++ Windows function call that get local hostname and IP address

我尝试了很多。如何在c ++中获取主机名。我正在使用Windows 7。 请有人帮助我。

我用

的#include “unistd.h中”

1 个答案:

答案 0 :(得分:3)

#include<stdio.h>
#include<Winsock2.h>
#pragma comment(lib, "Ws2_32.lib")
int main()
{
    char szPath[128] = "";
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);
    gethostname(szPath, sizeof(szPath));
    printf("%s", szPath);
    WSACleanup();
    return 0;
}