From 78edd5167177d01d9792700b3f8a2494b594e830 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 7 Jul 2003 09:38:14 -0400 Subject: [PATCH] ENH: Made call to FormatMessage more robust. --- Source/kwsys/ProcessWin32.c | 39 ++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/Source/kwsys/ProcessWin32.c b/Source/kwsys/ProcessWin32.c index 8f7a0d347..5296f2f1d 100644 --- a/Source/kwsys/ProcessWin32.c +++ b/Source/kwsys/ProcessWin32.c @@ -1237,9 +1237,42 @@ void kwsysProcessCleanup(kwsysProcess* cp, int error) /* If this is an error case, report the error. */ if(error) { - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - cp->ErrorMessage, CMPE_PIPE_BUFFER_SIZE, 0); + /* Format the error message. */ + DWORD original = GetLastError(); + DWORD length = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, 0, original, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + cp->ErrorMessage, CMPE_PIPE_BUFFER_SIZE, 0); + + if(length > 0) + { + /* Remove trailing period and newline, if any. */ + if(cp->ErrorMessage[length-1] == '\n') + { + cp->ErrorMessage[length-1] = 0; + --length; + if(length > 0 && cp->ErrorMessage[length-1] == '\r') + { + cp->ErrorMessage[length-1] = 0; + --length; + } + } + if(cp->ErrorMessage[length-1] == '.') + { + cp->ErrorMessage[length-1] = 0; + --length; + } + } + else + { + /* FormatMessage failed. Use a default message. */ + _snprintf(cp->ErrorMessage, CMPE_PIPE_BUFFER_SIZE, + "Process execution failed with error 0x%X. " + "FormatMessage failed with error 0x%X.", + original, GetLastError()); + } + + /* Set the error state. */ cp->State = kwsysProcess_State_Error; }