< prev index next >

src/jdk.jpackage/windows/native/libjpackage/WinErrorHandling.cpp

Print this page




  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "WinErrorHandling.h"
  27 #include "Log.h"
  28 #include "SysInfo.h"
  29 #include "FileUtils.h"
  30 
  31 
  32 namespace {
  33 
  34 std::string makeMessage(const std::string& msg, const char* label,
  35                                             const void* c, DWORD errorCode) {
  36     std::ostringstream err;
  37     err << (label ? label : "Some error") << " [" << errorCode << "]";
  38 
  39     HMODULE hmodule = NULL;
  40     if (c) {
  41         GetModuleHandleEx(
  42             GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
  43             reinterpret_cast<LPCTSTR>(c),
  44             &hmodule);
  45 
  46         if (!hmodule) {
  47             LOG_WARNING(tstrings::any() << "GetModuleHandleEx() failed for " << c << " address.");

  48         }
  49     }
  50     if (hmodule || !c) {
  51         err << "(" << SysError::getSysErrorMessage(errorCode, hmodule) << ")";
  52     }
  53 
  54     return joinErrorMessages(msg, err.str());
  55 }
  56 
  57 
  58 std::wstring getSystemMessageDescription(DWORD messageId, HMODULE moduleHandle) {
  59     LPWSTR pMsg = NULL;
  60     std::wstring descr;
  61 
  62     // we always retrieve UNICODE description from system,
  63     // convert it to utf8 if UNICODE is not defined
  64 
  65     while (true) {
  66         DWORD res = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER
  67                                         | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS


  88         } else {
  89             // if we fail to get description for specific moduleHandle,
  90             // try to get "common" description.
  91             if (moduleHandle != NULL) {
  92                 moduleHandle = NULL;
  93                 continue;
  94             }
  95             descr = L"No description available";
  96         }
  97         break;
  98     }
  99 
 100     return descr;
 101 }
 102 
 103 } // namespace
 104 
 105 
 106 SysError::SysError(const tstrings::any& msg, const void* caller, DWORD ec,
 107                                                             const char* label):
 108         std::runtime_error(makeMessage(msg.str(), label, caller, ec)) {

 109 }
 110 
 111 std::wstring SysError::getSysErrorMessage(DWORD errCode, HMODULE moduleHandle) {
 112     tstrings::any msg;
 113     msg << "system error " << errCode
 114         << " (" << getSystemMessageDescription(errCode, moduleHandle) << ")";
 115     return msg.tstr();
 116 }
 117 
 118 std::wstring SysError::getComErrorMessage(HRESULT hr) {
 119     HRESULT hrOrig = hr;
 120     // for FACILITY_WIN32 facility we need to reset hiword
 121     if(HRESULT_FACILITY(hr) == FACILITY_WIN32) {
 122         hr = HRESULT_CODE(hr);
 123     }
 124     return tstrings::format(_T("COM error 0x%08X (%s)"), hrOrig, getSystemMessageDescription(hr, NULL));

 125 }


  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "WinErrorHandling.h"
  27 #include "Log.h"
  28 #include "SysInfo.h"
  29 #include "FileUtils.h"
  30 
  31 
  32 namespace {
  33 
  34 std::string makeMessage(const std::string& msg, const char* label,
  35                                             const void* c, DWORD errorCode) {
  36     std::ostringstream err;
  37     err << (label ? label : "Some error") << " [" << errorCode << "]";
  38 
  39     HMODULE hmodule = NULL;
  40     if (c) {
  41         GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
  42                 | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
  43                 reinterpret_cast<LPCTSTR>(c), &hmodule);

  44 
  45         if (!hmodule) {
  46             LOG_WARNING(tstrings::any() << "GetModuleHandleEx() failed for "
  47                     << c << " address.");
  48         }
  49     }
  50     if (hmodule || !c) {
  51         err << "(" << SysError::getSysErrorMessage(errorCode, hmodule) << ")";
  52     }
  53 
  54     return joinErrorMessages(msg, err.str());
  55 }
  56 
  57 
  58 std::wstring getSystemMessageDescription(DWORD messageId, HMODULE moduleHandle) {
  59     LPWSTR pMsg = NULL;
  60     std::wstring descr;
  61 
  62     // we always retrieve UNICODE description from system,
  63     // convert it to utf8 if UNICODE is not defined
  64 
  65     while (true) {
  66         DWORD res = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER
  67                 | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS


  88         } else {
  89             // if we fail to get description for specific moduleHandle,
  90             // try to get "common" description.
  91             if (moduleHandle != NULL) {
  92                 moduleHandle = NULL;
  93                 continue;
  94             }
  95             descr = L"No description available";
  96         }
  97         break;
  98     }
  99 
 100     return descr;
 101 }
 102 
 103 } // namespace
 104 
 105 
 106 SysError::SysError(const tstrings::any& msg, const void* caller, DWORD ec,
 107         const char* label):
 108 
 109 std::runtime_error(makeMessage(msg.str(), label, caller, ec)) {
 110 }
 111 
 112 std::wstring SysError::getSysErrorMessage(DWORD errCode, HMODULE moduleHandle) {
 113     tstrings::any msg;
 114     msg << "system error " << errCode
 115         << " (" << getSystemMessageDescription(errCode, moduleHandle) << ")";
 116     return msg.tstr();
 117 }
 118 
 119 std::wstring SysError::getComErrorMessage(HRESULT hr) {
 120     HRESULT hrOrig = hr;
 121     // for FACILITY_WIN32 facility we need to reset hiword
 122     if(HRESULT_FACILITY(hr) == FACILITY_WIN32) {
 123         hr = HRESULT_CODE(hr);
 124     }
 125     return tstrings::format(_T("COM error 0x%08X (%s)"), hrOrig,
 126             getSystemMessageDescription(hr, NULL));
 127 }
< prev index next >