Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:41:49+00:00 2026-06-16T00:41:49+00:00

I wanted to ‘protect’ a certain window from closing. So I wanted to hook

  • 0

I wanted to ‘protect’ a certain window from closing. So I wanted to hook WM_CLOSE, WM_DESTOY or WM_QUIT. And this is what I tried to do so:

LRESULT CALLBACK WindowHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode >= 0)
    {
        MSG* msg = (MSG*)lParam;
        //if (msg->hwnd == GetForegroundWindow())
        {
            if (msg->message == WM_CLOSE || msg->message == WM_QUIT || msg->message == WM_DESTROY)
            {
                //MessageBox(0, "TEST", "", 0);
                msg->message = 0;
                CallNextHookEx(hMsg, nCode, (WPARAM)NULL, (LPARAM)NULL);
                return TRUE;
            }
        }
    }

    CallNextHookEx(hMsg, nCode, wParam, lParam);
}

I tried:

  • Returning TRUE or FALSE
  • Calling CallNextHookEx with NULL as LPARAM or
    editing the msg->message to 0

Also what I noticed, if I hook WH_GETMESSAGE it doesn’t ‘block’ the message loop. But it does so with hooking WH_CALLWNDPROC. I discovered this with prompting a messagebox when the msg->message equals to WM_CLOSE.

Thanks in advance,
Regards.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:41:50+00:00Added an answer on June 16, 2026 at 12:41 am

    WH_GETMESSAGE installs a hook for messages that are retrieved from the message queue. It does allow you to modify the message. But the problem is that WM_CLOSE is not posted to the message queue with PostMessage(), it is sent with SendMessage(). This hook can’t see it.

    WH_CALLWNDPROC installs a hook that runs just before the window procedure is called. Which will allow you to see WM_CLOSE. However, you are not allowed to modify the message or make it disappear.

    There is no hook that allows you to do what you want to do. Instead you have to sub-class the window procedure. So that your own custom window procedure is called before the window’s own procedure. Now you can simply filter WM_CLOSE by just not calling the old window procedure.

    Beware that you still need the same kind of plumbing that SetWindowsHookEx() provides, you still need to inject a DLL with your window procedure into the process. Best way is to still use SetWindowsHookEx to get that done with a dummy hook that doesn’t otherwise do anything.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wanted to know if I could do this without the sub-query SELECT * FROM
I wanted to get ideas from the SO community about this issue. Here is
Wanted to know if there was a way one could query shelveset details from
just wanted to ask where I define initial class properties? From other languages I
Wanted to create a fun app with photo. This is the scenario: I have
I wanted to know is it possible to find a which file from several
I wanted to include the facebook login inside my application. This is the sample
I wanted to bring this challenge to the attention of the stackoverflow community. The
Resharper wanted me to change this code: foreach (var item in cmbxColor1.Items) { cmbxColor2.Items.Add(item);
I wanted to see your input on this concern I'm currently experiencing. It turns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.