Click here to Skip to main content
15,885,365 members
Articles / Desktop Programming / ATL

ICopyHook implementation

Rate me:
Please Sign up or sign in to vote.
3.26/5 (21 votes)
18 May 2010CPOL2 min read 161K   2.1K   42   47
Implementing ICopyHook Windows extension

Introduction

Lately, I had to implement ICopyHook extension for my project. But, I could not get it working like other normal extensions. I tried searching for sample source code on the web/CodeProject with no success. So, I had no choice but to dig down to get it working. And success was not too far away.

Introduction to ICopyHook Interface

ICopyHook handler is a shell extension that determines if a folder or a printer can be moved/copied/renamed or deleted. It works with folder only and not with individual files. ICopyHook should only approve or deny the operation by returning the appropriate value.

ICopyHook interface has one method, CopyCallback, that we need to implement as per our liking. ICopyHook is really not the name of the interface, it is defined as follows:

C++
#ifdef UNICODE
#define ICopyHook ICopyHookW
#else
#define ICopyHook ICopyHookA
#endif

CopyCallback method in ICopyHookA is defined as:

C++
STDMETHOD_(UINT,CopyCallback) (THIS_ HWND hwnd, UINT wFunc, UINT wFlags, 
   LPCSTR pszSrcFile, DWORD dwSrcAttribs, 
   LPCSTR pszDestFile, DWORD dwDestAttribs) PURE;

and CopyCallback method in ICopyHookW is defined as:

C++
STDMETHOD_(UINT,CopyCallback) (THIS_ HWND hwnd, UINT wFunc, UINT wFlags, 
  LPCWSTR pszSrcFile, DWORD dwSrcAttribs, 
  LPCWSTR pszDestFile, DWORD dwDestAttribs) PURE;

So you see, you need to implement the right CopyCallback method for your type of compilation, i.e., for UNICODE or non-UNICODE.

Implementing ICopyHook

  1. Create an ATL DLL project. I have named it as CopyHook.
  2. Add a new ATL Object from the Insert menu.
  3. From the category, select Objects, and select Simple Object from the Objects list.
  4. Give a name (I have given it as MyHook). In the Properties, select Threading Model as 'Apartment', and interface as 'Dual'.
  5. Add ICopyHook in the list of derivations of the class.
    C++
    class ATL_NO_VTABLE CMyHook : public ComObjectRootEx<CComSingleThreadModel>,
      public CComCoClass<CMyHook, &CLSID_MyHook>,
      public ICopyHook,  // ICopyHook interface.
      public IDispatchImpl<IMyHook, &IID_IMyHook, &LIBID_COPYHOOKLib>
  6. Add the following in the COM Map:
    C++
    BEGIN_COM_MAP(CMyHook)
      COM_INTERFACE_ENTRY(IMyHook)
      COM_INTERFACE_ENTRY(IDispatch)
      COM_INTERFACE_ENTRY_IID(IID_IShellCopyHook , CMyHook)
    END_COM_MAP()
  7. Add the appropriate CopyCallBack method to the class and implement it. My implementation of the CopyCallBack is just to popup dialog.
  8. And of course, include shlobj.h.

Registration

These are the two places where we need to put our entries.

  1. HKEY_CLASSES_ROOT\Directory\ShellEx\CopyHookHandlers
  2. HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved

You can find these entries in MyHook.rgs file too in the sample project.

Note From the Author

  1. I have tested this under Win2K Prof and VC6, don't know about other operating systems but other Visual Studio versions should not be a problem.
  2. In the sample, I have implemented Unicode version of CopyCallBack, and ANSI version has been implemented but commented.
  3. For further help on ICopyHook, refer to MSDN.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
He is just a simple guy!!!

http://www.nadar.co.in

Comments and Discussions

 
SuggestionAlso need to restart explorer Pin
JonHarder13-Mar-13 9:47
JonHarder13-Mar-13 9:47 
QuestionType-o? Pin
David Crow7-May-10 10:07
David Crow7-May-10 10:07 
AnswerRe: Type-o? Pin
Prakash Nadar17-May-10 7:01
Prakash Nadar17-May-10 7:01 
AnswerMessage Closed Pin
13-Mar-13 9:38
JonHarder13-Mar-13 9:38 
QuestionRe: Type-o? Pin
David Crow13-Mar-13 9:43
David Crow13-Mar-13 9:43 
QuestionHow to hook individual file copy? Pin
Younger_XMU9-Dec-08 15:53
Younger_XMU9-Dec-08 15:53 
AnswerRe: How to hook individual file copy? Pin
Prakash Nadar9-Dec-08 15:56
Prakash Nadar9-Dec-08 15:56 
GeneralError Pin
MsmVc26-Aug-08 20:54
MsmVc26-Aug-08 20:54 
GeneralRe: Error Pin
jenus130-Jun-09 17:43
jenus130-Jun-09 17:43 
QuestionCopy hook handler - ICopyHook::CopyCallback not getting called Pin
Kanna13-Dec-07 10:05
Kanna13-Dec-07 10:05 
Generaldoes not work! Pin
soptest17-May-06 10:04
soptest17-May-06 10:04 
GeneralRe: does not work! Pin
Prakash Nadar18-May-06 2:53
Prakash Nadar18-May-06 2:53 
GeneralRe: does not work! Pin
David Crow24-May-06 2:25
David Crow24-May-06 2:25 
Generalhelp ! Pin
newnewman4-Apr-06 15:56
newnewman4-Apr-06 15:56 
GeneralCopyCallBack not getting called Pin
Anil_vvs7-Feb-06 19:32
Anil_vvs7-Feb-06 19:32 
GeneralRe: CopyCallBack not getting called Pin
Prakash Nadar7-Feb-06 19:36
Prakash Nadar7-Feb-06 19:36 
GeneralRe: CopyCallBack not getting called Pin
Anil_vvs7-Feb-06 19:52
Anil_vvs7-Feb-06 19:52 
GeneralRe: CopyCallBack not getting called Pin
Anil_vvs7-Feb-06 19:53
Anil_vvs7-Feb-06 19:53 
GeneralRe: CopyCallBack not getting called Pin
Prakash Nadar7-Feb-06 20:00
Prakash Nadar7-Feb-06 20:00 
GeneralRe: CopyCallBack not getting called Pin
Anil_vvs7-Feb-06 20:16
Anil_vvs7-Feb-06 20:16 
GeneralRe: CopyCallBack not getting called Pin
Prakash Nadar7-Feb-06 21:26
Prakash Nadar7-Feb-06 21:26 
GeneralRe: CopyCallBack not getting called Pin
Anil_vvs8-Feb-06 19:45
Anil_vvs8-Feb-06 19:45 
GeneralRe: CopyCallBack not getting called Pin
Prakash Nadar8-Feb-06 19:49
Prakash Nadar8-Feb-06 19:49 
GeneralRe: CopyCallBack not getting called Pin
Anil_vvs8-Feb-06 19:52
Anil_vvs8-Feb-06 19:52 
GeneralRe: CopyCallBack not getting called Pin
Prakash Nadar8-Feb-06 20:01
Prakash Nadar8-Feb-06 20:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.