From 4a30b5af2909452db7995b75406c45429c85f4ef Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Mon, 22 Jun 2020 17:01:56 +1000 Subject: Update thmviewer to show theme load errors. --- Tools.sln | 1 + src/thmviewer/load.cpp | 2 + src/thmviewer/packages.config | 4 +- src/thmviewer/precomp.h | 1 + src/thmviewer/thmviewer.cpp | 94 +++++++++++++++++++++++++++++++++++++---- src/thmviewer/thmviewer.vcxproj | 4 +- 6 files changed, 94 insertions(+), 12 deletions(-) diff --git a/Tools.sln b/Tools.sln index d24fc2a4..66f867d5 100644 --- a/Tools.sln +++ b/Tools.sln @@ -73,6 +73,7 @@ Global {0DF5D4CF-8457-469D-8288-13775E984F70}.Release|x86.ActiveCfg = Release|Any CPU {0DF5D4CF-8457-469D-8288-13775E984F70}.Release|x86.Build.0 = Release|Any CPU {95228C13-97F5-484A-B4A2-ECF4618B0881}.Debug|Any CPU.ActiveCfg = Debug|Win32 + {95228C13-97F5-484A-B4A2-ECF4618B0881}.Debug|Any CPU.Build.0 = Debug|Win32 {95228C13-97F5-484A-B4A2-ECF4618B0881}.Debug|x86.ActiveCfg = Debug|Win32 {95228C13-97F5-484A-B4A2-ECF4618B0881}.Debug|x86.Build.0 = Debug|Win32 {95228C13-97F5-484A-B4A2-ECF4618B0881}.Release|Any CPU.ActiveCfg = Release|Win32 diff --git a/src/thmviewer/load.cpp b/src/thmviewer/load.cpp index cd48caff..0267402a 100644 --- a/src/thmviewer/load.cpp +++ b/src/thmviewer/load.cpp @@ -106,6 +106,8 @@ static DWORD WINAPI LoadThreadProc( FILETIME ftModified = { }; FileGetTime(sczThemePath, NULL, NULL, &ftModified); + ::SendMessageW(hWnd, WM_THMVWR_THEME_LOAD_BEGIN, 0, 0); + // Try to load the theme file. hr = ThemeLoadFromFile(sczThemePath, &pTheme); if (FAILED(hr)) diff --git a/src/thmviewer/packages.config b/src/thmviewer/packages.config index 05bd6cc6..38ce9597 100644 --- a/src/thmviewer/packages.config +++ b/src/thmviewer/packages.config @@ -1,5 +1,5 @@ - + - + \ No newline at end of file diff --git a/src/thmviewer/precomp.h b/src/thmviewer/precomp.h index 1d186cf4..15d889fc 100644 --- a/src/thmviewer/precomp.h +++ b/src/thmviewer/precomp.h @@ -42,6 +42,7 @@ enum WM_THMVWR WM_THMVWR_PARSE_FILE, WM_THMVWR_NEW_THEME, WM_THMVWR_THEME_LOAD_ERROR, + WM_THMVWR_THEME_LOAD_BEGIN, }; extern "C" HRESULT DisplayStart( diff --git a/src/thmviewer/thmviewer.cpp b/src/thmviewer/thmviewer.cpp index 425cdf43..adad3f50 100644 --- a/src/thmviewer/thmviewer.cpp +++ b/src/thmviewer/thmviewer.cpp @@ -6,6 +6,7 @@ static const LPCWSTR THMVWR_WINDOW_CLASS_MAIN = L"ThmViewerMain"; static THEME* vpTheme = NULL; static DWORD vdwDisplayThreadId = 0; +static LPWSTR vsczThemeLoadErrors = NULL; enum THMVWR_CONTROL { @@ -39,6 +40,9 @@ static LRESULT CALLBACK MainWndProc( __in WPARAM wParam, __in LPARAM lParam ); +static void OnThemeLoadBegin( + __in_z_opt LPWSTR sczThemeLoadErrors + ); static void OnThemeLoadError( __in THEME* pTheme, __in HRESULT hrFailure @@ -48,6 +52,15 @@ static void OnNewTheme( __in HWND hWnd, __in HANDLE_THEME* pHandle ); +static void CALLBACK ThmviewerTraceError( + __in_z LPCSTR szFile, + __in int iLine, + __in REPORT_LEVEL rl, + __in UINT source, + __in HRESULT hrError, + __in_z __format_string LPCSTR szFormat, + __in va_list args + ); int WINAPI wWinMain( @@ -76,6 +89,8 @@ int WINAPI wWinMain( ExitOnFailure(hr, "Failed to initialize COM."); fComInitialized = TRUE; + DutilInitialize(&ThmviewerTraceError); + hr = ProcessCommandLine(lpCmdLine, &sczThemeFile, &sczWxlFile); ExitOnFailure(hr, "Failed to process command line."); @@ -161,6 +176,7 @@ LExit: ThemeFree(vpTheme); ThemeUninitialize(); + DutilUninitialize(); // uninitialize COM if (fComInitialized) @@ -168,11 +184,45 @@ LExit: ::CoUninitialize(); } + ReleaseNullStr(vsczThemeLoadErrors); ReleaseStr(sczThemeFile); ReleaseStr(sczWxlFile); return hr; } +static void CALLBACK ThmviewerTraceError( + __in_z LPCSTR /*szFile*/, + __in int /*iLine*/, + __in REPORT_LEVEL /*rl*/, + __in UINT source, + __in HRESULT hrError, + __in_z __format_string LPCSTR szFormat, + __in va_list args + ) +{ + HRESULT hr = S_OK; + LPSTR sczFormattedAnsi = NULL; + LPWSTR sczMessage = NULL; + + if (DUTIL_SOURCE_THMUTIL != source) + { + ExitFunction(); + } + + hr = StrAnsiAllocFormattedArgs(&sczFormattedAnsi, szFormat, args); + ExitOnFailure(hr, "Failed to format error log string."); + + hr = StrAllocFormatted(&sczMessage, L"Error 0x%08x: %S\r\n", hrError, sczFormattedAnsi); + ExitOnFailure(hr, "Failed to prepend error number to error log string."); + + hr = StrAllocConcat(&vsczThemeLoadErrors, sczMessage, 0); + ExitOnFailure(hr, "Failed to append theme load error."); + +LExit: + ReleaseStr(sczFormattedAnsi); + ReleaseStr(sczMessage); +} + // // ProcessCommandLine - process the provided command line arguments. @@ -311,6 +361,10 @@ static LRESULT CALLBACK MainWndProc( } break; + case WM_THMVWR_THEME_LOAD_BEGIN: + OnThemeLoadBegin(vsczThemeLoadErrors); + return 0; + case WM_THMVWR_THEME_LOAD_ERROR: OnThemeLoadError(vpTheme, lParam); return 0; @@ -351,6 +405,13 @@ static LRESULT CALLBACK MainWndProc( return ThemeDefWindowProc(vpTheme, hWnd, uMsg, wParam, lParam); } +static void OnThemeLoadBegin( + __in_z_opt LPWSTR sczThemeLoadErrors + ) +{ + ReleaseNullStr(sczThemeLoadErrors); +} + static void OnThemeLoadError( __in THEME* pTheme, __in HRESULT hrFailure @@ -358,6 +419,8 @@ static void OnThemeLoadError( { HRESULT hr = S_OK; LPWSTR sczMessage = NULL; + LPWSTR* psczErrors = NULL; + UINT cErrors = 0; TVINSERTSTRUCTW tvi = { }; // Add the application node. @@ -368,22 +431,37 @@ static void OnThemeLoadError( tvi.item.pszText = L"Failed to load theme."; tvi.hParent = reinterpret_cast(ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi))); - hr = StrAllocFormatted(&sczMessage, L"Error 0x%08x.", hrFailure); - ExitOnFailure(hr, "Failed to format error message."); + if (!vsczThemeLoadErrors) + { + hr = StrAllocFormatted(&sczMessage, L"Error 0x%08x.", hrFailure); + ExitOnFailure(hr, "Failed to format error message."); + + tvi.item.pszText = sczMessage; + ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi)); - tvi.item.pszText = sczMessage; - ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi)); + hr = StrAllocFromError(&sczMessage, hrFailure, NULL); + ExitOnFailure(hr, "Failed to format error message text."); - hr = StrAllocFromError(&sczMessage, hrFailure, NULL); - ExitOnFailure(hr, "Failed to format error message text."); + tvi.item.pszText = sczMessage; + ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi)); + } + else + { + hr = StrSplitAllocArray(&psczErrors, &cErrors, vsczThemeLoadErrors, L"\r\n"); + ExitOnFailure(hr, "Failed to split theme load errors."); - tvi.item.pszText = sczMessage; - ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi)); + for (DWORD i = 0; i < cErrors; ++i) + { + tvi.item.pszText = psczErrors[i]; + ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_INSERTITEMW, 0, reinterpret_cast(&tvi)); + } + } ThemeSendControlMessage(pTheme, THMVWR_CONTROL_TREE, TVM_EXPAND, TVE_EXPAND, reinterpret_cast(tvi.hParent)); LExit: ReleaseStr(sczMessage); + ReleaseMem(psczErrors); } diff --git a/src/thmviewer/thmviewer.vcxproj b/src/thmviewer/thmviewer.vcxproj index ea589b4c..47b72f78 100644 --- a/src/thmviewer/thmviewer.vcxproj +++ b/src/thmviewer/thmviewer.vcxproj @@ -2,7 +2,7 @@ - + @@ -70,6 +70,6 @@ This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + -- cgit v1.2.3-55-g6feb