-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
enumser.h
143 lines (107 loc) · 5.89 KB
/
enumser.h
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
/*
Module : enumser.h
Purpose: Defines the interface for a class to enumerate the serial ports installed on a PC
using a number of different approaches
Copyright (c) 1998 - 2023 by PJ Naughter (Web: www.naughter.com, Email: [email protected])
All rights reserved.
Copyright / Usage Details:
You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise)
when your product is released in binary form. You are allowed to modify the source code in any way you want
except you cannot modify the copyright details at the top of each module. If you want to distribute source
code with your application, then you are only allowed to distribute versions released by the author. This is
to maintain a single distribution point for the source code.
*/
///////////////////////// Macros / Defines ////////////////////////////////////
#pragma once
#if _MSVC_LANG < 201703
#error CEnumerateSerial requires a minimum C++ language standard of /std:c++17
#endif //#if __cplusplus < 201703
#ifndef __ENUMSER_H__
#define __ENUMSER_H__
#ifndef CENUMERATESERIAL_EXT_CLASS
#define CENUMERATESERIAL_EXT_CLASS
#endif //#ifndef CENUMERATESERIAL_EXT_CLASS
///////////////////////// Includes ////////////////////////////////////////////
#ifndef __ATLBASE_H__
#pragma message("To avoid this message, please put atlbase.h in your pre compiled header (normally stdafx.h)")
#include <atlbase.h>
#endif //#ifndef __ATLBASE_H__
#ifndef _VECTOR_
#pragma message("To avoid this message, please put vector in your pre compiled header (normally stdafx.h)")
#include <vector>
#endif //#ifndef _VECTOR_
#ifndef _STRING_
#pragma message("To avoid this message, please put string in your pre compiled header (normally stdafx.h)")
#include <string>
#endif //#ifndef _STRING_
#ifndef _UTILITY_
#pragma message("To avoid this message, please put utility in your pre compiled header (normally stdafx.h)")
#include <utility>
#endif //#ifndef _UTILITY_
#ifndef __ATLSTR_H__
#pragma message("To avoid this message, please put atlstr.h in your pre compiled header (normally stdafx.h)")
#include <atlstr.h>
#endif //#ifndef __ATLSTR_H__
#if !defined(NO_CENUMERATESERIAL_USING_SETUPAPI1) || !defined(NO_CENUMERATESERIAL_USING_SETUPAPI2)
#include <winioctl.h>
#ifndef _INC_SETUPAPI
#pragma message("To avoid this message, please put setupapi.h in your pre compiled header (normally stdafx.h)")
#include <setupapi.h>
#endif //#ifndef _INC_SETUPAPI
#endif //#if !defined(NO_CENUMERATESERIAL_USING_SETUPAPI1) || !defined(NO_CENUMERATESERIAL_USING_SETUPAPI2)
///////////////////////// Classes /////////////////////////////////////////////
class CENUMERATESERIAL_EXT_CLASS CEnumerateSerial
{
public:
//Typedefs
using CPortsArray = std::vector<UINT>;
#ifdef _UNICODE
using String = std::wstring;
#else
using String = std::string;
#endif
using CNamesArray = std::vector<String>;
using CPortAndNamesArray = std::vector<std::pair<UINT, String>>;
//Methods
#ifndef NO_CENUMERATESERIAL_USING_CREATEFILE
static _Return_type_success_(return != false) bool UsingCreateFile(_Inout_ CPortsArray& ports);
#endif //#ifndef NO_CENUMERATESERIAL_USING_CREATEFILE
#ifndef NO_CENUMERATESERIAL_USING_QUERYDOSDEVICE
static _Return_type_success_(return != false) bool UsingQueryDosDevice(_Inout_ CPortsArray& ports);
#endif //#ifndef NO_CENUMERATESERIAL_USING_QUERYDOSDEVICE
#ifndef NO_CENUMERATESERIAL_USING_GETDEFAULTCOMMCONFIG
static _Return_type_success_(return != false) bool UsingGetDefaultCommConfig(_Inout_ CPortsArray& ports);
#endif //#ifndef NO_CENUMERATESERIAL_USING_GETDEFAULTCOMMCONFIG
#ifndef NO_CENUMERATESERIAL_USING_SETUPAPI1
static _Return_type_success_(return != false) bool UsingSetupAPI1(_Inout_ CPortAndNamesArray& ports);
#endif //#ifndef NO_CENUMERATESERIAL_USING_SETUPAPI1
#ifndef NO_CENUMERATESERIAL_USING_SETUPAPI2
static _Return_type_success_(return != false) bool UsingSetupAPI2(_Inout_ CPortAndNamesArray& ports);
#endif //#ifndef NO_CENUMERATESERIAL_USING_SETUPAPI2
#ifndef NO_CENUMERATESERIAL_USING_ENUMPORTS
static _Return_type_success_(return != false) bool UsingEnumPorts(_Inout_ CPortAndNamesArray& ports);
#endif //#ifndef NO_CENUMERATESERIAL_USING_ENUMPORTS
#ifndef NO_CENUMERATESERIAL_USING_WMI
static HRESULT UsingWMI(_Inout_ CPortAndNamesArray& ports);
#endif //#ifndef NO_CENUMERATESERIAL_USING_WMI
#ifndef NO_CENUMERATESERIAL_USING_COMDB
static _Return_type_success_(return != false) bool UsingComDB(_Inout_ CPortsArray& ports);
#endif //#ifndef NO_CENUMERATESERIAL_USING_COMDB
#ifndef NO_CENUMERATESERIAL_USING_REGISTRY
static _Return_type_success_(return != false) bool UsingRegistry(_Inout_ CNamesArray& ports);
#endif //#ifndef NO_CENUMERATESERIAL_USING_REGISTRY
#ifndef NO_CENUMERATESERIAL_USING_GETCOMMPORTS
static _Return_type_success_(return != false) bool UsingGetCommPorts(_Inout_ CPortsArray& ports);
#endif //#ifndef NO_CENUMERATESERIAL_USING_REGISTRY
protected:
//Methods
#if !defined(NO_CENUMERATESERIAL_USING_SETUPAPI1) || !defined(NO_CENUMERATESERIAL_USING_SETUPAPI2)
static _Return_type_success_(return != false) bool RegQueryValueString(_In_ ATL::CRegKey& key, _In_ LPCTSTR lpValueName, _Inout_ String& sValue);
static _Return_type_success_(return != false) bool QueryRegistryPortName(_In_ ATL::CRegKey& deviceKey, _Out_ int& nPort);
static _Return_type_success_(return != false) bool QueryUsingSetupAPI(_In_ const GUID& guid, _In_ DWORD dwFlags, _Inout_ CPortAndNamesArray& ports);
static _Return_type_success_(return != false) bool QueryDeviceDescription(_In_ HDEVINFO hDevInfoSet, _In_ SP_DEVINFO_DATA& devInfo, _Inout_ String& sFriendlyName);
#endif //#if !defined(NO_CENUMERATESERIAL_USING_SETUPAPI1) || !defined(NO_CENUMERATESERIAL_USING_SETUPAPI2)
static _Return_type_success_(return != false) bool IsNumeric(_In_z_ LPCSTR pszString, _In_ bool bIgnoreColon) noexcept;
static _Return_type_success_(return != false) bool IsNumeric(_In_z_ LPCWSTR pszString, _In_ bool bIgnoreColon) noexcept;
};
#endif //#ifndef __ENUMSER_H__