5,696,038 members and growing! (11,636 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » ATL » General     Intermediate

ICopyHook implementation

By Mr.Prakash

Implementing ICopyHook Windows extension.
VC6, VC7, VC7.1, C++Windows, Win2K, WinXP, Win2003, ATL, VS6, Visual Studio, Dev

Posted: 4 Jun 2004
Updated: 4 Jun 2004
Views: 53,685
Bookmarked: 20 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
18 votes for this Article.
Popularity: 3.99 Rating: 3.18 out of 5
4 votes, 22.2%
1
0 votes, 0.0%
2
5 votes, 27.8%
3
7 votes, 38.9%
4
2 votes, 11.1%
5

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:

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

CopyCallback method in ICopyHookA is defined as:

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:

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.
    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:
    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_CURRENT_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 MSDN.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Mr.Prakash


He is just a simple guy!!!
Occupation: Software Developer (Senior)
Location: United States United States

Other popular ATL articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 39 (Total in Forum: 39) (Refresh)FirstPrevNext
GeneralErrormemberMsmVc21:54 26 Aug '08  
QuestionCopy hook handler - ICopyHook::CopyCallback not getting calledmemberKannanPerumal11:05 13 Dec '07  
Generaldoes not work!membersoptest11:04 17 May '06  
GeneralRe: does not work!memberMr.Prakash3:53 18 May '06  
GeneralRe: does not work!memberDavidCrow3:25 24 May '06  
Generalhelp !membernewnewman16:56 4 Apr '06  
GeneralCopyCallBack not getting calledmemberAnil_vvs20:32 7 Feb '06  
GeneralRe: CopyCallBack not getting calledmemberMr.Prakash20:36 7 Feb '06  
GeneralRe: CopyCallBack not getting calledmemberAnil_vvs20:52 7 Feb '06  
GeneralRe: CopyCallBack not getting calledmemberAnil_vvs20:53 7 Feb '06  
GeneralRe: CopyCallBack not getting calledmemberMr.Prakash21:00 7 Feb '06  
GeneralRe: CopyCallBack not getting calledmemberAnil_vvs21:16 7 Feb '06  
GeneralRe: CopyCallBack not getting calledmemberMr.Prakash22:26 7 Feb '06  
GeneralRe: CopyCallBack not getting calledmemberAnil_vvs20:45 8 Feb '06  
GeneralRe: CopyCallBack not getting calledmemberMr.Prakash20:49 8 Feb '06  
GeneralRe: CopyCallBack not getting calledmemberAnil_vvs20:52 8 Feb '06  
GeneralRe: CopyCallBack not getting calledmemberMr.Prakash21:01 8 Feb '06  
GeneralICopyHook for filesmemberAnil_vvs19:48 5 Feb '06  
GeneralICopyHook Implementationmemberjaneqshi18:54 5 Feb '06  
GeneralRe: ICopyHook ImplementationmemberMr.Prakash18:57 5 Feb '06  
GeneralICopyHook implementationsussJohn nhoJ21:32 20 Sep '05  
GeneralRe: ICopyHook implementationmemberMr.Prakash2:52 22 Sep '05  
GeneralRe: ICopyHook implementationmemberKarstenK0:46 16 Nov '05  
GeneralRe: ICopyHook implementationmemberMr.Prakash3:28 16 Nov '05  
GeneralCan I use this for files also ?memberCan Iyidogan1:18 29 Nov '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Jun 2004
Editor: Smitha Vijayan
Copyright 2004 by Mr.Prakash
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project