| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008 The Eraser Project |
|---|
| 4 | * Original Author: Kasra Nassiri <cjax@users.sourceforge.net> |
|---|
| 5 | * Modified By: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| 6 | * |
|---|
| 7 | * This file is part of Eraser. |
|---|
| 8 | * |
|---|
| 9 | * Eraser is free software: you can redistribute it and/or modify it under the |
|---|
| 10 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
|---|
| 12 | * version. |
|---|
| 13 | * |
|---|
| 14 | * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|---|
| 16 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * A copy of the GNU General Public License can be found at |
|---|
| 19 | * <http://www.gnu.org/licenses/>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | #include "stdafx.h" |
|---|
| 23 | #include "CtxMenu.h" |
|---|
| 24 | #include "DllMain.h" |
|---|
| 25 | #include <sstream> |
|---|
| 26 | |
|---|
| 27 | extern "C" |
|---|
| 28 | { |
|---|
| 29 | typedef LONG NTSTATUS; |
|---|
| 30 | enum KEY_INFORMATION_CLASS |
|---|
| 31 | { |
|---|
| 32 | KeyBasicInformation, |
|---|
| 33 | KeyNodeInformation, |
|---|
| 34 | KeyFullInformation, |
|---|
| 35 | KeyNameInformation, |
|---|
| 36 | KeyCachedInformation, |
|---|
| 37 | KeyVirtualizationInformation |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | struct KEY_BASIC_INFORMATION |
|---|
| 41 | { |
|---|
| 42 | LARGE_INTEGER LastWriteTime; |
|---|
| 43 | ULONG TitleIndex; |
|---|
| 44 | ULONG NameLength; |
|---|
| 45 | WCHAR Name[1]; |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | struct KEY_NODE_INFORMATION |
|---|
| 49 | { |
|---|
| 50 | LARGE_INTEGER LastWriteTime; |
|---|
| 51 | ULONG TitleIndex; |
|---|
| 52 | ULONG ClassOffset; |
|---|
| 53 | ULONG ClassLength; |
|---|
| 54 | ULONG NameLength; |
|---|
| 55 | WCHAR Name[1]; |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | typedef NTSTATUS (__stdcall *pNtQueryKey)(HANDLE KeyHandle, KEY_INFORMATION_CLASS KeyInformationClass, |
|---|
| 59 | PVOID KeyInformation, ULONG Length, PULONG ResultLength); |
|---|
| 60 | pNtQueryKey NtQueryKey = NULL; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | template<typename handleType> class Handle |
|---|
| 64 | { |
|---|
| 65 | public: |
|---|
| 66 | Handle() |
|---|
| 67 | { |
|---|
| 68 | Object = NULL; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | Handle(handleType handle) |
|---|
| 72 | { |
|---|
| 73 | Object = handle; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | ~Handle() |
|---|
| 77 | { |
|---|
| 78 | DeleteObject(Object); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | operator handleType&() |
|---|
| 82 | { |
|---|
| 83 | return Object; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | private: |
|---|
| 87 | handleType Object; |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | Handle<HANDLE>::~Handle() |
|---|
| 91 | { |
|---|
| 92 | CloseHandle(Object); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | Handle<HKEY>::~Handle() |
|---|
| 96 | { |
|---|
| 97 | CloseHandle(Object); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | namespace Eraser { |
|---|
| 101 | HRESULT CCtxMenu::FinalConstruct() |
|---|
| 102 | { |
|---|
| 103 | //Initialise member variables. |
|---|
| 104 | MenuID = 0; |
|---|
| 105 | std::wstring menuTitle(LoadString(IDS_ERASER)); |
|---|
| 106 | MenuTitle = new wchar_t[menuTitle.length() + 1]; |
|---|
| 107 | wcscpy_s(MenuTitle, menuTitle.length() + 1, menuTitle.c_str()); |
|---|
| 108 | |
|---|
| 109 | //Check if the shell extension has been disabled. |
|---|
| 110 | Handle<HKEY> eraserKey; |
|---|
| 111 | LONG openKeyResult = RegOpenKeyEx(HKEY_CURRENT_USER, |
|---|
| 112 | L"Software\\Eraser\\Eraser 6\\3460478d-ed1b-4ecc-96c9-2ca0e8500557\\", 0, |
|---|
| 113 | KEY_READ, &static_cast<HKEY&>(eraserKey)); |
|---|
| 114 | |
|---|
| 115 | switch (openKeyResult) |
|---|
| 116 | { |
|---|
| 117 | case ERROR_FILE_NOT_FOUND: |
|---|
| 118 | //No settings defined: we default to enabling the shell extension. |
|---|
| 119 | return S_OK; |
|---|
| 120 | |
|---|
| 121 | case ERROR_SUCCESS: |
|---|
| 122 | break; |
|---|
| 123 | |
|---|
| 124 | default: |
|---|
| 125 | return E_FAIL; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | //Check the value of the IntegrateWithShell value. |
|---|
| 129 | DWORD valueType; |
|---|
| 130 | BYTE valueBuffer[512]; |
|---|
| 131 | DWORD valueBufferSize = sizeof(valueBuffer); |
|---|
| 132 | DWORD error = RegQueryValueEx(eraserKey, L"IntegrateWithShell", NULL, &valueType, |
|---|
| 133 | valueBuffer, &valueBufferSize); |
|---|
| 134 | if (error == ERROR_SUCCESS) |
|---|
| 135 | { |
|---|
| 136 | if (!*reinterpret_cast<char*>(valueBuffer + 51)) |
|---|
| 137 | return E_FAIL; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | return S_OK; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | HRESULT CCtxMenu::FinalRelease() |
|---|
| 144 | { |
|---|
| 145 | delete[] MenuTitle; |
|---|
| 146 | return S_OK; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | HRESULT CCtxMenu::Initialize(LPCITEMIDLIST pidlFolder, LPDATAOBJECT pDataObj, |
|---|
| 150 | HKEY hProgID) |
|---|
| 151 | { |
|---|
| 152 | //Determine where the shell extension was invoked from. |
|---|
| 153 | if (GetHKeyPath(hProgID) == L"{645FF040-5081-101B-9F08-00AA002F954E}") |
|---|
| 154 | { |
|---|
| 155 | InvokeReason = INVOKEREASON_RECYCLEBIN; |
|---|
| 156 | |
|---|
| 157 | //We can't do much other processing: the LPDATAOBJECT parameter contains |
|---|
| 158 | //data that is a private type so we don't know how to query for it. |
|---|
| 159 | return S_OK; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | //Check pidlFolder for the drop path, if it exists. This is for drag-and-drop |
|---|
| 163 | //context menus. |
|---|
| 164 | else if (pidlFolder != NULL) |
|---|
| 165 | { |
|---|
| 166 | InvokeReason = INVOKEREASON_DRAGDROP; |
|---|
| 167 | |
|---|
| 168 | //Translate the drop path to a location on the filesystem. |
|---|
| 169 | wchar_t dropTargetPath[MAX_PATH]; |
|---|
| 170 | if (!SHGetPathFromIDList(pidlFolder, dropTargetPath)) |
|---|
| 171 | return E_FAIL; |
|---|
| 172 | |
|---|
| 173 | DragDropDestinationDirectory = dropTargetPath; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | //Okay, everything else is a simple context menu for a set of selected files/ |
|---|
| 177 | //folders/drives. |
|---|
| 178 | else |
|---|
| 179 | InvokeReason = INVOKEREASON_FILEFOLDER; |
|---|
| 180 | |
|---|
| 181 | //Look for CF_HDROP data in the data object. |
|---|
| 182 | FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL }; |
|---|
| 183 | STGMEDIUM stg = { TYMED_HGLOBAL }; |
|---|
| 184 | if (FAILED(pDataObj->GetData(&fmt, &stg))) |
|---|
| 185 | //Nope! Return an "invalid argument" error back to Explorer. |
|---|
| 186 | return E_INVALIDARG; |
|---|
| 187 | |
|---|
| 188 | //Get a pointer to the actual data. |
|---|
| 189 | HDROP hDrop = static_cast<HDROP>(GlobalLock(stg.hGlobal)); |
|---|
| 190 | if (hDrop == NULL) |
|---|
| 191 | return E_INVALIDARG; |
|---|
| 192 | |
|---|
| 193 | //Sanity check - make sure there is at least one filename. |
|---|
| 194 | UINT uNumFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0); |
|---|
| 195 | if (!uNumFiles) |
|---|
| 196 | { |
|---|
| 197 | GlobalUnlock(stg.hGlobal); |
|---|
| 198 | ReleaseStgMedium(&stg); |
|---|
| 199 | return E_INVALIDARG; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | //Collect all the files which have been selected. |
|---|
| 203 | HRESULT hr = S_OK; |
|---|
| 204 | WCHAR buffer[MAX_PATH] = {0}; |
|---|
| 205 | for (UINT i = 0; i < uNumFiles; i++) |
|---|
| 206 | { |
|---|
| 207 | UINT charsWritten = DragQueryFile(hDrop, i, buffer, sizeof(buffer) / sizeof(buffer[0])); |
|---|
| 208 | if (!charsWritten) |
|---|
| 209 | { |
|---|
| 210 | hr = E_INVALIDARG; |
|---|
| 211 | continue; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | SelectedFiles.push_back(std::wstring(buffer, charsWritten)); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | //Clean up. |
|---|
| 218 | GlobalUnlock(stg.hGlobal); |
|---|
| 219 | ReleaseStgMedium(&stg); |
|---|
| 220 | return hr; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | HRESULT CCtxMenu::QueryContextMenu(HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd, |
|---|
| 224 | UINT /*uidLastCmd*/, UINT uFlags) |
|---|
| 225 | { |
|---|
| 226 | //If the flags include CMF_DEFAULTONLY then we shouldn't do anything. |
|---|
| 227 | if (uFlags & CMF_DEFAULTONLY) |
|---|
| 228 | return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0); |
|---|
| 229 | |
|---|
| 230 | //First, create and populate a submenu. |
|---|
| 231 | UINT uID = uidFirstCmd; |
|---|
| 232 | HMENU hSubmenu = CreatePopupMenu(); |
|---|
| 233 | |
|---|
| 234 | //Create the submenu, following the order defined in the CEraserLPVERB enum, creating |
|---|
| 235 | //only items which are applicable. |
|---|
| 236 | Actions applicableActions = GetApplicableActions(); |
|---|
| 237 | VerbMenuIndices.clear(); |
|---|
| 238 | if (applicableActions & ACTION_ERASE) |
|---|
| 239 | { |
|---|
| 240 | InsertMenu(hSubmenu, ACTION_ERASE, MF_BYPOSITION, uID++, |
|---|
| 241 | LoadString(IDS_ACTION_ERASE).c_str()); //Erase |
|---|
| 242 | VerbMenuIndices.push_back(ACTION_ERASE); |
|---|
| 243 | } |
|---|
| 244 | if (applicableActions & ACTION_ERASE_ON_RESTART) |
|---|
| 245 | { |
|---|
| 246 | InsertMenu(hSubmenu, ACTION_ERASE_ON_RESTART, MF_BYPOSITION, uID++, |
|---|
| 247 | LoadString(IDS_ACTION_ERASERESTART).c_str()); //Erase on Restart |
|---|
| 248 | VerbMenuIndices.push_back(ACTION_ERASE_ON_RESTART); |
|---|
| 249 | } |
|---|
| 250 | if (applicableActions & ACTION_ERASE_UNUSED_SPACE) |
|---|
| 251 | { |
|---|
| 252 | InsertMenu(hSubmenu, ACTION_ERASE_UNUSED_SPACE, MF_BYPOSITION, uID++, |
|---|
| 253 | LoadString(IDS_ACTION_ERASEUNUSEDSPACE).c_str()); //Erase Unused Space |
|---|
| 254 | VerbMenuIndices.push_back(ACTION_ERASE_UNUSED_SPACE); |
|---|
| 255 | } |
|---|
| 256 | //------------------------------------------------------------------------- |
|---|
| 257 | if (applicableActions & ACTION_SECURE_MOVE) |
|---|
| 258 | { |
|---|
| 259 | if (uID - uidFirstCmd > 0) |
|---|
| 260 | { |
|---|
| 261 | std::auto_ptr<MENUITEMINFO> separator(GetSeparator()); |
|---|
| 262 | InsertMenuItem(hSubmenu, 0, FALSE, separator.get()); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | InsertMenu(hSubmenu, ACTION_SECURE_MOVE, MF_BYPOSITION, uID++, |
|---|
| 266 | LoadString(IDS_ACTION_SECUREMOVE).c_str()); //Secure Move |
|---|
| 267 | VerbMenuIndices.push_back(ACTION_SECURE_MOVE); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | //Insert the submenu into the Context menu provided by Explorer. |
|---|
| 271 | { |
|---|
| 272 | MENUITEMINFO mii = { sizeof(MENUITEMINFO) }; |
|---|
| 273 | mii.wID = uID++; |
|---|
| 274 | mii.fMask = MIIM_SUBMENU | MIIM_STRING | MIIM_ID; |
|---|
| 275 | mii.hSubMenu = hSubmenu; |
|---|
| 276 | mii.dwTypeData = const_cast<wchar_t*>(MenuTitle); |
|---|
| 277 | |
|---|
| 278 | //Set the bitmap for the registered item. Vista machines will be set using a DIB, |
|---|
| 279 | //older machines will be ownerdrawn. |
|---|
| 280 | OSVERSIONINFO osvi; |
|---|
| 281 | ZeroMemory(&osvi, sizeof(osvi)); |
|---|
| 282 | osvi.dwOSVersionInfoSize = sizeof(osvi); |
|---|
| 283 | |
|---|
| 284 | if (GetVersionEx(&osvi) && osvi.dwPlatformId == VER_PLATFORM_WIN32_NT && |
|---|
| 285 | osvi.dwMajorVersion >= 6) |
|---|
| 286 | { |
|---|
| 287 | mii.fMask |= MIIM_CHECKMARKS; |
|---|
| 288 | mii.hbmpUnchecked = GetMenuBitmap(); |
|---|
| 289 | } |
|---|
| 290 | else if (InvokeReason != INVOKEREASON_DRAGDROP) |
|---|
| 291 | { |
|---|
| 292 | mii.fMask |= MIIM_FTYPE; |
|---|
| 293 | mii.fType = MFT_OWNERDRAW; |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | MenuID = uMenuIndex++; |
|---|
| 297 | InsertMenuItem(hmenu, MenuID, TRUE, &mii); |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, uID - uidFirstCmd); |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | HRESULT CCtxMenu::HandleMenuMsg(UINT uMsg, WPARAM wParam, LPARAM lParam) |
|---|
| 304 | { |
|---|
| 305 | return HandleMenuMsg2(uMsg, wParam, lParam, NULL); |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | HRESULT CCtxMenu::HandleMenuMsg2(UINT uMsg, WPARAM /*wParam*/, LPARAM lParam, |
|---|
| 309 | LRESULT* result) |
|---|
| 310 | { |
|---|
| 311 | //Skip if we aren't handling our own. |
|---|
| 312 | bool handleResult = false; |
|---|
| 313 | switch (uMsg) |
|---|
| 314 | { |
|---|
| 315 | case WM_MEASUREITEM: |
|---|
| 316 | { |
|---|
| 317 | MEASUREITEMSTRUCT* mis = reinterpret_cast<MEASUREITEMSTRUCT*>(lParam); |
|---|
| 318 | if (mis->CtlID == MenuID) |
|---|
| 319 | handleResult = OnMeasureItem(mis->itemWidth, mis->itemHeight); |
|---|
| 320 | else |
|---|
| 321 | handleResult = false; |
|---|
| 322 | break; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | case WM_DRAWITEM: |
|---|
| 326 | { |
|---|
| 327 | DRAWITEMSTRUCT* dis = reinterpret_cast<DRAWITEMSTRUCT*>(lParam); |
|---|
| 328 | if (dis->CtlID == MenuID) |
|---|
| 329 | handleResult = OnDrawItem(dis->hDC, dis->rcItem, dis->itemAction, dis->itemState); |
|---|
| 330 | else |
|---|
| 331 | handleResult = false; |
|---|
| 332 | } |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | if (result) |
|---|
| 336 | *result = handleResult; |
|---|
| 337 | return S_OK; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | bool CCtxMenu::OnMeasureItem(UINT& itemWidth, UINT& itemHeight) |
|---|
| 341 | { |
|---|
| 342 | LOGFONT logFont; |
|---|
| 343 | if (!SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(logFont), &logFont, 0)) |
|---|
| 344 | return false; |
|---|
| 345 | |
|---|
| 346 | //Measure the size of the text. |
|---|
| 347 | Handle<HDC> screenDC = GetDC(NULL); |
|---|
| 348 | Handle<HFONT> font = CreateFontIndirect(&logFont); |
|---|
| 349 | SelectObject(screenDC, font); |
|---|
| 350 | SIZE textSize; |
|---|
| 351 | if (!GetTextExtentPoint32(screenDC, MenuTitle, static_cast<DWORD>(wcslen(MenuTitle)), &textSize)) |
|---|
| 352 | return false; |
|---|
| 353 | |
|---|
| 354 | itemWidth = textSize.cx; |
|---|
| 355 | itemHeight = textSize.cy; |
|---|
| 356 | |
|---|
| 357 | //Account for the size of the bitmap. |
|---|
| 358 | UINT iconWidth = GetSystemMetrics(SM_CXMENUCHECK); |
|---|
| 359 | itemWidth += iconWidth; |
|---|
| 360 | itemHeight = std::max(iconWidth, itemHeight); |
|---|
| 361 | |
|---|
| 362 | //And remember the minimum size for menu items. |
|---|
| 363 | itemHeight = std::max((int)itemHeight, GetSystemMetrics(SM_CXMENUSIZE)); |
|---|
| 364 | return true; |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | bool CCtxMenu::OnDrawItem(HDC hdc, RECT rect, UINT /*action*/, UINT state) |
|---|
| 368 | { |
|---|
| 369 | //Draw the background. |
|---|
| 370 | LOGBRUSH logBrush = { BS_SOLID, |
|---|
| 371 | (state & ODS_SELECTED) ? |
|---|
| 372 | GetSysColor(COLOR_HIGHLIGHT) : GetSysColor(COLOR_MENU), |
|---|
| 373 | 0 |
|---|
| 374 | }; |
|---|
| 375 | Handle<HBRUSH> bgBrush = CreateBrushIndirect(&logBrush); |
|---|
| 376 | FillRect(hdc, &rect, bgBrush); |
|---|
| 377 | |
|---|
| 378 | //Then the bitmap. |
|---|
| 379 | { |
|---|
| 380 | //Draw the icon with alpha and all first. |
|---|
| 381 | Handle<HICON> icon(GetMenuIcon()); |
|---|
| 382 | int iconSize = GetSystemMetrics(SM_CXMENUCHECK); |
|---|
| 383 | int iconMargin = GetSystemMetrics(SM_CXEDGE); |
|---|
| 384 | DrawIconEx(hdc, rect.left + iconMargin, rect.top + (rect.bottom - rect.top - iconSize) / 2, |
|---|
| 385 | icon, 0, 0, 0, bgBrush, DI_NORMAL); |
|---|
| 386 | |
|---|
| 387 | //Move the rectangle's left bound to the text starting position |
|---|
| 388 | rect.left += iconMargin * 2 + iconSize; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | //Draw the text. |
|---|
| 392 | SetBkMode(hdc, TRANSPARENT); |
|---|
| 393 | LOGFONT logFont; |
|---|
| 394 | if (!SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(logFont), &logFont, 0)) |
|---|
| 395 | return false; |
|---|
| 396 | |
|---|
| 397 | SIZE textSize; |
|---|
| 398 | if (!GetTextExtentPoint32(hdc, MenuTitle, static_cast<DWORD>(wcslen(MenuTitle)), &textSize)) |
|---|
| 399 | return false; |
|---|
| 400 | |
|---|
| 401 | COLORREF oldColour = SetTextColor(hdc, (state & ODS_SELECTED) ? |
|---|
| 402 | GetSysColor(COLOR_HIGHLIGHTTEXT) : GetSysColor(COLOR_MENUTEXT)); |
|---|
| 403 | UINT flags = DST_PREFIXTEXT; |
|---|
| 404 | if (state & ODS_NOACCEL) |
|---|
| 405 | flags |= DSS_HIDEPREFIX; |
|---|
| 406 | ::DrawState(hdc, NULL, NULL, reinterpret_cast<LPARAM>(MenuTitle), wcslen(MenuTitle), |
|---|
| 407 | rect.left, rect.top + (rect.bottom - rect.top - textSize.cy) / 2, textSize.cx, textSize.cy, flags); |
|---|
| 408 | SetTextColor(hdc, oldColour); |
|---|
| 409 | return true; |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | HRESULT CCtxMenu::GetCommandString(UINT_PTR idCmd, UINT uFlags, UINT* /*pwReserved*/, |
|---|
| 413 | LPSTR pszName, UINT cchMax) |
|---|
| 414 | { |
|---|
| 415 | //We only know how to handle help string requests. |
|---|
| 416 | if (!(uFlags & GCS_HELPTEXT)) |
|---|
| 417 | return E_INVALIDARG; |
|---|
| 418 | |
|---|
| 419 | //Get the command string for the given id |
|---|
| 420 | if (idCmd >= VerbMenuIndices.size()) |
|---|
| 421 | return E_INVALIDARG; |
|---|
| 422 | |
|---|
| 423 | std::wstring commandString; |
|---|
| 424 | switch (VerbMenuIndices[idCmd]) |
|---|
| 425 | { |
|---|
| 426 | case ACTION_ERASE: |
|---|
| 427 | commandString = LoadString(IDS_HELPSTRING_ERASE); |
|---|
| 428 | break; |
|---|
| 429 | case ACTION_ERASE_ON_RESTART: |
|---|
| 430 | commandString = LoadString(IDS_HELPSTRING_ERASEONRESTART); |
|---|
| 431 | break; |
|---|
| 432 | case ACTION_ERASE_UNUSED_SPACE: |
|---|
| 433 | commandString = LoadString(IDS_HELPSTRING_ERASEUNUSEDSPACE); |
|---|
| 434 | break; |
|---|
| 435 | case ACTION_SECURE_MOVE: |
|---|
| 436 | commandString = LoadString(IDS_HELPSTRING_SECUREMOVE); |
|---|
| 437 | break; |
|---|
| 438 | default: |
|---|
| 439 | //We don't know what action this is: return E_INVALIDARG. |
|---|
| 440 | return E_INVALIDARG; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | //Return the help string to Explorer. |
|---|
| 444 | if (uFlags & GCS_UNICODE) |
|---|
| 445 | wcscpy_s(reinterpret_cast<wchar_t*>(pszName), cchMax, commandString.c_str()); |
|---|
| 446 | else |
|---|
| 447 | { |
|---|
| 448 | size_t convCount = 0; |
|---|
| 449 | wcstombs_s(&convCount, pszName, cchMax, commandString.c_str(), commandString.length()); |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | return S_OK; |
|---|
| 453 | } |
|---|
| 454 | |
|---|
| 455 | HRESULT CCtxMenu::InvokeCommand(LPCMINVOKECOMMANDINFO pCmdInfo) |
|---|
| 456 | { |
|---|
| 457 | //If lpVerb really points to a string, ignore this function call and bail out. |
|---|
| 458 | if (HIWORD(pCmdInfo->lpVerb) != 0) |
|---|
| 459 | return E_INVALIDARG; |
|---|
| 460 | |
|---|
| 461 | //If the verb index refers to an item outside the bounds of our VerbMenuIndices |
|---|
| 462 | //vector, exit. |
|---|
| 463 | if (LOWORD(pCmdInfo->lpVerb) >= VerbMenuIndices.size()) |
|---|
| 464 | return E_INVALIDARG; |
|---|
| 465 | |
|---|
| 466 | //Build the command line |
|---|
| 467 | std::wstring commandAction; |
|---|
| 468 | std::wstring commandLine; |
|---|
| 469 | bool commandElevate = false; |
|---|
| 470 | HRESULT result = E_INVALIDARG; |
|---|
| 471 | switch (VerbMenuIndices[LOWORD(pCmdInfo->lpVerb)]) |
|---|
| 472 | { |
|---|
| 473 | case ACTION_ERASE_ON_RESTART: |
|---|
| 474 | commandLine += L"-s=restart "; |
|---|
| 475 | |
|---|
| 476 | case ACTION_ERASE: |
|---|
| 477 | { |
|---|
| 478 | //Add Task command. |
|---|
| 479 | commandAction = L"addtask"; |
|---|
| 480 | |
|---|
| 481 | //See the invocation context: if it is executed from the recycle bin |
|---|
| 482 | //then the list of selected files will be empty. |
|---|
| 483 | if (InvokeReason == INVOKEREASON_RECYCLEBIN) |
|---|
| 484 | { |
|---|
| 485 | commandLine += L"-r"; |
|---|
| 486 | } |
|---|
| 487 | else |
|---|
| 488 | { |
|---|
| 489 | //Okay, we were called from an item right-click; iterate over every |
|---|
| 490 | //selected file |
|---|
| 491 | for (std::list<std::wstring>::const_iterator i = SelectedFiles.begin(); |
|---|
| 492 | i != SelectedFiles.end(); ++i) |
|---|
| 493 | { |
|---|
| 494 | //Check if the current item is a file or folder. |
|---|
| 495 | std::wstring item(*i); |
|---|
| 496 | if (item.length() > 3 && item[item.length() - 1] == '\\') |
|---|
| 497 | item.erase(item.end() - 1); |
|---|
| 498 | DWORD attributes = GetFileAttributes(item.c_str()); |
|---|
| 499 | |
|---|
| 500 | //Add the correct command line for the file type. |
|---|
| 501 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
|---|
| 502 | commandLine += L"\"-d=" + item + L"\" "; |
|---|
| 503 | else |
|---|
| 504 | commandLine += L"\"" + item + L"\" "; |
|---|
| 505 | } |
|---|
| 506 | } |
|---|
| 507 | |
|---|
| 508 | break; |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | case ACTION_ERASE_UNUSED_SPACE: |
|---|
| 512 | { |
|---|
| 513 | //We want to add a new task |
|---|
| 514 | commandAction = L"addtask"; |
|---|
| 515 | |
|---|
| 516 | //Erasing unused space requires elevation |
|---|
| 517 | commandElevate = true; |
|---|
| 518 | |
|---|
| 519 | //Add every item onto the command line |
|---|
| 520 | for (std::list<std::wstring>::const_iterator i = SelectedFiles.begin(); |
|---|
| 521 | i != SelectedFiles.end(); ++i) |
|---|
| 522 | { |
|---|
| 523 | commandLine += L"\"-u=" + *i + L",clusterTips\" "; |
|---|
| 524 | } |
|---|
| 525 | |
|---|
| 526 | break; |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | default: |
|---|
| 530 | if (!(pCmdInfo->fMask & CMIC_MASK_FLAG_NO_UI)) |
|---|
| 531 | { |
|---|
| 532 | MessageBox(pCmdInfo->hwnd, FormatString(LoadString(IDS_ERROR_UNKNOWNACTION), |
|---|
| 533 | VerbMenuIndices[LOWORD(pCmdInfo->lpVerb)]).c_str(), |
|---|
| 534 | LoadString(IDS_ERASERSHELLEXT).c_str(), MB_OK | MB_ICONERROR); |
|---|
| 535 | } |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | try |
|---|
| 539 | { |
|---|
| 540 | RunEraser(commandAction, commandLine, commandElevate, pCmdInfo->hwnd, |
|---|
| 541 | pCmdInfo->nShow); |
|---|
| 542 | } |
|---|
| 543 | catch (const std::wstring& e) |
|---|
| 544 | { |
|---|
| 545 | if (!(pCmdInfo->fMask & CMIC_MASK_FLAG_NO_UI)) |
|---|
| 546 | { |
|---|
| 547 | MessageBox(pCmdInfo->hwnd, e.c_str(), LoadString(IDS_ERASERSHELLEXT).c_str(), |
|---|
| 548 | MB_OK | MB_ICONERROR); |
|---|
| 549 | } |
|---|
| 550 | } |
|---|
| 551 | |
|---|
| 552 | return result; |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | CCtxMenu::Actions CCtxMenu::GetApplicableActions() |
|---|
| 556 | { |
|---|
| 557 | unsigned result = 0; |
|---|
| 558 | |
|---|
| 559 | //First decide the actions which are applicable to the current invocation |
|---|
| 560 | //reason. |
|---|
| 561 | switch (InvokeReason) |
|---|
| 562 | { |
|---|
| 563 | case INVOKEREASON_RECYCLEBIN: |
|---|
| 564 | result |= ACTION_ERASE | ACTION_ERASE_ON_RESTART; |
|---|
| 565 | break; |
|---|
| 566 | case INVOKEREASON_FILEFOLDER: |
|---|
| 567 | result |= ACTION_ERASE | ACTION_ERASE_ON_RESTART | ACTION_ERASE_UNUSED_SPACE; |
|---|
| 568 | #if 0 |
|---|
| 569 | case INVOKEREASON_DRAGDROP: |
|---|
| 570 | result |= ACTION_SECURE_MOVE; |
|---|
| 571 | #endif |
|---|
| 572 | } |
|---|
| 573 | |
|---|
| 574 | //Remove actions that don't apply to the current invocation reason. |
|---|
| 575 | for (std::list<std::wstring>::const_iterator i = SelectedFiles.begin(); |
|---|
| 576 | i != SelectedFiles.end(); ++i) |
|---|
| 577 | { |
|---|
| 578 | //Remove trailing slashes if they are directories. |
|---|
| 579 | std::wstring item(*i); |
|---|
| 580 | |
|---|
| 581 | //Check if the path is a path to a volume, if it is not, remove the |
|---|
| 582 | //erase unused space verb. |
|---|
| 583 | wchar_t volumeName[MAX_PATH]; |
|---|
| 584 | if (!GetVolumeNameForVolumeMountPoint(item.c_str(), volumeName, |
|---|
| 585 | sizeof(volumeName) / sizeof(volumeName[0]))) |
|---|
| 586 | { |
|---|
| 587 | result &= ~ACTION_ERASE_UNUSED_SPACE; |
|---|
| 588 | } |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | return static_cast<Actions>(result); |
|---|
| 592 | } |
|---|
| 593 | |
|---|
| 594 | std::wstring CCtxMenu::LoadString(UINT stringID) |
|---|
| 595 | { |
|---|
| 596 | //Get a pointer to the buffer containing the string (we're copying it anyway) |
|---|
| 597 | wchar_t* buffer = NULL; |
|---|
| 598 | DWORD lastCount = ::LoadString(theApp.m_hInstance, stringID, |
|---|
| 599 | reinterpret_cast<wchar_t*>(&buffer), 0); |
|---|
| 600 | |
|---|
| 601 | if (lastCount > 0) |
|---|
| 602 | return std::wstring(buffer, lastCount); |
|---|
| 603 | return std::wstring(); |
|---|
| 604 | } |
|---|
| 605 | |
|---|
| 606 | std::wstring CCtxMenu::FormatString(const std::wstring& formatString, ...) |
|---|
| 607 | { |
|---|
| 608 | std::vector<wchar_t> formatStr(formatString.length() + 1); |
|---|
| 609 | wcscpy_s(&formatStr.front(), formatStr.size(), formatString.c_str()); |
|---|
| 610 | |
|---|
| 611 | std::vector<wchar_t> buffer(formatStr.size()); |
|---|
| 612 | for ( ; ; ) |
|---|
| 613 | { |
|---|
| 614 | buffer.resize(buffer.size() * 2); |
|---|
| 615 | va_list arguments; |
|---|
| 616 | va_start(arguments, formatString); |
|---|
| 617 | int result = vswprintf_s(&buffer.front(), buffer.size(), &formatStr.front(), |
|---|
| 618 | arguments); |
|---|
| 619 | va_end(arguments); |
|---|
| 620 | |
|---|
| 621 | if (result > 0 && static_cast<unsigned>(result) < buffer.size()) |
|---|
| 622 | { |
|---|
| 623 | break; |
|---|
| 624 | } |
|---|
| 625 | } |
|---|
| 626 | |
|---|
| 627 | //Return the result as a wstring |
|---|
| 628 | std::wstring result; |
|---|
| 629 | if (buffer.size() > 0) |
|---|
| 630 | result = &buffer.front(); |
|---|
| 631 | return result; |
|---|
| 632 | } |
|---|
| 633 | |
|---|
| 634 | std::wstring CCtxMenu::GetHKeyPath(HKEY handle) |
|---|
| 635 | { |
|---|
| 636 | if (NtQueryKey == NULL) |
|---|
| 637 | NtQueryKey = reinterpret_cast<pNtQueryKey>(GetProcAddress( |
|---|
| 638 | LoadLibrary(L"Ntdll.dll"), "NtQueryKey")); |
|---|
| 639 | |
|---|
| 640 | //Keep querying for the key information until enough buffer space has been allocated. |
|---|
| 641 | std::vector<char> buffer(sizeof(KEY_NODE_INFORMATION)); |
|---|
| 642 | NTSTATUS queryResult = STATUS_BUFFER_TOO_SMALL; |
|---|
| 643 | ULONG keyInfoSize = 0; |
|---|
| 644 | |
|---|
| 645 | while (queryResult == STATUS_BUFFER_TOO_SMALL || queryResult == STATUS_BUFFER_OVERFLOW) |
|---|
| 646 | { |
|---|
| 647 | buffer.resize(buffer.size() + keyInfoSize); |
|---|
| 648 | ZeroMemory(&buffer.front(), buffer.size()); |
|---|
| 649 | queryResult = NtQueryKey(handle, KeyNodeInformation, &buffer.front(), |
|---|
| 650 | static_cast<ULONG>(buffer.size()), &keyInfoSize); |
|---|
| 651 | } |
|---|
| 652 | |
|---|
| 653 | if (queryResult != STATUS_SUCCESS) |
|---|
| 654 | return std::wstring(); |
|---|
| 655 | |
|---|
| 656 | KEY_NODE_INFORMATION* keyInfo = reinterpret_cast<KEY_NODE_INFORMATION*>( |
|---|
| 657 | &buffer.front()); |
|---|
| 658 | return keyInfo->Name; |
|---|
| 659 | } |
|---|
| 660 | |
|---|
| 661 | void CCtxMenu::RunEraser(const std::wstring& action, const std::wstring& parameters, |
|---|
| 662 | bool elevated, HWND parent, int show) |
|---|
| 663 | { |
|---|
| 664 | //Get the path to this DLL so we can look for Eraser.exe |
|---|
| 665 | wchar_t fileName[MAX_PATH]; |
|---|
| 666 | DWORD fileNameLength = GetModuleFileName(theApp.m_hInstance, fileName, |
|---|
| 667 | sizeof(fileName) / sizeof(fileName[0])); |
|---|
| 668 | if (!fileNameLength || fileNameLength >= sizeof(fileName) / sizeof(fileName[0])) |
|---|
| 669 | throw LoadString(IDS_ERROR_CANNOTFINDERASER); |
|---|
| 670 | |
|---|
| 671 | //Trim to the last \, then append Eraser.exe |
|---|
| 672 | std::wstring eraserPath(fileName, fileNameLength); |
|---|
| 673 | std::wstring::size_type lastBackslash = eraserPath.rfind('\\'); |
|---|
| 674 | if (lastBackslash == std::wstring::npos) |
|---|
| 675 | throw LoadString(IDS_ERROR_CANNOTFINDERASER); |
|---|
| 676 | |
|---|
| 677 | eraserPath.erase(eraserPath.begin() + lastBackslash + 1, eraserPath.end()); |
|---|
| 678 | if (eraserPath.empty()) |
|---|
| 679 | throw LoadString(IDS_ERROR_CANNOTFINDERASER); |
|---|
| 680 | |
|---|
| 681 | eraserPath += L"Eraser.exe"; |
|---|
| 682 | |
|---|
| 683 | std::wostringstream finalCmdLine; |
|---|
| 684 | finalCmdLine << L"\"" << eraserPath << L"\" \"" << action << L"\" -q " |
|---|
| 685 | << parameters; |
|---|
| 686 | std::wstring cmdLine(finalCmdLine.str()); |
|---|
| 687 | std::vector<wchar_t> buffer(cmdLine.length() + 1); |
|---|
| 688 | wcscpy_s(&buffer.front(), cmdLine.length() + 1, cmdLine.c_str()); |
|---|
| 689 | |
|---|
| 690 | #if 0 |
|---|
| 691 | //If the process must be elevated we use ShellExecute with the runas verb |
|---|
| 692 | //to elevate the new process. |
|---|
| 693 | if (elevated) |
|---|
| 694 | { |
|---|
| 695 | int result = reinterpret_cast<int>(ShellExecute(parent, L"runas", |
|---|
| 696 | eraserPath.c_str(), &buffer.front(), NULL, show)); |
|---|
| 697 | if (result <= 32) |
|---|
| 698 | throw LoadString(IDS_ERROR_UNKNOWN); |
|---|
| 699 | } |
|---|
| 700 | |
|---|
| 701 | //If the process isn't to be elevated, we use CreateProcess so we can get |
|---|
| 702 | //read the output from the child process |
|---|
| 703 | else |
|---|
| 704 | #endif |
|---|
| 705 | { |
|---|
| 706 | //Create the process. |
|---|
| 707 | STARTUPINFO startupInfo; |
|---|
| 708 | ZeroMemory(&startupInfo, sizeof(startupInfo)); |
|---|
| 709 | startupInfo.cb = sizeof(startupInfo); |
|---|
| 710 | startupInfo.dwFlags = STARTF_USESHOWWINDOW; |
|---|
| 711 | startupInfo.wShowWindow = static_cast<WORD>(show); |
|---|
| 712 | startupInfo.hStdInput = startupInfo.hStdOutput = startupInfo.hStdError = |
|---|
| 713 | INVALID_HANDLE_VALUE; |
|---|
| 714 | |
|---|
| 715 | //Create handles for output redirection |
|---|
| 716 | Handle<HANDLE> readPipe; |
|---|
| 717 | HANDLE writePipe; |
|---|
| 718 | SECURITY_ATTRIBUTES security; |
|---|
| 719 | ZeroMemory(&security, sizeof(security)); |
|---|
| 720 | security.nLength = sizeof(security); |
|---|
| 721 | security.lpSecurityDescriptor = NULL; |
|---|
| 722 | security.bInheritHandle = true; |
|---|
| 723 | |
|---|
| 724 | if (CreatePipe(&static_cast<HANDLE&>(readPipe), &writePipe, &security, 0)) |
|---|
| 725 | { |
|---|
| 726 | startupInfo.dwFlags |= STARTF_USESTDHANDLES; |
|---|
| 727 | startupInfo.hStdOutput = startupInfo.hStdError = |
|---|
| 728 | writePipe; |
|---|
| 729 | } |
|---|
| 730 | |
|---|
| 731 | PROCESS_INFORMATION processInfo; |
|---|
| 732 | ZeroMemory(&processInfo, sizeof(processInfo)); |
|---|
| 733 | if (!CreateProcess(NULL, &buffer.front(), NULL, NULL, true, CREATE_NO_WINDOW, |
|---|
| 734 | NULL, NULL, &startupInfo, &processInfo)) |
|---|
| 735 | { |
|---|
| 736 | throw LoadString(IDS_ERROR_CANNOTFINDERASER); |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | //Wait for the process to finish. |
|---|
| 740 | Handle<HANDLE> hProcess(processInfo.hProcess), |
|---|
| 741 | hThread(processInfo.hThread); |
|---|
| 742 | CloseHandle(writePipe); |
|---|
| 743 | WaitForSingleObject(hProcess, static_cast<DWORD>(-1)); |
|---|
| 744 | DWORD exitCode = 0; |
|---|
| 745 | |
|---|
| 746 | if (GetExitCodeProcess(processInfo.hProcess, &exitCode) && exitCode) |
|---|
| 747 | { |
|---|
| 748 | char buffer[8192]; |
|---|
| 749 | DWORD lastRead = 0; |
|---|
| 750 | std::wstring output; |
|---|
| 751 | |
|---|
| 752 | while (ReadFile(readPipe, buffer, sizeof(buffer), &lastRead, NULL) && lastRead != 0) |
|---|
| 753 | { |
|---|
| 754 | size_t lastConvert = 0; |
|---|
| 755 | wchar_t wBuffer[8192]; |
|---|
| 756 | if (!mbstowcs_s(&lastConvert, wBuffer, sizeof(wBuffer) / sizeof(wBuffer[0]), |
|---|
| 757 | buffer, lastRead)) |
|---|
| 758 | { |
|---|
| 759 | output += std::wstring(wBuffer, lastConvert); |
|---|
| 760 | } |
|---|
| 761 | } |
|---|
| 762 | |
|---|
| 763 | //Show the error message. |
|---|
| 764 | throw output; |
|---|
| 765 | } |
|---|
| 766 | } |
|---|
| 767 | } |
|---|
| 768 | |
|---|
| 769 | MENUITEMINFO* CCtxMenu::GetSeparator() |
|---|
| 770 | { |
|---|
| 771 | MENUITEMINFO* mii = new MENUITEMINFO(); |
|---|
| 772 | mii->cbSize = sizeof(MENUITEMINFO); |
|---|
| 773 | mii->fMask = MIIM_TYPE; |
|---|
| 774 | mii->fType = MF_SEPARATOR; |
|---|
| 775 | return mii; |
|---|
| 776 | } |
|---|
| 777 | |
|---|
| 778 | HICON CCtxMenu::GetMenuIcon() |
|---|
| 779 | { |
|---|
| 780 | int smIconSize = GetSystemMetrics(SM_CXMENUCHECK); |
|---|
| 781 | return static_cast<HICON>(LoadImage(theApp.m_hInstance, L"Eraser", |
|---|
| 782 | IMAGE_ICON, smIconSize, smIconSize, LR_LOADTRANSPARENT)); |
|---|
| 783 | } |
|---|
| 784 | |
|---|
| 785 | HBITMAP CCtxMenu::GetMenuBitmap() |
|---|
| 786 | { |
|---|
| 787 | BITMAP bitmap; |
|---|
| 788 | ICONINFO iconInfo; |
|---|
| 789 | ZeroMemory(&bitmap, sizeof(bitmap)); |
|---|
| 790 | ZeroMemory(&iconInfo, sizeof(iconInfo)); |
|---|
| 791 | Handle<HICON> icon(GetMenuIcon()); |
|---|
| 792 | |
|---|
| 793 | //Try to get the icon's size, bitmap and bit depth. We will try to convert |
|---|
| 794 | //the bitmap into a DIB for display on Vista if it contains an alpha channel. |
|---|
| 795 | if (!GetIconInfo(icon, &iconInfo)) |
|---|
| 796 | return NULL; |
|---|
| 797 | |
|---|
| 798 | Handle<HBITMAP> iconMask(iconInfo.hbmMask); |
|---|
| 799 | if (!GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bitmap) || |
|---|
| 800 | bitmap.bmBitsPixel < 32) |
|---|
| 801 | return iconInfo.hbmColor; |
|---|
| 802 | |
|---|
| 803 | //Try converting the DDB into a DIB. |
|---|
| 804 | DIBSECTION dibSection; |
|---|
| 805 | HBITMAP dib = CreateDIB(bitmap.bmWidth, bitmap.bmHeight, NULL); |
|---|
| 806 | if (!GetObject(dib, sizeof(dibSection), &dibSection) || |
|---|
| 807 | !GetDIBits(CreateCompatibleDC(NULL), iconInfo.hbmColor, 0, bitmap.bmHeight, |
|---|
| 808 | dibSection.dsBm.bmBits, reinterpret_cast<BITMAPINFO*>(&dibSection.dsBmih), |
|---|
| 809 | DIB_RGB_COLORS)) |
|---|
| 810 | { |
|---|
| 811 | return iconInfo.hbmColor; |
|---|
| 812 | } |
|---|
| 813 | |
|---|
| 814 | return dib; |
|---|
| 815 | } |
|---|
| 816 | |
|---|
| 817 | HBITMAP CCtxMenu::CreateDIB(LONG width, LONG height, char** bitmapBits) |
|---|
| 818 | { |
|---|
| 819 | BITMAPINFO info; |
|---|
| 820 | ZeroMemory(&info, sizeof(info)); |
|---|
| 821 | info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
|---|
| 822 | info.bmiHeader.biWidth = width; |
|---|
| 823 | info.bmiHeader.biHeight = height; |
|---|
| 824 | info.bmiHeader.biPlanes = 1; |
|---|
| 825 | info.bmiHeader.biBitCount = 32; |
|---|
| 826 | info.bmiHeader.biCompression = BI_RGB; |
|---|
| 827 | char* dibBitmapBits = NULL; |
|---|
| 828 | return ::CreateDIBSection(0, &info, DIB_RGB_COLORS, |
|---|
| 829 | reinterpret_cast<void**>(bitmapBits ? bitmapBits : &dibBitmapBits), NULL, 0); |
|---|
| 830 | } |
|---|
| 831 | } |
|---|