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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:20:22+00:00 2026-05-28T08:20:22+00:00

#include <windows.h> #include <process.h> HWND MainHwnd; HHOOK MouseHook; LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM

  • 0
#include <windows.h>
#include <process.h>

HWND MainHwnd;
HHOOK MouseHook;
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
    static wchar_t szAppName[]=L"hooks";
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wndclass;

    wndclass.cbSize=sizeof(wndclass);
    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    wndclass.lpfnWndProc=WndProc;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hInstance=hInstance;
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName=NULL;
    wndclass.lpszClassName=szAppName;
    wndclass.hIconSm=LoadIcon(NULL,IDI_APPLICATION);

    RegisterClassEx(&wndclass);

    MainHwnd=hwnd=CreateWindow(szAppName,L"hooks",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
        CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
    ShowWindow(hwnd,SW_MAXIMIZE);
    UpdateWindow(hwnd);

    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
LRESULT CALLBACK LowLevelMouseProc(int nCode,WPARAM wParam,LPARAM lParam) 
{
    if (nCode==HC_ACTION) 
        ((LPMSLLHOOKSTRUCT)lParam)->flags=0;
  return CallNextHookEx(NULL,nCode,wParam,lParam);
}
 void thread(void *param)
 {
    for (int i=0;i<3;i++)
    {
        UnhookWindowsHookEx(MouseHook);
        MouseHook=SetWindowsHookEx(WH_MOUSE_LL,reinterpret_cast<HOOKPROC>(LowLevelMouseProc),(HINSTANCE)GetWindowLong(MainHwnd,GWL_HINSTANCE),NULL);
        for (int j=0;j<100;j++)
        {
                mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE, (j)*(65535.0/500),(j)*(65535.0/500),0,0);
                Sleep(10);
        }
        Sleep(2000);
    }
 }
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    switch(iMsg)
    {
    case WM_CREATE:
        {
            MouseHook=SetWindowsHookEx(WH_MOUSE_LL,reinterpret_cast<HOOKPROC>(LowLevelMouseProc),(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
            _beginthread(thread,0,0);
            return 0;
        }
    case WM_PAINT:
        {
            hdc=BeginPaint(hwnd,&ps);
            EndPaint(hwnd,&ps);
            return 0;
        }
    case WM_DESTROY:
        UnhookWindowsHookEx(MouseHook);
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd,iMsg,wParam,lParam);
}

I create thread with _beginthread. This thread simulate mouse’s moves and call Sleep(2000). I can’t move mouse after simulating moves in thread. But if I comment this lines in void thread(void *param)

UnhookWindowsHookEx(MouseHook);
MouseHook=SetWindowsHookEx(WH_MOUSE_LL,reinterpret_cast<HOOKPROC>(LowLevelMouseProc),(HINSTANCE)GetWindowLong(MainHwnd,GWL_HINSTANCE),NULL);`

program will work right, without mouse ‘blocking’. Can everybody explain reason for it.

  • 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-28T08:20:23+00:00Added an answer on May 28, 2026 at 8:20 am

    A thread that calls SetWindowsHookEx() to set a low-level mouse hook must pump a message loop. Required so that Windows can call the callback you registered so it can notify you of the mouse message. Which is your problem, the thread you started sets a hook but doesn’t pump a message loop.

    Windows has protection against misbehaving programs like this, it automatically destroys the hook when it has to wait too long to make the callback. But your thread sleeps for 2 seconds, not long enough to trigger the timeout. You then call UnhookWindowsHookEx() which unblocks Windows. But immediately hook again.

    There’s just no point in calling SetWindowsHookEx() in that thread. Just remove it and the hook that you have set in the main thread will run as normal. Hard to see the point of that thread in general btw.

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

Sidebar

Related Questions

I was under the impression that this code #include <windows.h> #include <stdio.h> int WINAPI
#include <iostream> #include <iomanip> #include <stdio.h> #include <windows.h> #include <process.h> using namespace std; HANDLE
This may be the simplest win32 program ever .. #include <windows.h> int WINAPI WinMain(HINSTANCE
Is there a lightweight process execution timer like Unix's time included with Windows? Sometimes
I am experimenting with Beep function on Windows: #include <windows.h> ... Beep(frequency, duration); The
When trying to compile a file that include winnt.h via windows.h, I get the
suppose you have the following structure: #include <windows.h> // BOOL is here. #include <stdio.h>
i want to do this simple piece of code work. #include <iostream> #include <windows.h>
I'm using System.Diagnostics.Process to execute an svn command from a windows console application. This
I wrote this code and compiled it, #include <windows.h> #include <stdio.h> #include <stdlib.h> #include

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.