/ tools / CleanUp_tool / main.cpp
main.cpp
  1  #include <windows.h>
  2  #include <cstdlib>
  3  #include <cstring>
  4  #include <shlwapi.h>
  5  #include <shlobj.h>
  6  
  7  static wchar_t szWindowClass[] = L"CleanUp tool";
  8  static wchar_t szTitle[] = L"Tool to clean up FancyZones installation";
  9  
 10  HINSTANCE hInst;
 11  
 12  LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 13  void CleanUp();
 14  void RemoveSettingsFolder();
 15  void ClearRegistry();
 16  
 17  int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
 18  {
 19      WNDCLASSEX wcex;
 20  
 21      wcex.cbSize = sizeof(WNDCLASSEX);
 22      wcex.style = CS_HREDRAW | CS_VREDRAW;
 23      wcex.lpfnWndProc = WndProc;
 24      wcex.cbClsExtra = 0;
 25      wcex.cbWndExtra = 0;
 26      wcex.hInstance = hInstance;
 27      wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
 28      wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
 29      wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
 30      wcex.lpszMenuName = nullptr;
 31      wcex.lpszClassName = szWindowClass;
 32      wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
 33  
 34      if (!RegisterClassEx(&wcex))
 35      {
 36          MessageBox(nullptr, L"Call to RegisterClassEx failed!", szTitle, NULL);
 37          return 1;
 38      }
 39  
 40      hInst = hInstance;
 41  
 42      HWND hWnd = CreateWindow(
 43          szWindowClass,
 44          szTitle,
 45          WS_OVERLAPPEDWINDOW,
 46          CW_USEDEFAULT, CW_USEDEFAULT,
 47          200, 200,
 48          nullptr,
 49          nullptr,
 50          hInstance,
 51          nullptr
 52      );
 53  
 54      if (!hWnd)
 55      {
 56          MessageBox(nullptr, L"Call to CreateWindow failed!", szTitle, NULL);
 57          return 1;
 58      }
 59  
 60      ShowWindow(hWnd, nCmdShow);
 61      UpdateWindow(hWnd);
 62  
 63      HWND hwndButton = CreateWindow(
 64          L"BUTTON",  // Predefined class; Unicode assumed 
 65          L"Clear",      // Button text 
 66          WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
 67          50,         // x position 
 68          50,         // y position 
 69          100,        // Button width
 70          100,        // Button height
 71          hWnd,     // Parent window
 72          (HMENU) 1,       // No menu.
 73          (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
 74          nullptr);      // Pointer not needed.
 75  
 76      MSG msg;
 77      while (GetMessage(&msg, nullptr, 0, 0))
 78      {
 79          TranslateMessage(&msg);
 80          DispatchMessage(&msg);
 81      }
 82  
 83      return (int)msg.wParam;
 84  }
 85  
 86  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 87  {
 88      PAINTSTRUCT ps;
 89      HDC hdc;
 90  
 91      switch (message)
 92      {
 93      case WM_PAINT:
 94          hdc = BeginPaint(hWnd, &ps);
 95          EndPaint(hWnd, &ps);
 96          break;
 97      case WM_COMMAND:
 98          if (wParam == 1)
 99          {
100              CleanUp();
101          }
102          break;
103      case WM_DESTROY:
104          PostQuitMessage(0);
105          break;
106      default:
107          return DefWindowProc(hWnd, message, wParam, lParam);
108          break;
109      }
110  
111      return 0;
112  }
113  
114  void CleanUp()
115  {
116      RemoveSettingsFolder();
117      ClearRegistry();
118  }
119  
120  void RemoveSettingsFolder()
121  {
122      wchar_t settingsPath[MAX_PATH];
123      if (SUCCEEDED(SHGetFolderPath(nullptr, ssfLOCALAPPDATA, nullptr, 0, settingsPath)))
124      {
125          PathAppend(settingsPath, L"\\Microsoft\\PowerToys");
126      }
127  
128      HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
129      if (FAILED(hr))
130      {
131          return;
132      }
133  
134      IFileOperation* pfo;
135      hr = CoCreateInstance(CLSID_FileOperation, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&pfo));
136      if (FAILED(hr))
137      {
138          return;
139      }
140  
141      hr = pfo->SetOperationFlags(FOF_NO_UI);
142      if (SUCCEEDED(hr))
143      {
144          IShellItem* psiFrom = nullptr;
145          hr = SHCreateItemFromParsingName(settingsPath, nullptr, IID_PPV_ARGS(&psiFrom));
146          if (SUCCEEDED(hr))
147          {
148              if (SUCCEEDED(hr))
149              {
150                  hr = pfo->DeleteItem(psiFrom, nullptr);
151              }
152              psiFrom->Release();
153          }
154  
155          if (SUCCEEDED(hr))
156          {
157              hr = pfo->PerformOperations();
158          }
159      }
160      pfo->Release();
161  }
162  
163  void ClearRegistry()
164  {
165      RegDeleteTreeW(HKEY_CURRENT_USER, L"Software\\SuperFancyZones");
166      RegDeleteTreeW(HKEY_CURRENT_USER, L"Software\\Microsoft\\PowerRename");
167      RegDeleteTreeW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\DontShowMeThisDialogAgain\\{e16ea82f-6d94-4f30-bb02-d6d911588afd}");
168      RegDeleteTreeW(HKEY_CURRENT_USER, L"Software\\Microsoft\\ImageResizer");
169  }