#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2023 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_SUPPORT_PLATFORM_WINDOWSAPI_H #define NUCLEX_SUPPORT_PLATFORM_WINDOWSAPI_H #include "Nuclex/Support/Config.h" #if defined(NUCLEX_SUPPORT_WINDOWS) // The Windows headers tend to include a ton of crap and pollute the global namespace // like nothing else. These macros cut down on that a bit. #define WIN32_LEAN_AND_MEAN #define VC_EXTRALEAN #define NOMINMAX #include // These symbols are redefined globally when you include the Windows header. Needless // to say that we don't want that, because it would result in those names chaotically // changing between implementation and public header - and even in the application using // this library depending on whether the user included Windows.h somewhere. #undef CreateFile #undef DeleteFile #undef MoveFile #undef CreateDirectory #undef RemoveDirectory #undef GetFileAttributes #undef GetFileAttributesEx #undef FindFirstFile #undef FindNextFile #undef GetTempPath #undef GetModuleFileName #undef GetFullPathName #undef GetWindowsDirectory #undef GetSystemDirectory #include // for std::string namespace Nuclex { namespace Support { namespace Platform { // ------------------------------------------------------------------------------------------- // /// Offers generic methods for dealing with the Windows API class WindowsApi { /// Returns the error message for the specified error number /// /// Error number for which the error message will be looked up /// /// The error message for the specified error number /// /// Some posix methods can also be found in the Windows API, usually with /// non-standard underscore prefixes. For these methods, Microsoft's reimplementation /// of strerror(), named _wcserror_s(), needs to be used with the error number /// found in the 'errno' variable (like on Posix systems). This method handles /// calling _wcserror_s() to obtain a meaningful error message for 'errno'. /// public: static std::string GetErrorMessage(int errorNumber); /// Returns the error message for the specified error code /// /// Error code for which the error message will be looked up /// /// The error message for the specified error code /// /// Standard Windows API methods that only exist on Microsoft systems usually /// signal error/success with their return code. The actual error type can be /// looked up by calling GetLastError(). This method fetches a meaningful error /// message for the error code returned by GetLastError(). /// public: static std::string GetErrorMessage(DWORD errorCode); /// Returns the error message for the specified HRESULT /// /// HRESULT for which the error message will be looked up /// /// The error message for the specified HRESULT /// /// COM (a cross-language ABI that defines vtable layout, calling convention, /// error handling etc.) uses HRESULTs for all method returns. A HRESULT is /// a combination of flags, the most significant bit indicates error/success /// (so all negative HRESULTS are error codes). This method fetches a meaningful /// error message for the HRESULT returned by a COM method. /// public: static std::string GetErrorMessage(HRESULT resultHandle); /// Throws the appropriate exception for an error reported by the OS /// /// Error message that should be included in the exception, will be prefixed to /// the OS error message /// /// /// Value that GetLastError() returned at the time of failure /// public: [[noreturn]] static void ThrowExceptionForSystemError( const std::string &errorMessage, DWORD errorCode ); /// Throws the appropriate exception for an error reported by the OS /// /// Error message that should be included in the exception, will be prefixed to /// the OS error message /// /// HRESULT that was returned by the failed function public: [[noreturn]] static void ThrowExceptionForHResult( const std::string &errorMessage, HRESULT resultHandle ); }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Support::Platform #endif // defined(NUCLEX_SUPPORT_WINDOWS) #endif // NUCLEX_SUPPORT_PLATFORM_WINDOWSAPI_H