[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

WH_GETMESSAGE hook doesn't reveive SC_CLOSE on shutdown button only on menu.

Posted on 2005-05-12
4
Medium Priority
?
1,106 Views
Last Modified: 2008-01-09
Hi!
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(
        WH_GETMESSAGE,
        (HOOKPROC)MessageProc,
        (HINSTANCE)ThisDllHandle,
        GetCurrentThreadId());

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.

Fredrik Högberg
   
0
Comment
Question by:fhogberg
  • 2
4 Comments
 

Author Comment

by:fhogberg
ID: 13993532
When looking at the messages in spy++ using the button causes two entries "Sent" and "Return" using the button makes a single entry "Posted". Don't know the difference.

Tried subclassing and this seams to work better as i receive all messages with that.

Still don't understand what was wrong with the hook though.

Fredrik Högberg
0
 
LVL 16

Accepted Solution

by:
nonubik earned 1000 total points
ID: 13993716
When you set the hook, one of the last 2 params *must* be 0.
I.e. you want a system-wide hook, you call:

     SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)MessageProc, (HINSTANCE)ThisDllHandle,  0);

and if you want a hook on a thread created by the current process, you call:

    SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)MessageProc, NULL,  GetCurrentThreadId());

0
 
LVL 1

Assisted Solution

by:a_dube
a_dube earned 1000 total points
ID: 14216333
From your hook proc:

if (msg->message == WM_SYSCOMMAND || msg->message == WM_CLOSE)
     {
          if (msg->wParam == SC_CLOSE)
          {
               int i = 1; //Breakpoint here;
          }
     }

If msg->message == WM_CLOSE, msg->wParam would be != SC_CLOSE and you'll never stop at your breakpoint.
0
 

Author Comment

by:fhogberg
ID: 14218498
Still doesn't solved but I managed to get it working with subclassing.
Since I don't have time to test your solutions you get a 50/50 split in points.
Hope you think this is fair.

Thank you for helping.


Fredrik Högberg
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question