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 926527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:41:54+00:00 2026-05-15T19:41:54+00:00

I have successfully gotten my low-level mouse hook code to work, but there are

  • 0

I have successfully gotten my low-level mouse hook code to work, but there are some behaviors I’m observing that I don’t quite understand. Everything works fine if I spawn a thread which installs the mouse hook. When i no longer need it running, I let the thread get destroyed automatically as I exit the program, and I imagine I can easily terminate the thread explicitly. However this makes me uneasy because I won’t end up calling UnhookWindowsHookEx to free the mouse hook resources.

So I tried to reverse the logic in my testing program. I try spawning a thread which sleeps for a bit, and then writes to a global variable. Then I call the hook installing routine from the primary thread. In here is a loop which checks the global variable and exits the loop when appropriate. Then it will be able to clean up after itself. Here is the relevant code:

static int willQuit = 0;
unsigned WINAPI MouseProcessingProc (void *param) {
 try { // will catch exceptions and not propagate them
  HHOOK mousehook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc,
    NULL, 0);
  if (mousehook == NULL) printf("Mousehook error %u\n",GetLastError());

  while(true) {
   MSG msg;
   if (GetMessage(&msg,0,0,0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
   }
   if (willQuit) { printf("willQuit no longer 0!\n"); break; }
   printf("Looped\n");
  }
  willQuit = 0;
  UnhookWindowsHookEx(mousehook);

  printf("Procedure completed without exceptional events.\n");
 } catch (const std::string& s) {
  printf("String exception: %s\n", s.c_str());
 } catch (int i) {
  printf("Int exception: %d\n", i);
 } catch (...) {
  printf("Unknown default exception!\n");
 }
 printf("Exiting thread execution.\n");
 return 0;
}

void spawn() {
 printf("Spawning Mouse thread\n");
 _beginthreadex(NULL,0,MouseProcessingThreadProc,NULL,0,0);
}

void run() {
 printf("Running Mouse routine\n");
 MouseProcessingThreadProc(0);
}

void stop() {
 printf("Stopping Mouse routine\n");
 willQuit = 1;
}

unsigned WINAPI threadproc(void *param) {
    Sleep(500);
    printf("Spawned thread says 3");
    Sleep(500);
    printf("2");
    Sleep(500);
    printf("1\n");
    Sleep(500);
    printf("Spawned thread calls stop now -->\n");
    stop();
}

int main() {
    _beginthreadex(NULL,0,threadproc,NULL,0,0); // waiter and stopper thread
    run(); // become the mousehook thread
    printf("Completed\n");
    return 0;
}

Now what’s happening is that the little message polling loop I have (the while loop) never really returns from the GetMessage call, so it can never reach the point where it checks willQuit. I have verified this with those printf’s and gdb. Why does GetMessage not return? Is there a better way to set up my mouse hook thread? Or am I trying to do something that I am not supposed to do?

thanks for reading.

  • 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-05-15T19:41:54+00:00Added an answer on May 15, 2026 at 7:41 pm

    In your stop() routine, after setting the willQuit variable, you will also need to POST any message to the thread running MouseProcessingProc, so GetMessage will return. See PostThreadMessage API.

    EDIT: You could also use an event, or other sync object, instead of using windows messages and the message pump. So the message pump would get replace by something like WaitForSingleObject, some kernel object that you can signal, such as an event.

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

Sidebar

Ask A Question

Stats

  • Questions 483k
  • Answers 483k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are the referenced aspnet_client folders the same or are you… May 16, 2026 at 7:02 am
  • Editorial Team
    Editorial Team added an answer simple example http://www.developer.com/java/data/article.php/3335001/BASIC-and-FORM-based-Authorization-in-Your-Web-Application.htm Apache Documentation http://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html#Standard_Realm_Implementations May 16, 2026 at 7:02 am
  • Editorial Team
    Editorial Team added an answer You can use LINQ: Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().Select(v => new SelectListItem { Text… May 16, 2026 at 7:02 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.