diff options
author | Brad King <brad.king@kitware.com> | 2003-07-07 13:38:14 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2003-07-07 13:38:14 (GMT) |
commit | 78edd5167177d01d9792700b3f8a2494b594e830 (patch) | |
tree | 5dc07ebed9acb2deeb4ed39efc94141751aed36f /Source/kwsys/ProcessWin32.c | |
parent | 7479303e01e01595ce384582ed5507909f3744dc (diff) | |
download | CMake-78edd5167177d01d9792700b3f8a2494b594e830.zip CMake-78edd5167177d01d9792700b3f8a2494b594e830.tar.gz CMake-78edd5167177d01d9792700b3f8a2494b594e830.tar.bz2 |
ENH: Made call to FormatMessage more robust.
Diffstat (limited to 'Source/kwsys/ProcessWin32.c')
-rw-r--r-- | Source/kwsys/ProcessWin32.c | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/Source/kwsys/ProcessWin32.c b/Source/kwsys/ProcessWin32.c index 8f7a0d3..5296f2f 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; } |