[CLIPBRD][EVENTVWR][MPLAY32][WELCOME][SYSDM]

Fix the ShowLastWin32Error helpers:
- don't display the last error string if it's ERROR_SUCCESS (since the code usually expects to display the last error message in case of errors);
- *always* call FormatMessage with the FORMAT_MESSAGE_IGNORE_INSERTS flag enabled. Indeed if it happens that the message string associated to an error ID contains insert parameter placeholders, and since we don't give here FormatMessage any argument array, the call would result into a crash without this flag.

svn path=/trunk/; revision=72897
This commit is contained in:
Hermès Bélusca-Maïto 2016-10-03 12:22:29 +00:00
parent eb94308bbb
commit 1c4750792d
5 changed files with 68 additions and 39 deletions

View file

@ -14,9 +14,21 @@ void ShowLastWin32Error(HWND hwndParent)
LPWSTR lpMsgBuf = NULL;
dwError = GetLastError();
if (dwError == ERROR_SUCCESS)
return;
if (!FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError,
LANG_USER_DEFAULT,
(LPWSTR)&lpMsgBuf,
0, NULL))
{
return;
}
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, dwError, 0, (LPWSTR)&lpMsgBuf, 0, NULL);
MessageBoxW(hwndParent, lpMsgBuf, NULL, MB_OK | MB_ICONERROR);
LocalFree(lpMsgBuf);
}

View file

@ -209,16 +209,23 @@ ShowLastWin32Error(HWND hwnd)
LPTSTR lpMessageBuffer;
DWORD dwError = GetLastError();
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMessageBuffer,
0, NULL) != 0)
if (dwError == ERROR_SUCCESS)
return;
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError,
LANG_USER_DEFAULT,
(LPTSTR)&lpMessageBuffer,
0, NULL))
{
MessageBox(hwnd, lpMessageBuffer, szAppTitle, MB_OK | MB_ICONERROR);
if (lpMessageBuffer) LocalFree(lpMessageBuffer);
return;
}
MessageBox(hwnd, lpMessageBuffer, szAppTitle, MB_OK | MB_ICONERROR);
LocalFree(lpMessageBuffer);
}
static VOID

View file

@ -245,13 +245,20 @@ ShowLastWin32Error(VOID)
LPWSTR lpMessageBuffer;
dwError = GetLastError();
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError,
0,
(LPWSTR)&lpMessageBuffer,
0,
NULL);
if (dwError == ERROR_SUCCESS)
return;
if (!FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError,
LANG_USER_DEFAULT,
(LPWSTR)&lpMessageBuffer,
0, NULL))
{
return;
}
MessageBoxW(hwndMainWindow, lpMessageBuffer, szTitle, MB_OK | MB_ICONERROR);
LocalFree(lpMessageBuffer);

View file

@ -113,20 +113,23 @@ ShowLastWin32Error(HWND hWnd)
LPTSTR lpMessageBuffer = NULL;
DWORD dwError = GetLastError();
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMessageBuffer,
0, NULL))
if (dwError == ERROR_SUCCESS)
return;
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError,
LANG_USER_DEFAULT,
(LPTSTR)&lpMessageBuffer,
0, NULL))
{
MessageBox(hWnd, lpMessageBuffer, szAppTitle, MB_OK | MB_ICONERROR);
return;
}
if (lpMessageBuffer)
{
LocalFree(lpMessageBuffer);
}
MessageBox(hWnd, lpMessageBuffer, szAppTitle, MB_OK | MB_ICONERROR);
LocalFree(lpMessageBuffer);
}
#endif

View file

@ -35,23 +35,23 @@ VOID ShowLastWin32Error(HWND hWndOwner)
DWORD LastError;
LastError = GetLastError();
if (LastError == ERROR_SUCCESS)
return;
if ((LastError == 0) ||
!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
LastError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsg,
0,
NULL))
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
LastError,
LANG_USER_DEFAULT,
(LPTSTR)&lpMsg,
0, NULL))
{
return;
}
MessageBox(hWndOwner, lpMsg, NULL, MB_OK | MB_ICONERROR);
LocalFree((LPVOID)lpMsg);
LocalFree(lpMsg);
}