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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:44:01+00:00 2026-05-28T06:44:01+00:00

I downloaded a mouse hook sample, which didn’t work. So I stripped all the

  • 0

I downloaded a mouse hook sample, which didn’t work. So I stripped all the unnecessary stuff and would like to know what is wrong in it. When I start the program, both it and Delphi freeze and I have to close it through taskmgr.

App:

type
...
    procedure ms(var message: tmessage); message WM_USER+1234;

  end;

var
  MainHookTestForm: TMainHookTestForm;

implementation
procedure HookMouse; stdcall; external 'MouseHook.DLL'; // Added stdcalls;
procedure UnHookMouse; stdcall; external 'MouseHook.DLL';

{$R *.dfm}    

procedure TMainHookTestForm.FormCreate(Sender: TObject);
begin
  HookMouse;
end;

procedure TMainHookTestForm.FormDestroy(Sender: TObject);
begin
  UnHookMouse;
end;

procedure TMainHookTestForm.ms(var message: tmessage);
begin
  Label1.Caption:=format('%d - %d',[message.LParam, message.WParam]); // Edited
end;

Lib:

library MouseHook;
uses
  Forms,
  Windows,
  Messages;

var Hook: HHOOK;
{$R *.res}

function HookProc(nCode: Integer; MsgID: WParam; Data: LParam): LResult; stdcall;
var
  mousePoint: TPoint;
begin
  mousePoint := PMouseHookStruct(Data)^.pt;
  PostMessage(FindWindow('TMainHookTestForm', 'Main'), WM_USER+1234, mousePoint.X, mousePoint.Y); // Edited class name
  Result := CallNextHookEx(Hook,nCode,MsgID,Data);
end;

procedure HookMouse; stdcall;
begin
  if Hook = 0 then Hook:=SetWindowsHookEx(WH_MOUSE,@HookProc,HInstance,0);
end;

procedure UnHookMouse; stdcall;
begin
  UnhookWindowsHookEx(Hook);
  Hook:=0;
end;

exports
  HookMouse, UnHookMouse;

begin
end.

I think this is as simple as it gets. The freeze happens at the call of HookMouse, when this line is executed, the whole IDE freezes and I can’t debug further. But I can’t see anything wrong in that procedure.

I am using XE2, if that helps. Thanks for the troubleshooting


Edit: I edited the calls of HookMouse and UnhookMouse with stdcall; and the name of the window class to find. It seems to be working nice now, it shows correct values, BUT only if the mouse cursor is not in the app window – when I move the mouse to the window, it changes to the HourGlass and the Label caption stops updating. What could cause this?

  • 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-28T06:44:01+00:00Added an answer on May 28, 2026 at 6:44 am

    You have a few problems with your code

    1.You are missing the stdcall calling convention:

    procedure HookMouse; stdcall; external 'MouseHook.DLL';
    procedure UnHookMouse; stdcall; external 'MouseHook.DLL';
    

    2.You are changing the caption in the main form. so FindWindow (in the DLL) will find the window only once. you can use TMemo to debug:

    procedure TMainHookTestForm.ms(var message: tmessage);
    begin
      Memo1.Lines.Add(format('%d - %d',[message.LParam, message.WParam]));
    end;
    

    2.1.The class name MainHookTestForm is not correct. should be:

    FindWindow('TMainHookTestForm', 'Main')
    

    Note the T MainHookTestForm

    3.in the HookProc you must use:

    if nCode = HC_ACTION then
    begin
      mousePoint := PMouseHookStruct(Data)^.pt;      
      PostMessage(FindWindow('TMainHookTestForm', 'Main'), WM_USER+1234, mousePoint.X, mousePoint.Y);
    end;  
    
    Result := CallNextHookEx(Hook,nCode,MsgID,Data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know about the GetLastInputInfo method but that would only give me the duration
I have connected 2 mice to PC and I wish one mouse to work
I'm running an emulator targeting 2.3 (API Level 9). I've removed all downloaded apps
Downloaded IOS5 SDK yesterday, and this code which I use to set my UIToolbar's
I downloaded the sample ASP.NET login page from here: https://wiki.jasig.org/display/CASC/ASP.NET+Forms+Authentication And got CAS working
I downloaded the latest version of Twitterizer and I have the below code which
I downloaded and installed the Aptana Studio free version. But apparently, to install the
I downloaded Hex Workshop, and I was told to read a .dbc file. It
I downloaded Reflector.Net and it's an .Exe outside VS. I remember (last year) when
Having downloaded Pango and GLib from the GTK+ Project's Win32 downloads page and having

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.