-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
171 lines (155 loc) · 6.35 KB
/
main.cpp
File metadata and controls
171 lines (155 loc) · 6.35 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "stdafx.h"
#include "enumser.h"
int main()
{
//Initialize COM (Required by CEnumerateSerial::UsingWMI)
HRESULT hr = CoInitialize(nullptr);
if (FAILED(hr))
{
#pragma warning(suppress: 26472)
_tprintf(_T("Failed to initialize COM, Error:%x\n"), static_cast<unsigned int>(hr));
return hr;
}
//Initialize COM security (Required by CEnumerateSerial::UsingWMI)
hr = CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE, nullptr);
if (FAILED(hr))
{
#pragma warning(suppress: 26472)
_tprintf(_T("Failed to initialize COM security, Error:%08X\n"), static_cast<unsigned int>(hr));
CoUninitialize();
return hr;
}
CEnumerateSerial::CPortsArray ports;
CEnumerateSerial::CPortAndNamesArray portAndNames;
CEnumerateSerial::CNamesArray names;
ULONGLONG nStartTick = 0;
#ifndef NO_CENUMERATESERIAL_USING_CREATEFILE
_tprintf(_T("CreateFile method reports\n"));
nStartTick = GetTickCount64();
if (CEnumerateSerial::UsingCreateFile(ports))
{
for (const auto& port : ports)
_tprintf(_T("COM%u\n"), port);
}
else
_tprintf(_T("CEnumerateSerial::UsingCreateFile failed, Error:%u\n"), GetLastError()); //NOLINT(clang-diagnostic-format)
_tprintf(_T(" Time taken: %I64u ms\n"), GetTickCount64() - nStartTick);
#endif //#ifndef NO_CENUMERATESERIAL_USING_CREATEFILE
#ifndef NO_CENUMERATESERIAL_USING_QUERYDOSDEVICE
_tprintf(_T("QueryDosDevice method reports\n"));
nStartTick = GetTickCount64();
if (CEnumerateSerial::UsingQueryDosDevice(ports))
{
for (const auto& port: ports)
_tprintf(_T("COM%u\n"), port);
}
else
_tprintf(_T("CEnumerateSerial::UsingQueryDosDevice failed, Error:%u\n"), GetLastError()); //NOLINT(clang-diagnostic-format)
_tprintf(_T(" Time taken: %I64u ms\n"), GetTickCount64() - nStartTick);
#endif //#ifndef NO_CENUMERATESERIAL_USING_QUERYDOSDEVICE
#ifndef NO_CENUMERATESERIAL_USING_GETDEFAULTCOMMCONFIG
_tprintf(_T("GetDefaultCommConfig method reports\n"));
nStartTick = GetTickCount64();
if (CEnumerateSerial::UsingGetDefaultCommConfig(ports))
{
for (const auto& port: ports)
_tprintf(_T("COM%u\n"), port);
}
else
_tprintf(_T("CEnumerateSerial::UsingGetDefaultCommConfig failed, Error:%u\n"), GetLastError()); //NOLINT(clang-diagnostic-format)
_tprintf(_T(" Time taken: %I64u ms\n"), GetTickCount64() - nStartTick);
#endif //#ifndef NO_CENUMERATESERIAL_USING_GETDEFAULTCOMMCONFIG
#ifndef NO_CENUMERATESERIAL_USING_SETUPAPI1
_tprintf(_T("Device Manager (SetupAPI - GUID_DEVINTERFACE_COMPORT) reports\n"));
nStartTick = GetTickCount64();
if (CEnumerateSerial::UsingSetupAPI1(portAndNames))
{
for (const auto& port : portAndNames)
#pragma warning(suppress: 26489)
_tprintf(_T("COM%u <%s>\n"), port.first, port.second.c_str());
}
else
_tprintf(_T("CEnumerateSerial::UsingSetupAPI1 failed, Error:%u\n"), GetLastError()); //NOLINT(clang-diagnostic-format)
_tprintf(_T(" Time taken: %I64u ms\n"), GetTickCount64() - nStartTick);
#endif //#ifndef NO_CENUMERATESERIAL_USING_SETUPAPI1
#ifndef NO_CENUMERATESERIAL_USING_SETUPAPI2
_tprintf(_T("Device Manager (SetupAPI - Ports Device information set) reports\n"));
nStartTick = GetTickCount64();
if (CEnumerateSerial::UsingSetupAPI2(portAndNames))
{
for (const auto& port : portAndNames)
#pragma warning(suppress: 26489)
_tprintf(_T("COM%u <%s>\n"), port.first, port.second.c_str());
}
else
_tprintf(_T("CEnumerateSerial::UsingSetupAPI2 failed, Error:%u\n"), GetLastError()); //NOLINT(clang-diagnostic-format)
_tprintf(_T(" Time taken: %I64u ms\n"), GetTickCount64() - nStartTick);
#endif //#ifndef NO_CENUMERATESERIAL_USING_SETUPAPI2
#ifndef NO_CENUMERATESERIAL_USING_ENUMPORTS
_tprintf(_T("EnumPorts method reports\n"));
nStartTick = GetTickCount64();
if (CEnumerateSerial::UsingEnumPorts(portAndNames))
{
for (const auto& port : portAndNames)
#pragma warning(suppress: 26489)
_tprintf(_T("COM%u <%s>\n"), port.first, port.second.c_str());
}
else
_tprintf(_T("CEnumerateSerial::UsingEnumPorts failed, Error:%u\n"), GetLastError()); //NOLINT(clang-diagnostic-format)
_tprintf(_T(" Time taken: %I64u ms\n"), GetTickCount64() - nStartTick);
#endif //#ifndef NO_CENUMERATESERIAL_USING_ENUMPORTS
#ifndef NO_CENUMERATESERIAL_USING_WMI
_tprintf(_T("WMI method reports\n"));
nStartTick = GetTickCount64();
hr = CEnumerateSerial::UsingWMI(portAndNames);
if (SUCCEEDED(hr))
{
for (const auto& port : portAndNames)
#pragma warning(suppress: 26489)
_tprintf(_T("COM%u <%s>\n"), port.first, port.second.c_str());
}
else
#pragma warning(suppress: 26472)
_tprintf(_T("CEnumerateSerial::UsingWMI failed, Error:%08X\n"), static_cast<unsigned int>(hr));
_tprintf(_T("Time taken: %I64u ms\n"), GetTickCount64() - nStartTick);
#endif //#ifndef NO_CENUMERATESERIAL_USING_WMI
#ifndef NO_CENUMERATESERIAL_USING_COMDB
_tprintf(_T("ComDB method reports\n"));
nStartTick = GetTickCount64();
if (CEnumerateSerial::UsingComDB(ports))
{
for (const auto& port : ports)
_tprintf(_T("COM%u\n"), port);
}
else
_tprintf(_T("CEnumerateSerial::UsingComDB failed, Error:%u\n"), GetLastError()); //NOLINT(clang-diagnostic-format)
_tprintf(_T(" Time taken: %I64u ms\n"), GetTickCount64() - nStartTick);
#endif //#ifndef NO_CENUMERATESERIAL_USING_COMDB
#ifndef NO_CENUMERATESERIAL_USING_REGISTRY
_tprintf(_T("Registry method reports\n"));
nStartTick = GetTickCount64();
if (CEnumerateSerial::UsingRegistry(names))
{
for (const auto& name : names)
_tprintf(_T("%s\n"), name.c_str());
}
else
_tprintf(_T("CEnumerateSerial::UsingRegistry failed, Error:%u\n"), GetLastError()); //NOLINT(clang-diagnostic-format)
_tprintf(_T(" Time taken: %I64u ms\n"), GetTickCount64() - nStartTick);
#endif //#ifndef NO_CENUMERATESERIAL_USING_REGISTRY
#ifndef NO_CENUMERATESERIAL_USING_GETCOMMPORTS
_tprintf(_T("UsingGetCommPorts method reports\n"));
nStartTick = GetTickCount64();
if (CEnumerateSerial::UsingGetCommPorts(ports))
{
for (const auto& port : ports)
_tprintf(_T("COM%u\n"), port);
}
else
_tprintf(_T("CEnumerateSerial::UsingGetCommPorts failed, Error:%u\n"), GetLastError()); //NOLINT(clang-diagnostic-format)
_tprintf(_T(" Time taken: %I64u ms\n"), GetTickCount64() - nStartTick);
#endif //#ifndef NO_CENUMERATESERIAL_USING_GETCOMMPORTS
//Close down COM
CoUninitialize();
return 0;
}