Changed: Reuse NLMISC::formatErrorMessage instead of native FormatMessage

This commit is contained in:
kervala 2016-02-12 16:04:18 +01:00
parent f2ae43d229
commit a69103237f
4 changed files with 13 additions and 45 deletions

View file

@ -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);

View file

@ -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 );
}
}

View file

@ -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

View file

@ -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