[RAPPS] Properly use FormatMessageW with WinInet message table

Addendum to fb1582d. CORE-17375 CORE-17377
This commit is contained in:
Stanislav Motylkov 2020-11-20 21:16:37 +03:00
parent 8297109b95
commit 059ba9b0c0
No known key found for this signature in database
GPG key ID: AFE513258CBA9E92

View file

@ -522,25 +522,28 @@ VOID CDownloadManager::UpdateProgress(
} }
} }
VOID ShowLastError( BOOL ShowLastError(
HWND hWndOwner, HWND hWndOwner,
BOOL bInetError,
DWORD dwLastError) DWORD dwLastError)
{ {
CLocalPtr<WCHAR> lpMsg; CLocalPtr<WCHAR> lpMsg;
if (!FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | if (!FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_IGNORE_INSERTS, (bInetError ? FORMAT_MESSAGE_FROM_HMODULE : FORMAT_MESSAGE_FROM_SYSTEM),
NULL, (bInetError ? GetModuleHandleW(L"wininet.dll") : NULL),
dwLastError, dwLastError,
LANG_USER_DEFAULT, LANG_USER_DEFAULT,
(LPWSTR)&lpMsg, (LPWSTR)&lpMsg,
0, NULL)) 0, NULL))
{ {
return; DPRINT1("FormatMessageW unexpected failure (err %d)\n", GetLastError());
return FALSE;
} }
MessageBoxW(hWndOwner, lpMsg, NULL, MB_OK | MB_ICONERROR); MessageBoxW(hWndOwner, lpMsg, NULL, MB_OK | MB_ICONERROR);
return TRUE;
} }
unsigned int WINAPI CDownloadManager::ThreadFunc(LPVOID param) unsigned int WINAPI CDownloadManager::ThreadFunc(LPVOID param)
@ -596,7 +599,7 @@ unsigned int WINAPI CDownloadManager::ThreadFunc(LPVOID param)
{ {
if (!GetStorageDirectory(Path)) if (!GetStorageDirectory(Path))
{ {
ShowLastError(hMainWnd, GetLastError()); ShowLastError(hMainWnd, FALSE, GetLastError());
goto end; goto end;
} }
} }
@ -646,7 +649,7 @@ unsigned int WINAPI CDownloadManager::ThreadFunc(LPVOID param)
{ {
if (!CreateDirectoryW(Path.GetString(), NULL)) if (!CreateDirectoryW(Path.GetString(), NULL))
{ {
ShowLastError(hMainWnd, GetLastError()); ShowLastError(hMainWnd, FALSE, GetLastError());
goto end; goto end;
} }
} }
@ -700,7 +703,7 @@ unsigned int WINAPI CDownloadManager::ThreadFunc(LPVOID param)
if (!hOpen) if (!hOpen)
{ {
ShowLastError(hMainWnd, GetLastError()); ShowLastError(hMainWnd, TRUE, GetLastError());
goto end; goto end;
} }
@ -715,7 +718,7 @@ unsigned int WINAPI CDownloadManager::ThreadFunc(LPVOID param)
if (!InternetCrackUrlW(InfoArray[iAppId].szUrl, urlLength + 1, ICU_DECODE | ICU_ESCAPE, &urlComponents)) if (!InternetCrackUrlW(InfoArray[iAppId].szUrl, urlLength + 1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
{ {
ShowLastError(hMainWnd, GetLastError()); ShowLastError(hMainWnd, TRUE, GetLastError());
goto end; goto end;
} }
@ -729,14 +732,18 @@ unsigned int WINAPI CDownloadManager::ThreadFunc(LPVOID param)
0); 0);
if (!hFile) if (!hFile)
{ {
ShowLastError(hMainWnd, GetLastError()); if (!ShowLastError(hMainWnd, TRUE, GetLastError()))
{
/* Workaround for CORE-17377 */
MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
}
goto end; goto end;
} }
// query connection // query connection
if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL)) if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL))
{ {
ShowLastError(hMainWnd, GetLastError()); ShowLastError(hMainWnd, TRUE, GetLastError());
goto end; goto end;
} }
@ -757,7 +764,11 @@ unsigned int WINAPI CDownloadManager::ThreadFunc(LPVOID param)
0); 0);
if (!hFile) if (!hFile)
{ {
ShowLastError(hMainWnd, GetLastError()); if (!ShowLastError(hMainWnd, TRUE, GetLastError()))
{
/* Workaround for CORE-17377 */
MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
}
goto end; goto end;
} }
@ -778,13 +789,13 @@ unsigned int WINAPI CDownloadManager::ThreadFunc(LPVOID param)
} }
else else
{ {
ShowLastError(hMainWnd, GetLastError()); ShowLastError(hMainWnd, FALSE, GetLastError());
goto end; goto end;
} }
} }
else else
{ {
ShowLastError(hMainWnd, hr); ShowLastError(hMainWnd, FALSE, hr);
goto end; goto end;
} }
} }
@ -842,7 +853,7 @@ unsigned int WINAPI CDownloadManager::ThreadFunc(LPVOID param)
if (hOut == INVALID_HANDLE_VALUE) if (hOut == INVALID_HANDLE_VALUE)
{ {
ShowLastError(hMainWnd, GetLastError()); ShowLastError(hMainWnd, FALSE, GetLastError());
goto end; goto end;
} }
@ -851,13 +862,13 @@ unsigned int WINAPI CDownloadManager::ThreadFunc(LPVOID param)
{ {
if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead)) if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead))
{ {
ShowLastError(hMainWnd, GetLastError()); ShowLastError(hMainWnd, TRUE, GetLastError());
goto end; goto end;
} }
if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL)) if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL))
{ {
ShowLastError(hMainWnd, GetLastError()); ShowLastError(hMainWnd, FALSE, GetLastError());
goto end; goto end;
} }
@ -947,7 +958,7 @@ run:
} }
else else
{ {
ShowLastError(hMainWnd, GetLastError()); ShowLastError(hMainWnd, FALSE, GetLastError());
} }
} }