| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors |
| 3 |
* SPDX-License-Identifier: GPL-2.0-or-later OR CPOL-1.02 |
| 4 |
* |
| 5 |
* Parts of this file are based on: |
| 6 |
* https://www.codeproject.com/articles/570751/devmsi-an-example-cplusplus-msi-wix-deferred-custo |
| 7 |
* |
| 8 |
* Licensed under the The Code Project Open License (CPOL): |
| 9 |
* https://www.codeproject.com/info/cpol10.aspx |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "NCMsiHelper.h" |
| 13 |
|
| 14 |
// |
| 15 |
// This code modified from MSDN article 256348 |
| 16 |
// "How to obtain error message descriptions using the FormatMessage API" |
| 17 |
// Currently found at http://support.microsoft.com/kb/256348/en-us |
| 18 |
|
| 19 |
#define ERRMSGBUFFERSIZE 256 |
| 20 |
|
| 21 |
/** |
| 22 |
* Use FormatMessage() to look an error code and log the error text. |
| 23 |
* |
| 24 |
* @param dwErrorMsgId The error code to be investigated. |
| 25 |
*/ |
| 26 |
void LogError(DWORD dwErrorMsgId) |
| 27 |
{ |
| 28 |
HLOCAL pBuffer = nullptr; // Buffer to hold the textual error description. |
| 29 |
DWORD ret = 0; // Temp space to hold a return value. |
| 30 |
HINSTANCE hInst = nullptr; // Instance handle for DLL. |
| 31 |
bool doLookup = true; |
| 32 |
DWORD dwMessageId = dwErrorMsgId; |
| 33 |
LPCSTR pMessage = "Error %d"; |
| 34 |
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS; |
| 35 |
|
| 36 |
if (HRESULT_FACILITY(dwErrorMsgId) == FACILITY_MSMQ) { |
| 37 |
hInst = LoadLibrary(TEXT("MQUTIL.DLL")); |
| 38 |
flags |= FORMAT_MESSAGE_FROM_HMODULE; |
| 39 |
doLookup = (nullptr != hInst); |
| 40 |
} else if (dwErrorMsgId >= NERR_BASE && dwErrorMsgId <= MAX_NERR) { |
| 41 |
hInst = LoadLibrary(TEXT("NETMSG.DLL")); |
| 42 |
flags |= FORMAT_MESSAGE_FROM_HMODULE; |
| 43 |
doLookup = (nullptr != hInst); |
| 44 |
} else if (HRESULT_FACILITY(dwErrorMsgId) == FACILITY_WIN32) { |
| 45 |
// A "GetLastError" error, drop the HRESULT_FACILITY |
| 46 |
dwMessageId &= 0x0000FFFF; |
| 47 |
flags |= FORMAT_MESSAGE_FROM_SYSTEM; |
| 48 |
} |
| 49 |
|
| 50 |
if (doLookup) { |
| 51 |
ret = FormatMessageA( |
| 52 |
flags, |
| 53 |
hInst, // Handle to the DLL. |
| 54 |
dwMessageId, // Message identifier. |
| 55 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language. |
| 56 |
reinterpret_cast<LPSTR>(&pBuffer), // Buffer that will hold the text string. |
| 57 |
ERRMSGBUFFERSIZE, // Allocate at least this many chars for pBuffer. |
| 58 |
nullptr // No insert values. |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
if (0 < ret && nullptr != pBuffer) { |
| 63 |
pMessage = static_cast<LPSTR>(pBuffer); |
| 64 |
} |
| 65 |
|
| 66 |
// Display the string. |
| 67 |
if (WcaIsInitialized()) { |
| 68 |
WcaLogError(dwErrorMsgId, pMessage, dwMessageId); |
| 69 |
} else { |
| 70 |
// Log to stdout/stderr |
| 71 |
fprintf_s(stderr, pMessage, dwMessageId); |
| 72 |
if ('\n' != pMessage[strlen(pMessage) - 1]) { |
| 73 |
fprintf_s(stderr, "\n"); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Free the buffer. |
| 78 |
LocalFree(pBuffer); |
| 79 |
} |
| 80 |
|
| 81 |
void LogResult( |
| 82 |
__in HRESULT hr, |
| 83 |
__in_z __format_string PCSTR fmt, ... |
| 84 |
) |
| 85 |
{ |
| 86 |
// This code taken from MSDN vsprintf example found currently at |
| 87 |
// http://msdn.microsoft.com/en-us/library/28d5ce15(v=vs.71).aspx |
| 88 |
// ...and then modified... because it doesn't seem to work! |
| 89 |
va_list args; |
| 90 |
|
| 91 |
va_start(args, fmt); |
| 92 |
#pragma warning(push) |
| 93 |
#pragma warning(disable : 4996) |
| 94 |
const auto len = _vsnprintf(nullptr, 0, fmt, args) + 1; |
| 95 |
#pragma warning(pop) |
| 96 |
auto buffer = static_cast<char*>(malloc(len * sizeof(char))); |
| 97 |
|
| 98 |
#ifdef _DEBUG |
| 99 |
::ZeroMemory(buffer, len); |
| 100 |
#endif // _DEBUG |
| 101 |
_vsnprintf_s(buffer, len, len - 1, fmt, args); |
| 102 |
|
| 103 |
// (MSDN code complete) |
| 104 |
|
| 105 |
// Now that the buffer holds the formatted string, send it to |
| 106 |
// the appropriate output. |
| 107 |
if (WcaIsInitialized()) { |
| 108 |
if (FAILED(hr)) { |
| 109 |
WcaLogError(hr, buffer); |
| 110 |
LogError(hr); |
| 111 |
} else { |
| 112 |
WcaLog(LOGMSG_STANDARD, buffer); |
| 113 |
} |
| 114 |
} else { // Log to stdout/stderr |
| 115 |
if (FAILED(hr)) { |
| 116 |
fprintf_s(stderr, "%s\n", buffer); |
| 117 |
LogError(hr); |
| 118 |
} else { |
| 119 |
fprintf_s(stdout, "%s\n", buffer); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
free(buffer); |
| 124 |
} |