4

I can't be clearer than my title. :P

I want to run my program whenever a user renames a file in Windows Explorer (and only within the Explorer). Here's a simple mock up:

Rename Hook Mockup

A simple link to a tutorial will be very helpful. I couldn't find anything. :/

Thank you in advance.

P.S. I'm new in C++

pek
  • 17,847
  • 28
  • 86
  • 99
  • 1
    Why only explorer? Would you count a rename operation in the common open/save dialogs? – Anders Feb 05 '11 at 22:57
  • I thought that the open/save dialogs were part of Explorer. If this is true, then that is OK. Otherwise, no. – pek Feb 05 '11 at 22:59
  • What is the goal? Why do you need this? There may be a better solution if we know the actual problem instead of your attempted solution. – Fred Nurk Feb 05 '11 at 23:41
  • This may sound silly, but I'd rather not disclose more details. After all, I don't believe it matters. I really need to do this through an explorer hook. – pek Feb 05 '11 at 23:58
  • 1
    @pek: That's fine, just understand that it's *really* hard to answer a hidden question. – Fred Nurk Feb 06 '11 at 10:51
  • I really don't see the need for more details. All I ask is how to hook a custom code on Explorer's rename event. – pek Feb 06 '11 at 16:04
  • 1
    @pek Open/save dialogs run in-process and not in Explorer. Also you attitude is not conducive to receiving help. Since you already appear to know the exactly solution that you will implement, what's stopping you doing it? – David Heffernan Feb 07 '11 at 11:44
  • I'm really sorry if I gave this impression (reading my replies I can understand why). I haven't found the solution. As for what's stopping me from doing it, I haven't found the solution. It's just that I had done some research before asking and the answers people give me are the same as the ones I found; and they are not what I want (I even explain to them why). And since Open/save dialogs are in-process, then no, I don't want to hook it on them. – pek Feb 07 '11 at 20:04
  • Man, my replies really don't look polite... :P Honestly, it's probably because I haven't been writing a lot.. ;) – pek Feb 07 '11 at 20:16
  • Do you really mean a *file* specifically ? The Shell (which you seem to call Explorer) can show many things besides drives, folders, and files. – MSalters Feb 08 '11 at 14:19
  • I added a mock up that I believe will help clarify things. – pek Feb 08 '11 at 17:00
  • 2
    "I don't see the need for more details" very strongly says to us "I don't want to tell you because it's nefarious/I think you'll steal my idea." Either way, we're less inclined to help you. Stack Overflow works best when you're open about the problem you're facing, especially when the actual solution may be only tangentially related to the solution you're pursuing. – Jonathan Grynspan Feb 08 '11 at 17:07
  • 1
    I'd like to echo what Jonathan Grynspan and Fred Nurk said. As well, I have a question: Do you want to know if a file is renamed before the change is committed to disk or is afterwards OK? – JimR Feb 08 '11 at 17:32

2 Answers2

3

It looks like Windows API hooking may be your best bet. You'll want to intercept all calls related to Windows file renaming (i.e. MoveFile, MoveFileEx, SHFileOperation, possibly more). There are a few commercial and open source solutions; Microsoft Detours, Madshi's madCodeHook, and the free, open source EasyHook.

This approach, when done correctly, will allow you to capture all file renaming on a system.

Joe Jordan
  • 2,372
  • 2
  • 17
  • 20
2

I would avoid hooking APIs as much as possible. It gets really ugly really fast.

There are 2 ways I see that you can approach this.
Both ways have a few common factors:

  • The ReadDirectoryChangesW API. For a very good implementation of that API, see this article
  • You will need to minimize your dependencies, so... Use a Microsoft compiler, link to the DLL runtime, stick to C as much as possible etc. This reduces problems. Loading things into the shell memory space is already problematic enough.

Method one is to use ReadDirectoryChangesW from an Explorer shell extension that does nothing else. Keep it minimal. I'm reasonably sure I saw a "do nothing" shell extension as an example in some of Microsoft's documentation.

Method two would be to package your code as a DLL and use a system hook to get your DLL loaded into Explorer only. The system hook should only load inside Explorer to prevent spurious notifications via ReadDirectoryChangesW.

Hope this helps and that you're not using it for something Evil.

JimR
  • 15,513
  • 2
  • 20
  • 26
  • Hehehe... Never crossed my mind to do harm with rename hooks... ;) Thanks for the great answer! – pek Feb 11 '11 at 16:33