From 4451fd13a1accec8d7c2b1bb7fdafd6e50a2566b Mon Sep 17 00:00:00 2001 From: attilamolnar Date: Mon, 3 Dec 2012 20:32:22 +0100 Subject: Fix broken RetrieveLastError() - FORMAT_MESSAGE_IGNORE_INSERTS flag wasn't used and we never pass insertion parameters, this could cause FormatMessage() to fail - The return value of FormatMessage() wasn't checked, if it returned zero (failure) then RetrieveLastError() returned garbage if err[] wasn't initialized previously --- win/win32service.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/win/win32service.cpp b/win/win32service.cpp index 81f8e7516..b04eff558 100644 --- a/win/win32service.cpp +++ b/win/win32service.cpp @@ -55,7 +55,9 @@ SETSERVDESC ChangeServiceConf; LPCSTR RetrieveLastError() { static char err[100]; - FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)err, sizeof(err), 0); + DWORD LastError = GetLastError(); + if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, 0, LastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)err, sizeof(err), 0) == 0) + snprintf(err, sizeof(err), "Error code: %d", LastError); SetLastError(ERROR_SUCCESS); return err; } -- cgit v1.2.3