WinStd Additional templates and function helpers for Microsoft Windows using Standard C++ classes

WinSock2.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
7#pragma once
8
9#include "Common.h"
10#include <WinSock2.h>
11#include <ws2def.h>
12#include <WS2tcpip.h>
13
14 namespace winstd
15{
21
25 class ws2_runtime_error : public num_runtime_error<int>
26 {
27 public:
34 ws2_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<int>(num, msg)
35 {
36 }
37
44 ws2_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) : num_runtime_error<int>(num, msg)
45 {
46 }
47
53 ws2_runtime_error(_In_ const std::string& msg) : num_runtime_error<int>(WSAGetLastError(), msg)
54 {
55 }
56
62 ws2_runtime_error(_In_opt_z_ const char *msg = nullptr) : num_runtime_error<int>(WSAGetLastError(), msg)
63 {
64 }
65
71 tstring msg(_In_opt_ DWORD dwLanguageId = 0) const
72 {
73 tstring str;
74 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, m_num, dwLanguageId, str, NULL)) {
75 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
76 str.erase(str.find_last_not_of(_T(" \t\n\r\f\v")) + 1);
77 } else
78 sprintf(str, m_num >= 0x10000 ? _T("Error 0x%X") : _T("Error %u"), m_num);
79 return str;
80 }
81 };
82
84
87
88#if (NTDDI_VERSION >= NTDDI_WINXPSP2) || (_WIN32_WINNT >= 0x0502)
89
93 class addrinfo : public handle<PADDRINFOT, NULL>
94 {
95 WINSTD_HANDLE_IMPL(addrinfo, NULL)
96
97 public:
103 bool get(
104 _In_opt_ PCTSTR pNodeName,
105 _In_opt_ PCTSTR pServiceName,
106 _In_opt_ const ADDRINFOT *pHints)
107 {
108 handle_type h;
109 if (GetAddrInfo(pNodeName, pServiceName, pHints, &h) == 0) {
110 attach(h);
111 return true;
112 } else
113 return false;
114 }
115
121 virtual ~addrinfo()
122 {
123 if (m_h != invalid)
124 free_internal();
125 }
126
127 protected:
133 void free_internal() noexcept override
134 {
135 FreeAddrInfo(m_h);
136 }
137 };
138
139#endif
140
142}
winstd::addrinfo
SID wrapper class.
Definition: WinSock2.h:94
winstd::addrinfo::free_internal
void free_internal() noexcept override
Frees address information.
Definition: WinSock2.h:133
winstd::addrinfo::get
bool get(_In_opt_ PCTSTR pNodeName, _In_opt_ PCTSTR pServiceName, _In_opt_ const ADDRINFOT *pHints)
Provides protocol-independent translation from a host name to an address.
Definition: WinSock2.h:103
winstd::addrinfo::~addrinfo
virtual ~addrinfo()
Frees address information.
Definition: WinSock2.h:121
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition: Common.h:580
winstd::handle< PADDRINFOT, NULL >::handle_type
PADDRINFOT handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:585
winstd::handle< PADDRINFOT, NULL >::attach
void attach(_In_opt_ handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:794
winstd::handle< PADDRINFOT, NULL >::m_h
handle_type m_h
Object handle.
Definition: Common.h:833
winstd::num_runtime_error
Numerical runtime error.
Definition: Common.h:981
winstd::num_runtime_error< int >::error_type
int error_type
Error number type.
Definition: Common.h:983
winstd::num_runtime_error< int >::m_num
error_type m_num
Numeric error code.
Definition: Common.h:1019
winstd::ws2_runtime_error
WinSock2 runtime error.
Definition: WinSock2.h:26
winstd::ws2_runtime_error::msg
tstring msg(_In_opt_ DWORD dwLanguageId=0) const
Returns a user-readable Windows error message.
Definition: WinSock2.h:71
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error(_In_ const std::string &msg)
Constructs an exception using WSAGetLastError()
Definition: WinSock2.h:53
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error(_In_ error_type num, _In_ const std::string &msg)
Constructs an exception.
Definition: WinSock2.h:34
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error(_In_opt_z_ const char *msg=nullptr)
Constructs an exception using WSAGetLastError()
Definition: WinSock2.h:62
winstd::ws2_runtime_error::ws2_runtime_error
ws2_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg=nullptr)
Constructs an exception.
Definition: WinSock2.h:44
winstd::tstring
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition: Common.h:311
WINSTD_HANDLE_IMPL
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:138
winstd::handle< PADDRINFOT, NULL >::invalid
static const PADDRINFOT invalid
Invalid handle value.
Definition: Common.h:590