From 40a4a6a1a83d701c86d75f8bfb9bee540d9ef00a Mon Sep 17 00:00:00 2001 From: Yevhenii Holovachov <55396981+yevhenii44@users.noreply.github.com> Date: Thu, 27 Aug 2020 11:19:49 +0300 Subject: [PATCH] Clean Up tool (#5994) * Initial commit * Code review changes * Added PowerShell script to clean up PowerToys * Removed commented code * Added about and comments to script, fixed deleting of DontShowThisAgain directory inside registry * Added type to var's, changed positions of comments * Changed RegDeleteKey to RegDeleteTree --- tools/CleanUp_tool/CleanUp_tool.sln | 25 +++ tools/CleanUp_tool/CleanUp_tool.vcxproj | 88 +++++++++ .../CleanUp_tool/CleanUp_tool.vcxproj.filters | 22 +++ tools/CleanUp_tool/main.cpp | 169 ++++++++++++++++++ .../CleanUp_tool.ps1 | 48 +++++ 5 files changed, 352 insertions(+) create mode 100644 tools/CleanUp_tool/CleanUp_tool.sln create mode 100644 tools/CleanUp_tool/CleanUp_tool.vcxproj create mode 100644 tools/CleanUp_tool/CleanUp_tool.vcxproj.filters create mode 100644 tools/CleanUp_tool/main.cpp create mode 100644 tools/CleanUp_tool_powershell_script/CleanUp_tool.ps1 diff --git a/tools/CleanUp_tool/CleanUp_tool.sln b/tools/CleanUp_tool/CleanUp_tool.sln new file mode 100644 index 000000000..30c5abd47 --- /dev/null +++ b/tools/CleanUp_tool/CleanUp_tool.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30225.117 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CleanUp_tool", "CleanUp_tool.vcxproj", "{0995F59A-5074-42E4-AFE4-6335A0368E8E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0995F59A-5074-42E4-AFE4-6335A0368E8E}.Debug|x64.ActiveCfg = Debug|x64 + {0995F59A-5074-42E4-AFE4-6335A0368E8E}.Debug|x64.Build.0 = Debug|x64 + {0995F59A-5074-42E4-AFE4-6335A0368E8E}.Release|x64.ActiveCfg = Release|x64 + {0995F59A-5074-42E4-AFE4-6335A0368E8E}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B025F9FC-0262-4669-BADD-BBD61121FBD3} + EndGlobalSection +EndGlobal diff --git a/tools/CleanUp_tool/CleanUp_tool.vcxproj b/tools/CleanUp_tool/CleanUp_tool.vcxproj new file mode 100644 index 000000000..3f6c697c0 --- /dev/null +++ b/tools/CleanUp_tool/CleanUp_tool.vcxproj @@ -0,0 +1,88 @@ + + + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {0995f59a-5074-42e4-afe4-6335a0368e8e} + CleanUptool + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + true + + + false + + + + Level3 + true + _DEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + + + Windows + true + shlwapi.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + shlwapi.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file diff --git a/tools/CleanUp_tool/CleanUp_tool.vcxproj.filters b/tools/CleanUp_tool/CleanUp_tool.vcxproj.filters new file mode 100644 index 000000000..82c46ea9c --- /dev/null +++ b/tools/CleanUp_tool/CleanUp_tool.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/tools/CleanUp_tool/main.cpp b/tools/CleanUp_tool/main.cpp new file mode 100644 index 000000000..9dd1f6688 --- /dev/null +++ b/tools/CleanUp_tool/main.cpp @@ -0,0 +1,169 @@ +#include +#include +#include +#include +#include + +static wchar_t szWindowClass[] = L"CleanUp tool"; +static wchar_t szTitle[] = L"Tool to clean up FancyZones installation"; + +HINSTANCE hInst; + +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); +void CleanUp(); +void RemoveSettingsFolder(); +void ClearRegistry(); + +int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) +{ + WNDCLASSEX wcex; + + wcex.cbSize = sizeof(WNDCLASSEX); + wcex.style = CS_HREDRAW | CS_VREDRAW; + wcex.lpfnWndProc = WndProc; + wcex.cbClsExtra = 0; + wcex.cbWndExtra = 0; + wcex.hInstance = hInstance; + wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION); + wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); + wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); + wcex.lpszMenuName = nullptr; + wcex.lpszClassName = szWindowClass; + wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION); + + if (!RegisterClassEx(&wcex)) + { + MessageBox(nullptr, L"Call to RegisterClassEx failed!", szTitle, NULL); + return 1; + } + + hInst = hInstance; + + HWND hWnd = CreateWindow( + szWindowClass, + szTitle, + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, + 200, 200, + nullptr, + nullptr, + hInstance, + nullptr + ); + + if (!hWnd) + { + MessageBox(nullptr, L"Call to CreateWindow failed!", szTitle, NULL); + return 1; + } + + ShowWindow(hWnd, nCmdShow); + UpdateWindow(hWnd); + + HWND hwndButton = CreateWindow( + L"BUTTON", // Predefined class; Unicode assumed + L"Clear", // Button text + WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles + 50, // x position + 50, // y position + 100, // Button width + 100, // Button height + hWnd, // Parent window + (HMENU) 1, // No menu. + (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), + nullptr); // Pointer not needed. + + MSG msg; + while (GetMessage(&msg, nullptr, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + return (int)msg.wParam; +} + +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + PAINTSTRUCT ps; + HDC hdc; + + switch (message) + { + case WM_PAINT: + hdc = BeginPaint(hWnd, &ps); + EndPaint(hWnd, &ps); + break; + case WM_COMMAND: + if (wParam == 1) + { + CleanUp(); + } + break; + case WM_DESTROY: + PostQuitMessage(0); + break; + default: + return DefWindowProc(hWnd, message, wParam, lParam); + break; + } + + return 0; +} + +void CleanUp() +{ + RemoveSettingsFolder(); + ClearRegistry(); +} + +void RemoveSettingsFolder() +{ + wchar_t settingsPath[MAX_PATH]; + if (SUCCEEDED(SHGetFolderPath(nullptr, ssfLOCALAPPDATA, nullptr, 0, settingsPath))) + { + PathAppend(settingsPath, L"\\Microsoft\\PowerToys"); + } + + HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); + if (FAILED(hr)) + { + return; + } + + IFileOperation* pfo; + hr = CoCreateInstance(CLSID_FileOperation, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&pfo)); + if (FAILED(hr)) + { + return; + } + + hr = pfo->SetOperationFlags(FOF_NO_UI); + if (SUCCEEDED(hr)) + { + IShellItem* psiFrom = nullptr; + hr = SHCreateItemFromParsingName(settingsPath, nullptr, IID_PPV_ARGS(&psiFrom)); + if (SUCCEEDED(hr)) + { + if (SUCCEEDED(hr)) + { + hr = pfo->DeleteItem(psiFrom, nullptr); + } + psiFrom->Release(); + } + + if (SUCCEEDED(hr)) + { + hr = pfo->PerformOperations(); + } + } + pfo->Release(); +} + +void ClearRegistry() +{ + RegDeleteTreeW(HKEY_CURRENT_USER, L"Software\\SuperFancyZones"); + RegDeleteTreeW(HKEY_CURRENT_USER, L"Software\\Microsoft\\PowerRename"); + RegDeleteTreeW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\DontShowMeThisDialogAgain\\{e16ea82f-6d94-4f30-bb02-d6d911588afd}"); + RegDeleteTreeW(HKEY_CURRENT_USER, L"Software\\Microsoft\\ImageResizer"); +} diff --git a/tools/CleanUp_tool_powershell_script/CleanUp_tool.ps1 b/tools/CleanUp_tool_powershell_script/CleanUp_tool.ps1 new file mode 100644 index 000000000..24474780f --- /dev/null +++ b/tools/CleanUp_tool_powershell_script/CleanUp_tool.ps1 @@ -0,0 +1,48 @@ +#CleanUp tool 1.0 +#Copyright (C) 2020 Microsoft Corporation +#Tool to clean PowerToys settings inside AppData folder and registry + +#Deleting json settings files in %AppData%/Local/Microsoft/PowerToys. + +[String]$SettingsPath = $Env:LOCALAPPDATA + '\Microsoft\PowerToys' + +if (Test-Path -Path $SettingsPath -PathType Any) +{ + Remove-Item –Path $SettingsPath –Recurse +} + +#Deleting SuperFancyZones registry key + +[String]$SuperFancyZones = "HKCU:\Software\SuperFancyZones" + +if (Test-Path -Path $SuperFancyZones -PathType Any) +{ + Remove-Item –Path $SuperFancyZones –Recurse +} + +#Deleting PowerRename registry key + +[String]$PowerRename = "HKCU:\Software\Microsoft\PowerRename" + +if (Test-Path -Path $PowerRename -PathType Any) +{ + Remove-Item –Path $PowerRename –Recurse +} + +#Deleting ImageResizer registry key + +[String]$ImageResizer = "HKCU:\Software\Microsoft\ImageResizer" + +if (Test-Path -Path $ImageResizer -PathType Any) +{ + Remove-Item –Path $ImageResizer –Recurse +} + +#Deleting DontShowThisDialogAgain registry key + +[String]$DontShowThisDialogAgain = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\DontShowMeThisDialogAgain\{e16ea82f-6d94-4f30-bb02-d6d911588afd}" + +if (Test-Path -Path $DontShowThisDialogAgain -PathType Any) +{ + Remove-Item –Path $DontShowThisDialogAgain +}