How to catch copy paste from explorer using shell extension? ICopyHook alternative for files and folder
How to catch copy paste from explorer using shell extension? ICopyHook alternative for files and folder
Howto trap copy paste event from explorer to do your custom processing or even
Howto replace/substitute the copy-paste/cut-paste operation with your own copy or move or anything else?
Yes, tracking copy paste operation is possible using DragDropHandlers i.e. just by implementing IShellExtInit & IContextMenu:
Technique in brief with code snippet:
- Use IShellExtInit::Initialize to save the source files and destination folder – you get this data from LPCITEMIDLIST and LPDATAOBJECT in parameter to method.
- Most important part, implement IContextMenu::QueryContextMenu to substitute default menu item for copy paste or cut paste event from explorer
- In IContextMenu::InvokeCommand write your implementation for copy-paste/cut-paste event from explorer.
The code snippet below shows how to get source file names, destination folder name involved in copy paste operation (see TrapCopy::Initialize method below), how to direct the default copy paste event to your custom one (see TrapCopy::QueryContextMenu and TrapCopy::InvokeCommand
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | STDMETHODIMP TrapCopy::Initialize(LPCITEMIDLIST pidlFolder, LPDATAOBJECT pDO, HKEY hProgID) { // Look for CF_HDROP data in the data object. // If there is no such data, return an error back to Explorer. FORMATETC fmt = {CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL}; STGMEDIUM stg = {TYMED_HGLOBAL}; if (FAILED(pDO->GetData(&fmt,&stg))) return E_INVALIDARG; // Get a pointer to the actual data. HDROP hDrop = (HDROP)GlobalLock(stg.hGlobal); if (!hDrop) return E_INVALIDARG; UINT numFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0); if(numFiles) { WCHAR fileName[MAX_PATH] = L""; for(UINT i=0; i<numFiles; ++i) { if(DragQueryFile(hDrop, i, fileName, MAX_PATH)) { // add file names to filename list to moved/copied m_srcFileNames.Add(fileName); } } } GlobalUnlock(stg.hGlobal); ReleaseStgMedium(&stg); // get destination folder: if (!SHGetPathFromIDList(pidlFolder, m_destFolder)) return E_INVALIDARG; } // Code snippet for IContextMenu::QueryContextMenu STDMETHODIMP TrapCopy::QueryContextMenu(HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd, UINT uidLastCmd, UINT uFlags) { // If the flags include CMF_DEFAULTONLY or if pipe client // is not connected don't do anything. if (uFlags & CMF_DEFAULTONLY) return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0); int cmd = uidFirstCmd; InsertMenu(hmenu, uMenuIndex++, MF_STRING|MF_BYPOSITION, cmd ++, L"Custom copy"); InsertMenu(hmenu, uMenuIndex++, MF_STRING|MF_BYPOSITION, cmd ++, "Custom paste"); // set the copy/paste default handler to the menu item create above int defItem = GetMenuDefaultItem(hmenu, false, 0); if (defItem == 1) // 1: Copy { SetMenuDefaultItem(hmenu, uidFirstCmd + defItem - 1, false); } else if (defItem == 2) // 2: Move { SetMenuDefaultItem(hmenu, uidFirstCmd + defItem - 1, false); } // Return 2 to tell the shell that we added 2 top-level menu items. return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 2); } STDMETHODIMP TrapCopy::InvokeCommand ( LPCMINVOKECOMMANDINFO pInfo ) { if(HIWORD(pInfo->lpVerb)) return E_INVALIDARG; // custom implementaion for copy/move for source files m_srcFileNames // and destination folder m_destFolder, saved in TrapCopy::Initialize switch(LOWORD(pInfo->lpVerb)) { case 0: InvokeCustomCopy() break; case 1: InvokeCustomeMove() break; } return S_OK; } |
Recent Entries
- Clicking on Radio button hangs for MFC based dialog Application
- zFeeder – Making PHP 5.3 + compatible, solving ‘Function ereg_replace() is deprecated’
- How to multilingualize or localize URL in Gallery2 – enhancement of multilang module to create multilingual URLs, Multi-language URL for SEO with Multi Language Module
- Fix for mergelog, fails with error “abort due to a problem” for dummy records of apache access logs
- Shepherds life in mountain
- The modern marriage culture among mate
- Do you know that barter system still exists in most villages in uttarakhand, may be true for many other parts of India also
- How to catch copy paste from explorer using shell extension? ICopyHook alternative for files and folder
- How to convert ASCII/UNICODE string to hex string? Function/API to convert to hex string
- How to solve ‘ReadFile hangs’ for named pipe client/server – sample working code for named pipe