How can I override the close button on a third-party application so that it minimizes instead?

Tamara Wijsman picture Tamara Wijsman · Oct 16, 2010 · Viewed 8k times · Source

I would like to override the close button on a third-party application so that it causes the application to be minimized instead. I do not have source code for the target application.

  • Can I write such thing in C#? Or do I need to use C++?
  • How do I write this kind of hook? Do I need a process running or would a driver/dll/service suffice?

As far as I got researching I think I have to do something like this but I don't know how exactly:

A WH_GETMESSAGE hook to override WM_CLOSE to set the Windows status to WS_MINIMIZE.

Answer

Achintya Sharma picture Achintya Sharma · Oct 16, 2010

You can do it in both C++ and C#. To do this, you would have to hook into the applications message loop and override the WM_CLOSE message to WM_MINIMIZE. To hook into any process that's running you can use:

  1. Microsoft Detours (Commercial and not free if I remember correctly) (http://research.microsoft.com/en-us/projects/detours/)

  2. EasyHook (Open source under LGPL) (http://easyhook.codeplex.com/)

I've used EasyHook and I was very satisfied with the results. It gives you really nice features like starting up a process with the hooks attached OR attaching hooks to already running processes. Also, it provides you with both managed(C#) and native hooking libraries. I'd recommend you take a look at it...