-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
120 lines (96 loc) · 2.85 KB
/
main.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <vector>
#include <Windows.h>
#include <iphlpapi.h>
using namespace std;
void callGetComputerNameExA()
{
const int size = 255;
char buffer[size] = {'\0'};
DWORD resultSize = size;
auto result = GetComputerNameExA(ComputerNameDnsHostname, buffer, &resultSize);
if (result != 0)
cout << "GetComputerNameEx() succeeded. Result : " << buffer << endl;
else
cout << "GetComputerNameEx() failed with error code : " << GetLastError() << endl;
}
void callGetNetworkParams()
{
FIXED_INFO info;
ULONG bufLen = sizeof(info);
auto result = GetNetworkParams(&info, &bufLen);
if (result == ERROR_SUCCESS)
cout << "GetNetworkParams() succeeded. Result : " << info.HostName << endl;
else
cout << "GetNetworkParams() failed with error code " << result << endl;
}
void callGetNetworkParams2()
{
PFIXED_INFO info = nullptr;
ULONG bufLen = 0;
auto result = GetNetworkParams(info, &bufLen);
std::vector<char> buffer(bufLen, '\0');
info = reinterpret_cast<FIXED_INFO*>(buffer.data());
result = GetNetworkParams(info, &bufLen);
if (result == ERROR_SUCCESS)
cout << "Two-step GetNetworkParams() succeeded. Result : " << info->HostName << endl;
else
cout << "Two-step GetNetworkParams() failed with error code " << result << endl;
}
int cygwin_gethostname(char* name, size_t len)
{
PFIXED_INFO info = NULL;
ULONG size = 0;
if (GetNetworkParams(info, &size) == ERROR_BUFFER_OVERFLOW
&& (info = (PFIXED_INFO)alloca(size))
&& GetNetworkParams(info, &size) == ERROR_SUCCESS)
{
strncpy(name, info->HostName, len);
return 0;
}
return -1;
}
void call_cygwin_gethostname()
{
const int NI_MAXHOST = 1025;
char buf[NI_MAXHOST + 1];
memset(buf, 0, sizeof buf);
auto result = cygwin_gethostname(buf, sizeof buf - 1);
if (result == 0)
cout << "cygwin_gethostname() succeeded. Result : " << buf << endl;
else
cout << "cygwin_gethostname() failed with error code " << result << endl;
}
void call_git_hostname()
{
auto result = system("\"c:\\Program Files\\Git\\usr\\bin\\hostname.exe\"");
if (result == 0)
cout << "Calling git hostname succeeded." << endl;
else
cout << "Calling git hostname failed with error code " << result << endl;
}
void call_git_uname()
{
auto result = system("\"c:\\Program Files\\Git\\usr\\bin\\uname.exe\"");
if (result == 0)
cout << "Calling git uname succeeded." << endl;
else
cout << "Calling git uname failed with error code " << result << endl;
}
int main()
{
cout << "Testing GetComputerNameEx()" << endl;
callGetComputerNameExA();
cout << "Testing GetNetworkParams()" << endl;
callGetNetworkParams();
cout << "Testing 2-step GetNetworkParams()" << endl;
callGetNetworkParams2();
cout << "Testing cygwin_gethostname()" << endl;
call_cygwin_gethostname();
cout << "Testing git hostname" << endl;
call_git_hostname();
cout << "Testing git uname" << endl;
call_git_uname();
return 0;
}