void SystemException::createMessage(const TCHAR *userMessage, int errcode) { _ASSERT(!(userMessage == 0 && errcode == ERROR_SUCCESS)); bool formatMessageOk = true; TCHAR buffer[1024 * 10] = {0}; if (FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), (LPTSTR)&buffer[0], sizeof(buffer), NULL) == 0) { formatMessageOk = false; } StringStorage windowsErrorDescription(buffer); const TCHAR badCharacters[] = { 10, 13, _T('\n'), _T('\t') }; windowsErrorDescription.removeChars(badCharacters, sizeof(badCharacters) / sizeof(TCHAR)); if (windowsErrorDescription.endsWith(_T('.'))) { windowsErrorDescription.truncate(1); } if (formatMessageOk) { m_systemMessage.format(_T("%s (error code %d)"), windowsErrorDescription.getString(), errcode); } else { m_systemMessage.format(_T("Error code %d"), errcode); } if (errcode != 0) { if (userMessage == 0) { m_message = m_systemMessage; } else { m_message.format(_T("%s (system error: %s)"), userMessage, m_systemMessage.getString()); } } else { m_message = userMessage; m_systemMessage = userMessage; } }
void SystemException::createMessage(const TCHAR *userMessage, int errcode) { if (userMessage == 0 && errcode == ERROR_SUCCESS) { userMessage = _T("Thrown a system exception but the program") _T(" cannot identify the corresponding system error."); } // Get description of windows specific error. bool formatMessageOk = true; TCHAR buffer[1024 * 10] = {0}; if (FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), (LPTSTR)&buffer[0], sizeof(buffer), NULL) == 0) { formatMessageOk = false; } StringStorage windowsErrorDescription(buffer); // Remove bad characters. const TCHAR badCharacters[] = { 10, 13, _T('\n'), _T('\t') }; windowsErrorDescription.removeChars(badCharacters, sizeof(badCharacters) / sizeof(TCHAR)); if (windowsErrorDescription.endsWith(_T('.'))) { windowsErrorDescription.truncate(1); } // Create system error part of message. if (formatMessageOk) { m_systemMessage.format(_T("%s (error code %d)"), windowsErrorDescription.getString(), errcode); } else { m_systemMessage.format(_T("Error code %d"), errcode); } // Use user message if specified. if (errcode != 0) { if (userMessage == 0) { m_message = m_systemMessage; } else { m_message.format(_T("%s (system error: %s)"), userMessage, m_systemMessage.getString()); } } else { m_message = userMessage; m_systemMessage = userMessage; } }