diff --git a/include/WinStd/COM.h b/include/WinStd/COM.h index dbdddc77..c6fc14ae 100644 --- a/include/WinStd/COM.h +++ b/include/WinStd/COM.h @@ -88,6 +88,7 @@ namespace winstd /// /// \sa [CoCreateInstance function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686615.aspx) /// + __declspec(deprecated("Use CoCreateInstance")) com_obj(_In_ REFCLSID rclsid, _In_opt_ LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) { CoCreateInstance(rclsid, pUnkOuter, dwClsContext, __uuidof(T), (LPVOID*)&m_h); @@ -130,6 +131,7 @@ namespace winstd /// /// \sa [CoCreateInstance function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686615.aspx) /// + __declspec(deprecated("Use CoCreateInstance")) HRESULT create(_In_ REFCLSID rclsid, _In_opt_ LPUNKNOWN pUnkOuter = NULL, _In_ DWORD dwClsContext = CLSCTX_ALL) { handle_type h; @@ -1085,3 +1087,23 @@ namespace winstd /// @} } + +/// \addtogroup WinStdCOM +/// @{ + +/// +/// Creates and default-initializes a single object of the class associated with a specified CLSID +/// +/// \sa [CoCreateInstance function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686615.aspx) +/// +template +static _Check_return_ HRESULT CoCreateInstance(_In_ REFCLSID rclsid, _In_opt_ LPUNKNOWN pUnkOuter, _In_ DWORD dwClsContext, _Inout_ winstd::com_obj &v) +{ + T* ppv; + HRESULT hr = CoCreateInstance(rclsid, pUnkOuter, dwClsContext, __uuidof(T), (LPVOID*)&ppv); + if (SUCCEEDED(hr)) + v.attach(ppv); + return hr; +} + +/// @} diff --git a/include/WinStd/Crypt.h b/include/WinStd/Crypt.h index 5c51de9f..f96afe86 100644 --- a/include/WinStd/Crypt.h +++ b/include/WinStd/Crypt.h @@ -273,6 +273,7 @@ namespace winstd /// /// \sa [CertCreateCertificateContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376033.aspx) /// + __declspec(deprecated("Use CertCreateCertificateContext")) bool create(_In_ DWORD dwCertEncodingType, _In_ LPCBYTE pbCertEncoded, _In_ DWORD cbCertEncoded) noexcept { handle_type h = CertCreateCertificateContext(dwCertEncodingType, pbCertEncoded, cbCertEncoded); @@ -422,6 +423,7 @@ namespace winstd /// /// \sa [CertGetCertificateChain function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376078.aspx) /// + __declspec(deprecated("Use CertGetCertificateChain")) bool create(_In_opt_ HCERTCHAINENGINE hChainEngine, _In_ PCCERT_CONTEXT pCertContext, _In_opt_ LPFILETIME pTime, _In_opt_ HCERTSTORE hAdditionalStore, _In_ PCERT_CHAIN_PARA pChainPara, _In_ DWORD dwFlags, __reserved LPVOID pvReserved = NULL) noexcept { handle_type h; @@ -486,6 +488,7 @@ namespace winstd /// /// \sa [CertOpenStore function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376559.aspx) /// + __declspec(deprecated("Use CertOpenStore")) bool create(_In_ LPCSTR lpszStoreProvider, _In_ DWORD dwEncodingType, _In_opt_ HCRYPTPROV_LEGACY hCryptProv, _In_ DWORD dwFlags, _In_opt_ const void *pvPara) noexcept { handle_type h = CertOpenStore(lpszStoreProvider, dwEncodingType, hCryptProv, dwFlags, pvPara); @@ -505,6 +508,7 @@ namespace winstd /// /// \sa [CertOpenSystemStore function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376560.aspx) /// + __declspec(deprecated("Use CertOpenSystemStore")) bool create(_In_opt_ HCRYPTPROV_LEGACY hCryptProv, _In_z_ LPCTSTR szSubsystemProtocol) noexcept { handle_type h = CertOpenSystemStore(hCryptProv, szSubsystemProtocol); @@ -555,6 +559,7 @@ namespace winstd /// /// \sa [CryptAcquireContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379886.aspx) /// + __declspec(deprecated("Use CryptAcquireContext")) bool create(_In_opt_z_ LPCTSTR szContainer, _In_opt_z_ LPCTSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags = 0) noexcept { handle_type h; @@ -605,6 +610,7 @@ namespace winstd /// /// \sa [CryptCreateHash function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379908.aspx) /// + __declspec(deprecated("Use CryptCreateHash")) bool create(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_opt_ HCRYPTKEY hKey = NULL, _In_opt_ DWORD dwFlags = 0) noexcept { handle_type h; @@ -637,7 +643,7 @@ namespace winstd /// handle_type duplicate_internal(_In_ handle_type h) const noexcept override { - handle_type hNew = invalid; + handle_type hNew; return CryptDuplicateHash(h, NULL, 0, &hNew) ? hNew : invalid; } }; @@ -666,6 +672,7 @@ namespace winstd /// /// \sa [CryptGenKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379941.aspx) /// + __declspec(deprecated("Use CryptGenKey")) bool generate(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwFlags) noexcept { handle_type h; @@ -681,6 +688,7 @@ namespace winstd /// /// \sa [CryptImportKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380207.aspx) /// + __declspec(deprecated("Use CryptImportKey")) bool import(_In_ HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE pbData, _In_ DWORD dwDataLen, _In_ HCRYPTKEY hPubKey, _In_ DWORD dwFlags) noexcept { handle_type h; @@ -696,6 +704,7 @@ namespace winstd /// /// \sa [CryptImportPublicKeyInfo function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380209.aspx) /// + __declspec(deprecated("Use CryptImportPublicKeyInfo")) bool import_public(_In_ HCRYPTPROV hCryptProv, _In_ DWORD dwCertEncodingType, _In_ PCERT_PUBLIC_KEY_INFO pInfo) noexcept { handle_type h; @@ -711,6 +720,7 @@ namespace winstd /// /// \sa [CryptDeriveKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379916.aspx) /// + __declspec(deprecated("Use CryptDeriveKey")) bool derive(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTHASH hBaseData, _In_ DWORD dwFlags) noexcept { handle_type h; @@ -813,7 +823,7 @@ namespace winstd /// handle_type duplicate_internal(_In_ handle_type h) const noexcept override { - handle_type hNew = invalid; + handle_type hNew; return CryptDuplicateKey(h, NULL, 0, &hNew) ? hNew : invalid; } }; @@ -943,3 +953,121 @@ namespace winstd /// @} } + +/// \addtogroup WinStdCryptoAPI +/// @{ + +#pragma warning(push) +#pragma warning(disable: 4505) // Don't warn on unused code + +/// +/// The CertGetCertificateChain function builds a certificate chain context starting from an end certificate and going back, if possible, to a trusted root certificate. +/// +/// \sa [CertGetCertificateChain function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376078.aspx) +/// +static BOOL CertGetCertificateChain(_In_opt_ HCERTCHAINENGINE hChainEngine, _In_ PCCERT_CONTEXT pCertContext, _In_opt_ LPFILETIME pTime, _In_opt_ HCERTSTORE hAdditionalStore, _In_ PCERT_CHAIN_PARA pChainPara, _In_ DWORD dwFlags, _Reserved_ LPVOID pvReserved, _Inout_ winstd::cert_chain_context &ctx) +{ + PCCERT_CHAIN_CONTEXT pChainContext; + BOOL bResult = CertGetCertificateChain(hChainEngine, pCertContext, pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, &pChainContext); + if (bResult) + ctx.attach(pChainContext); + return bResult; +} + +/// @copydoc CryptAcquireContextW() +static BOOL CryptAcquireContextA(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCSTR szContainer, _In_opt_ LPCSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags) +{ + HCRYPTPROV h; + BOOL bResult = CryptAcquireContextA(&h, szContainer, szProvider, dwProvType, dwFlags); + if (bResult) + prov.attach(h); + return bResult; +} + +/// +/// Acquires the cryptographic context. +/// +/// \sa [CryptAcquireContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379886.aspx) +/// +static BOOL CryptAcquireContextW(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCWSTR szContainer, _In_opt_ LPCWSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags) +{ + HCRYPTPROV h; + BOOL bResult = CryptAcquireContextW(&h, szContainer, szProvider, dwProvType, dwFlags); + if (bResult) + prov.attach(h); + return bResult; +} + +/// +/// Creates the hash context. +/// +/// \sa [CryptCreateHash function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379908.aspx) +/// +static BOOL CryptCreateHash(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTKEY hKey, _In_ DWORD dwFlags, _Inout_ winstd::crypt_hash &hash) +{ + HCRYPTHASH h; + BOOL bResult = CryptCreateHash(hProv, Algid, hKey, dwFlags, &h); + if (bResult) + hash.attach(h); + return bResult; +} + +/// +/// Generates the key. +/// +/// \sa [CryptGenKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379941.aspx) +/// +static BOOL CryptGenKey(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key) +{ + HCRYPTKEY h; + BOOL bResult = CryptGenKey(hProv, Algid, dwFlags, &h); + if (bResult) + key.attach(h); + return bResult; +} + +/// +/// Imports the key. +/// +/// \sa [CryptImportKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380207.aspx) +/// +static bool CryptImportKey(_In_ HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE pbData, _In_ DWORD dwDataLen, _In_ HCRYPTKEY hPubKey, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key) +{ + HCRYPTKEY h; + BOOL bResult = CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, &h); + if (bResult) + key.attach(h); + return bResult; +} + +/// +/// Imports the public key. +/// +/// \sa [CryptImportPublicKeyInfo function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380209.aspx) +/// +static bool CryptImportPublicKeyInfo(_In_ HCRYPTPROV hCryptProv, _In_ DWORD dwCertEncodingType, _In_ PCERT_PUBLIC_KEY_INFO pInfo, _Inout_ winstd::crypt_key &key) +{ + HCRYPTKEY h; + BOOL bResult = CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, &h); + if (bResult) + key.attach(h); + return bResult; +} + +/// +/// Generates cryptographic session keys derived from a base data value. +/// +/// \sa [CryptDeriveKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379916.aspx) +/// +static bool CryptDeriveKey(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTHASH hBaseData, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key) +{ + HCRYPTKEY h; + BOOL bResult = CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, &h); + if (bResult) + key.attach(h); + return bResult; +} + +#pragma warning(pop) + +/// @} diff --git a/include/WinStd/ETW.h b/include/WinStd/ETW.h index e3c89464..3fe99424 100644 --- a/include/WinStd/ETW.h +++ b/include/WinStd/ETW.h @@ -926,6 +926,7 @@ namespace winstd /// /// \sa [OpenTrace function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364089.aspx) /// + __declspec(deprecated("Use OpenTrace")) bool create(_Inout_ PEVENT_TRACE_LOGFILE Logfile) { handle_type h = OpenTrace(Logfile); diff --git a/include/WinStd/SetupAPI.h b/include/WinStd/SetupAPI.h index a7b941f9..0182d55e 100644 --- a/include/WinStd/SetupAPI.h +++ b/include/WinStd/SetupAPI.h @@ -45,6 +45,7 @@ namespace winstd /// /// \sa [SetupDiCreateDeviceInfoList function](https://docs.microsoft.com/en-us/windows/desktop/api/setupapi/nf-setupapi-setupdicreatedeviceinfolist) /// + __declspec(deprecated("Use SetupDiCreateDeviceInfoList")) bool create( _In_opt_ const GUID * ClassGuid, _In_opt_ HWND hwndParent) noexcept @@ -67,6 +68,7 @@ namespace winstd /// /// \sa [SetupDiGetClassDevsExW function](https://docs.microsoft.com/en-us/windows/desktop/api/setupapi/nf-setupapi-setupdigetclassdevsexw) /// + __declspec(deprecated("Use SetupDiGetClassDevsEx")) bool create( _In_opt_ const GUID * ClassGuid, _In_opt_ PCTSTR Enumerator, diff --git a/include/WinStd/WLAN.h b/include/WinStd/WLAN.h index bb167f0e..246c959a 100644 --- a/include/WinStd/WLAN.h +++ b/include/WinStd/WLAN.h @@ -149,6 +149,7 @@ namespace winstd /// - \c true when succeeds; /// - \c false when fails. Use `GetLastError()` for failure reason. /// + __declspec(deprecated("Use WlanOpenHandle - mind it returns error number rather than SetLastError")) bool open(_In_ DWORD dwClientVersion, _Out_ PDWORD pdwNegotiatedVersion) noexcept { handle_type h; @@ -176,3 +177,27 @@ namespace winstd /// @} } + +/// \addtogroup WinStdWLANAPI +/// @{ + +/// +/// Opens a connection to the server. +/// +/// \sa [WlanOpenHandle function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms706759.aspx) +/// +#pragma warning(suppress: 4505) // Don't warn on unused code +static DWORD WlanOpenHandle( + _In_ DWORD dwClientVersion, + _Reserved_ PVOID pReserved, + _Out_ PDWORD pdwNegotiatedVersion, + _Inout_ winstd::wlan_handle &handle) +{ + HANDLE h; + DWORD dwResult = WlanOpenHandle(dwClientVersion, pReserved, pdwNegotiatedVersion, &h); + if (dwResult == ERROR_SUCCESS) + handle.attach(h); + return dwResult; +} + +/// @} diff --git a/include/WinStd/Win.h b/include/WinStd/Win.h index 51623bb8..3f01168d 100644 --- a/include/WinStd/Win.h +++ b/include/WinStd/Win.h @@ -19,6 +19,18 @@ /// /// @{ +/// +/// Implements default constructors and operators to prevent their auto-generation by compiler. +/// +#define WINSTD_WINHANDLE_IMPL(C, INVAL) \ +public: \ + C ( ) noexcept { } \ + C (_In_opt_ handle_type h) noexcept : win_handle( h ) { } \ + C (_Inout_ C &&h) noexcept : win_handle(std::move(h)) { } \ + C& operator=(_In_opt_ handle_type h) noexcept { win_handle::operator=( h ); return *this; } \ + C& operator=(_Inout_ C &&h) noexcept { win_handle::operator=(std::move(h)); return *this; } \ +WINSTD_NONCOPYABLE(C) + /// @copydoc GetModuleFileNameW() template static DWORD GetModuleFileNameA(_In_opt_ HMODULE hModule, _Out_ std::basic_string &sValue) noexcept @@ -1399,6 +1411,7 @@ namespace winstd /// - \c true when succeeds; /// - \c false when fails. Use `GetLastError()` for failure reason. /// + __declspec(deprecated("Use LoadLibraryEx")) bool load(_In_z_ LPCTSTR lpFileName, __reserved handle_type hFile, _In_ DWORD dwFlags) noexcept { handle_type h = LoadLibraryEx(lpFileName, hFile, dwFlags); @@ -1426,6 +1439,8 @@ namespace winstd /// class process : public win_handle { + WINSTD_WINHANDLE_IMPL(process, NULL) + public: /// /// Opens process handle. @@ -1436,6 +1451,7 @@ namespace winstd /// - \c true when succeeds; /// - \c false when fails. Use `GetLastError()` for failure reason. /// + __declspec(deprecated("Use OpenProcess")) bool open(_In_ DWORD dwDesiredAccess, _In_ BOOL bInheritHandle, _In_ DWORD dwProcessId) noexcept { handle_type h = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); @@ -1452,6 +1468,8 @@ namespace winstd /// class file : public win_handle { + WINSTD_WINHANDLE_IMPL(file, INVALID_HANDLE_VALUE) + public: /// /// Opens file handle. @@ -1462,6 +1480,7 @@ namespace winstd /// - \c true when succeeds; /// - \c false when fails. Use `GetLastError()` for failure reason. /// + __declspec(deprecated("Use CreateFile")) bool create(_In_z_ LPCTSTR lpFileName, _In_ DWORD dwDesiredAccess, _In_ DWORD dwShareMode, _In_ DWORD dwCreationDisposition, _In_opt_ DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, _In_opt_ HANDLE hTemplateFile = NULL) noexcept { handle_type h = CreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); @@ -1478,6 +1497,8 @@ namespace winstd /// class file_mapping : public win_handle { + WINSTD_WINHANDLE_IMPL(file_mapping, NULL) + public: /// /// Creates or opens a named or unnamed file mapping object for a specified file. @@ -1488,9 +1509,10 @@ namespace winstd /// - \c true when succeeds; /// - \c false when fails. Use `GetLastError()` for failure reason. /// + __declspec(deprecated("Use CreateFileMapping")) bool create(_In_ HANDLE hFile, _In_ DWORD flProtect, _In_ DWORD dwMaximumSizeHigh, _In_ DWORD dwMaximumSizeLow, _In_opt_ LPSECURITY_ATTRIBUTES lpFileMappingAttributes = NULL, _In_opt_ LPCTSTR lpName = NULL) noexcept { - handle_type h = CreateFileMappingW(hFile, lpFileMappingAttributes, flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, lpName); + handle_type h = CreateFileMapping(hFile, lpFileMappingAttributes, flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, lpName); if (h != invalid) { attach(h); return true; @@ -1563,6 +1585,8 @@ namespace winstd /// class event : public win_handle { + WINSTD_WINHANDLE_IMPL(event, NULL) + public: /// /// Creates or opens a named or unnamed event object. @@ -1573,6 +1597,7 @@ namespace winstd /// - \c true when succeeds; /// - \c false when fails. Use `GetLastError()` for failure reason. /// + __declspec(deprecated("Use CreateEvent")) bool create(_In_ BOOL bManualReset, _In_ BOOL bInitialState, _In_opt_ LPSECURITY_ATTRIBUTES lpEventAttributes = NULL, _In_opt_z_ LPCTSTR lpName = NULL) noexcept { handle_type h = CreateEvent(lpEventAttributes, bManualReset, bInitialState, lpName); @@ -1592,6 +1617,7 @@ namespace winstd /// - \c true when succeeds; /// - \c false when fails. Use `GetLastError()` for failure reason. /// + __declspec(deprecated("Use OpenEvent")) bool open(_In_ DWORD dwDesiredAccess, _In_ BOOL bInheritHandle, _In_z_ LPCTSTR lpName) noexcept { handle_type h = OpenEvent(dwDesiredAccess, bInheritHandle, lpName); @@ -1678,6 +1704,7 @@ namespace winstd /// - \c true when succeeds; /// - \c false when fails. Use `GetLastError()` for failure reason. /// + __declspec(deprecated("Use FindFirstFile")) bool find(_In_ LPCTSTR lpFileName, _Out_ LPWIN32_FIND_DATA lpFindFileData) noexcept { handle_type h = FindFirstFile(lpFileName, lpFindFileData); @@ -1728,6 +1755,7 @@ namespace winstd /// - \c true when succeeds; /// - \c false when fails. Use `GetLastError()` for failure reason. /// + __declspec(deprecated("Use HeapCreate")) bool create(_In_ DWORD flOptions, _In_ SIZE_T dwInitialSize, _In_ SIZE_T dwMaximumSize) noexcept { handle_type h = HeapCreate(flOptions, dwInitialSize, dwMaximumSize); @@ -2168,6 +2196,7 @@ namespace winstd /// /// \sa [RegCreateKeyEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724844.aspx) /// + __declspec(deprecated("Use RegCreateKeyEx - mind it returns error number rather than SetLastError")) bool create( _In_ HKEY hKey, _In_z_ LPCTSTR lpSubKey, @@ -2197,6 +2226,7 @@ namespace winstd /// /// \sa [RegOpenKeyEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724897.aspx) /// + __declspec(deprecated("Use RegOpenKeyEx - mind it returns error number rather than SetLastError")) bool open( _In_ HKEY hKey, _In_opt_z_ LPCTSTR lpSubKey, @@ -2204,7 +2234,7 @@ namespace winstd _In_ REGSAM samDesired) noexcept { handle_type h; - const LONG s = RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, &h); + const LSTATUS s = RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, &h); if (s == ERROR_SUCCESS) { attach(h); return true; @@ -2233,8 +2263,14 @@ namespace winstd { reg_key k; - if (!k.open(m_h, szSubkey, 0, KEY_ENUMERATE_SUB_KEYS)) + handle_type h; + s = RegOpenKeyEx(m_h, szSubkey, 0, KEY_ENUMERATE_SUB_KEYS, &h); + if (s == ERROR_SUCCESS) + k.attach(h); + else { + SetLastError(s); return false; + } for (;;) { TCHAR szName[MAX_PATH]; DWORD dwSize = _countof(szName); @@ -2368,6 +2404,7 @@ namespace winstd /// - \c true when succeeds; /// - \c false when fails. Use `GetLastError()` for failure reason. /// + __declspec(deprecated("Use RegisterEventSource")) bool open(_In_z_ LPCTSTR lpUNCServerName, _In_z_ LPCTSTR lpSourceName) noexcept { handle_type h = RegisterEventSource(lpUNCServerName, lpSourceName); @@ -2392,3 +2429,93 @@ namespace winstd /// @} } + +/// \addtogroup WinStdWinAPI +/// @{ + +#pragma warning(push) +#pragma warning(disable: 4505) // Don't warn on unused code + +/// @copydoc RegCreateKeyExW() +static LSTATUS RegCreateKeyExA( + _In_ HKEY hKey, + _In_ LPCSTR lpSubKey, + _Reserved_ DWORD Reserved, + _In_opt_ LPSTR lpClass, + _In_ DWORD dwOptions, + _In_ REGSAM samDesired, + _In_opt_ CONST LPSECURITY_ATTRIBUTES lpSecurityAttributes, + _Inout_ winstd::reg_key &result, + _Out_opt_ LPDWORD lpdwDisposition) +{ + HKEY h; + LSTATUS s = RegCreateKeyExA(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, &h, lpdwDisposition); + if (s == ERROR_SUCCESS) + result.attach(h); + return s; +} + +/// +/// Creates the specified registry key. If the key already exists, the function opens it. +/// +/// \sa [RegCreateKeyEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724844.aspx) +/// +static LSTATUS RegCreateKeyExW( + _In_ HKEY hKey, + _In_ LPCWSTR lpSubKey, + _Reserved_ DWORD Reserved, + _In_opt_ LPWSTR lpClass, + _In_ DWORD dwOptions, + _In_ REGSAM samDesired, + _In_opt_ CONST LPSECURITY_ATTRIBUTES lpSecurityAttributes, + _Inout_ winstd::reg_key &result, + _Out_opt_ LPDWORD lpdwDisposition) +{ + HKEY h; + LSTATUS s = RegCreateKeyExW(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, &h, lpdwDisposition); + if (s == ERROR_SUCCESS) + result.attach(h); + return s; +} + +/// @copydoc RegOpenKeyExW() +static LSTATUS RegOpenKeyExA( + _In_ HKEY hKey, + _In_opt_ LPCSTR lpSubKey, + _In_opt_ DWORD ulOptions, + _In_ REGSAM samDesired, + _Inout_ winstd::reg_key &result) +{ + HKEY h; + LSTATUS s = RegOpenKeyExA(hKey, lpSubKey, ulOptions, samDesired, &h); + if (s == ERROR_SUCCESS) + result.attach(h); + return s; +} + +/// +/// Opens the specified registry key. +/// +/// \return +/// - true when creation succeeds; +/// - false when creation fails. For extended error information, call `GetLastError()`. +/// +/// \sa [RegOpenKeyEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724897.aspx) +/// +static LSTATUS RegOpenKeyExW( + _In_ HKEY hKey, + _In_opt_ LPCWSTR lpSubKey, + _In_opt_ DWORD ulOptions, + _In_ REGSAM samDesired, + _Inout_ winstd::reg_key &result) +{ + HKEY h; + LSTATUS s = RegOpenKeyExW(hKey, lpSubKey, ulOptions, samDesired, &h); + if (s == ERROR_SUCCESS) + result.attach(h); + return s; +} + +#pragma warning(pop) + +/// @} diff --git a/include/WinStd/WinSock2.h b/include/WinStd/WinSock2.h index f607e5b0..eef088d8 100644 --- a/include/WinStd/WinSock2.h +++ b/include/WinStd/WinSock2.h @@ -90,7 +90,7 @@ namespace winstd /// /// SID wrapper class /// - class addrinfo : public handle + class addrinfo : public handle { WINSTD_HANDLE_IMPL(addrinfo, NULL) @@ -100,13 +100,14 @@ namespace winstd /// /// \sa [GetAddrInfoW function](https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-getaddrinfow) /// + __declspec(deprecated("Use GetAddrInfoA")) bool get( - _In_opt_ PCTSTR pNodeName, - _In_opt_ PCTSTR pServiceName, - _In_opt_ const ADDRINFOT *pHints) + _In_opt_ PCSTR pNodeName, + _In_opt_ PCSTR pServiceName, + _In_opt_ const ADDRINFOA *pHints) { handle_type h; - if (GetAddrInfo(pNodeName, pServiceName, pHints, &h) == 0) { + if (GetAddrInfoA(pNodeName, pServiceName, pHints, &h) == 0) { attach(h); return true; } else @@ -132,11 +133,112 @@ namespace winstd /// void free_internal() noexcept override { - FreeAddrInfo(m_h); + FreeAddrInfoA(m_h); } }; + /// + /// SID wrapper class + /// + class waddrinfo : public handle + { + WINSTD_HANDLE_IMPL(waddrinfo, NULL) + + public: + /// + /// Provides protocol-independent translation from a host name to an address. + /// + /// \sa [GetAddrInfoW function](https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-getaddrinfow) + /// + __declspec(deprecated("Use GetAddrInfoW")) + bool get( + _In_opt_ PCWSTR pNodeName, + _In_opt_ PCWSTR pServiceName, + _In_opt_ const ADDRINFOW *pHints) + { + handle_type h; + if (GetAddrInfoW(pNodeName, pServiceName, pHints, &h) == 0) { + attach(h); + return true; + } else + return false; + } + + /// + /// Frees address information + /// + /// \sa [FreeAddrInfoW function](https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-freeaddrinfow) + /// + virtual ~waddrinfo() + { + if (m_h != invalid) + free_internal(); + } + + protected: + /// + /// Frees address information + /// + /// \sa [FreeAddrInfoW function](https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-freeaddrinfow) + /// + void free_internal() noexcept override + { + FreeAddrInfoW(m_h); + } + }; + + /// + /// Multi-byte / Wide-character SID wrapper class (according to _UNICODE) + /// +#ifdef _UNICODE + typedef waddrinfo taddrinfo; +#else + typedef addrinfo taddrinfo; +#endif + #endif /// @} } + +/// \addtogroup WinSock2API +/// @{ + +#pragma warning(push) +#pragma warning(disable: 4505) // Don't warn on unused code + +/// @copydoc GetAddrInfoW() +static INT GetAddrInfoA( + _In_opt_ PCSTR pNodeName, + _In_opt_ PCSTR pServiceName, + _In_opt_ const ADDRINFOA *pHints, + _Inout_ winstd::addrinfo &result) +{ + PADDRINFOA h; + INT iResult = GetAddrInfoA(pNodeName, pServiceName, pHints, &h); + if (iResult == 0) + result.attach(h); + return iResult; +} + +/// +/// Provides protocol-independent translation from a host name to an address. +/// +/// \sa [GetAddrInfoW function](https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-getaddrinfow) +/// +static INT GetAddrInfoW( + _In_opt_ PCWSTR pNodeName, + _In_opt_ PCWSTR pServiceName, + _In_opt_ const ADDRINFOW *pHints, + _Inout_ winstd::waddrinfo &result) +{ + PADDRINFOW h; + INT iResult = GetAddrInfoW(pNodeName, pServiceName, pHints, &h); + if (iResult == 0) + result.attach(h); + return iResult; +} + +#pragma warning(pop) + +/// @}