forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetcomputername.cpp
More file actions
30 lines (25 loc) · 779 Bytes
/
Copy pathgetcomputername.cpp
File metadata and controls
30 lines (25 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//! @file getcomputername.cpp
//! @author George Fleming <v-geflem@microsoft>
//! @brief Implements GetComputerName Win32 API
#include "getcomputername.h"
#include <errno.h>
#include <unistd.h>
#include <string>
//! @brief GetComputerName retrieves the name of the host associated with
//! the current thread.
//!
//! @retval username as UTF-8 string, or null if unsuccessful
char* GetComputerName()
{
errno = 0;
// Get computername from system, note that gethostname(2) gets the
// nodename from uname
std::string computername(_POSIX_HOST_NAME_MAX, 0);
int32_t ret = gethostname(&computername[0], computername.length());
// Map errno to Win32 Error Codes
if (ret != 0)
{
return NULL;
}
return strdup(computername.c_str());
}