I'm trying to hook an application to prevent it from shutting down when the user presses the close button on the form. Instead i want to run my own code wich just closes that specific window and not the whole application.
The application loads the dll and i add a hook with .
hMsgHook = SetWindowsHookEx(
And the hook proc looks like
LRESULT CALLBACK MessageProc(int code, WPARAM wParam, LPARAM lParam)
MSG *msg = (MSG*)lParam;
//if this is a command message we should look into it.
if (msg->message == WM_COMMAND)
//Code to process commands that works well.
if (msg->message == WM_SYSCOMMAND || msg->message == WM_CLOSE)
if (msg->wParam == SC_CLOSE)
int i = 1; //Breakpoint here;
When i use the right klick menu or window menu to close the form the HookProc get's called but when i press the x it doesn't.
I've tried a lot of different hook types and have found that WH_CALLWNDPROC get's the x but not the menus. The problem with WH_CALLWNDPROC is that modifying the message to WM_NULL as suggested in another thread doesn't stop the application from shutting down. The docuementation also describes that you cannot modify the message in a callback of this type.
I'm running on xp and is using VS2003 to make this.
I think the forms i'm trying to prevent from closing is a child form, could this have anything to do with it. I've tried WH_MSGFILTER without success.
If anyone can tell me how to provide more info i'll do so.
I'm stuck and don't know what to do next.