From a69103237f98513597e782978d83f4011dd83e72 Mon Sep 17 00:00:00 2001 From: kervala Date: Fri, 12 Feb 2016 16:04:18 +0100 Subject: [PATCH] Changed: Reuse NLMISC::formatErrorMessage instead of native FormatMessage --- code/nel/src/misc/common.cpp | 22 +++++++++------------- code/nel/src/misc/mutex.cpp | 4 ---- code/nel/src/misc/path.cpp | 16 +++------------- code/nel/src/misc/system_utils.cpp | 16 +--------------- 4 files changed, 13 insertions(+), 45 deletions(-) diff --git a/code/nel/src/misc/common.cpp b/code/nel/src/misc/common.cpp index 6ec0261f6..41a11976f 100644 --- a/code/nel/src/misc/common.cpp +++ b/code/nel/src/misc/common.cpp @@ -685,10 +685,7 @@ bool killProgram(uint32 pid) /*#elif defined(NL_OS_WINDOWS) // it doesn't work because pid != handle and i don't know how to kill a pid or know the real handle of another service (not -1) int res = TerminateProcess((HANDLE)pid, 888); - LPVOID lpMsgBuf; - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL); nlwarning("Failed to kill '%d' err %d: '%s'", pid, GetLastError (), lpMsgBuf); - LocalFree(lpMsgBuf); return res != 0; */ #else @@ -758,11 +755,12 @@ bool launchProgram(const std::string &programName, const std::string &arguments, } else { - LPVOID lpMsgBuf; - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL); if (log) - nlwarning("LAUNCH: Failed launched '%s' with arg '%s' err %d: '%s'", programName.c_str(), arguments.c_str(), GetLastError(), lpMsgBuf); - LocalFree(lpMsgBuf); + { + sint lastError = getLastError(); + nlwarning("LAUNCH: Failed launched '%s' with arg '%s' err %d: '%s'", programName.c_str(), arguments.c_str(), lastError, formatErrorMessage(lastError).c_str()); + } + CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); } @@ -920,13 +918,11 @@ sint launchProgramAndWaitForResult(const std::string &programName, const std::st } else { - LPVOID lpMsgBuf; - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL); - if (log) - nlwarning("LAUNCH: Failed launched '%s' with arg '%s' err %d: '%s'", programName.c_str(), arguments.c_str(), GetLastError(), lpMsgBuf); - - LocalFree(lpMsgBuf); + { + sint lastError = getLastError(); + nlwarning("LAUNCH: Failed launched '%s' with arg '%s' err %d: '%s'", programName.c_str(), arguments.c_str(), lastError, formatErrorMessage(lastError).c_str()); + } CloseHandle(pi.hProcess); CloseHandle(pi.hThread); diff --git a/code/nel/src/misc/mutex.cpp b/code/nel/src/misc/mutex.cpp index af0e850ab..098bce795 100644 --- a/code/nel/src/misc/mutex.cpp +++ b/code/nel/src/misc/mutex.cpp @@ -81,11 +81,7 @@ inline void LeaveMutex( void *handle ) { if (ReleaseMutex(handle) == 0) { - //LPVOID lpMsgBuf; - //FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - // NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL );*/ nlerror ("error while releasing the mutex (0x%x %d), %p", GetLastError(), GetLastError(), handle); - //LocalFree( lpMsgBuf ); } } diff --git a/code/nel/src/misc/path.cpp b/code/nel/src/misc/path.cpp index 077bc18be..41477db32 100644 --- a/code/nel/src/misc/path.cpp +++ b/code/nel/src/misc/path.cpp @@ -2358,23 +2358,13 @@ static bool CopyMoveFile(const std::string &dest, const std::string &src, bool c #ifdef NL_OS_WINDOWS if (MoveFile(ssrc.c_str(), sdest.c_str()) == 0) { - LPVOID lpMsgBuf; - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR) &lpMsgBuf, - 0, - NULL ); + sint lastError = NLMISC::getLastError(); nlwarning ("PATH: CopyMoveFile error: can't link/move '%s' into '%s', error %u (%s)", ssrc.c_str(), sdest.c_str(), - GetLastError(), - lpMsgBuf); + lastError, + NLMISC::formatErrorMessage(lastError).c_str()); - LocalFree(lpMsgBuf); return false; } #else diff --git a/code/nel/src/misc/system_utils.cpp b/code/nel/src/misc/system_utils.cpp index 2746c3f39..3997f75ab 100644 --- a/code/nel/src/misc/system_utils.cpp +++ b/code/nel/src/misc/system_utils.cpp @@ -411,21 +411,7 @@ typedef HRESULT (WINAPI* LPCREATEDXGIFACTORY)(REFIID, void**); static std::string FormatError(HRESULT hr) { - std::string res; - - LPTSTR errorText = NULL; - - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errorText, 0, NULL); - - if (errorText) - { - res = NLMISC::toString("%s (0x%x)", errorText, hr); - LocalFree(errorText); - errorText = NULL; - } - - return res; + return NLMISC::toString("%s (0x%x)", formatErrorMessage(hr).c_str(), hr); } struct SAdapter