🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.borland-delphi
9 messages
9 total messages Started by 0BIT Sat, 26 Apr 2003 11:03
Mouse Hook
#1675
Author: 0BIT
Date: Sat, 26 Apr 2003 11:03
81 lines
3587 bytes
I've created a mouse hook dll, but when I activate it, it stops when
the mouse is moved out of the window (does windows automaticly unhook it ?)

How can I get it to keep running ?
Is the instance handle not correct ?
My source is based on the masm32 v8 ICZTUTES/TUTE24...

It also crashes when I use QContols for the Beep; procedure now I use
Beep(1000,2);
Why does it crash when using this unit ?


Greetings,
0BIT



library hookit;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes,
  Windows{,QControls};

{$R *.res}

var
  Instance : DWORD;
  recieve_window : HWND;
  MouseHook : HHOOK = 0;

function MouseProc(nCode : Longint; wp : WPARAM; lp : LPARAM) : DWORD;
stdcall;
begin
(*  if nCode<0 then begin*)
    {Result:=}CallNextHookEx(MouseHook,nCode,wp,lp);
(*  end else begin
  end;*)
  Beep(1000,2);
  Result:=0;
end;

function HookMouse(recieve_win : HWND) : Boolean; export;
begin
  if MouseHook=0 then begin
    recieve_window:=recieve_win;
    MouseHook:=SetWindowsHookEx(WH_MOUSE,@MouseProc,Instance,0);
    Result:=MouseHook<>0;
  end else
    Result:=False;
end;

function UnHookMouse : Boolean; export;
begin
  if MouseHook<>0 then begin
    if UnHookWindowsHookEx(MouseHook) then begin
      MouseHook:=0;
      Result:=True;
    end else
      Result:=False;
  end else
    Result:=false;
end;

exports
  HookMouse {name 'HookMouse'},
  UnHookMouse {name 'UnHookMouse'};

begin
  Instance:=GetModuleHandle(nil);
end.
Re: Mouse Hook
#1676
Author: 0BIT
Date: Sat, 26 Apr 2003 12:36
167 lines
5945 bytes
I've changed the source to use hInstance instead of getting it myself.
But the problem stays when the mouse is out of the window it doesn't update
?   : /
Why doesn't the MouseProc get called on all mouse changes ?

The updated source:

library hookit;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes,
  Windows{,QControls};

{$R *.res}

var
  recieve_window : HWND;
  MouseHook : HHOOK = 0;

  MouseInfo : MOUSEHOOKSTRUCT;

function MouseProc(nCode : Longint; wp : WPARAM; lp : LPARAM) : DWORD;
stdcall;
begin
  if nCode<0 then begin
    Result:=CallNextHookEx(MouseHook,nCode,wp,lp);
    Exit;
  end;
  MouseInfo:=MOUSEHOOKSTRUCT(Pointer(lp)^);
  PostMessage(recieve_window,$400{WM_USER}+100,wp,nCode);
{  CallNextHookEx(MouseHook,nCode,wp,lp);}
  Result:=0;
end;

function GetMouseInfo : MOUSEHOOKSTRUCT; export;
begin
  Result:=MouseInfo;
end;

function HookMouse(recieve_win : HWND) : Boolean; export;
begin
  if MouseHook=0 then begin
    recieve_window:=recieve_win;
    MouseHook:=SetWindowsHookEx(WH_MOUSE,@MouseProc,hInstance,0);
    Result:=MouseHook<>0;
  end else
    Result:=False;
end;

function UnHookMouse : Boolean; export;
begin
  if MouseHook<>0 then begin
    if UnHookWindowsHookEx(MouseHook) then begin
      MouseHook:=0;
      Result:=True;
    end else
      Result:=False;
  end else
    Result:=false;
end;

exports
  GetMouseInfo,
  HookMouse {name 'HookMouse'},
  UnHookMouse {name 'UnHookMouse'};

begin
end.


"0BIT" <someone@123.com> wrote in message
news:jItqa.216586$ZG5.19706348@amsnews02.chello.com...
> I've created a mouse hook dll, but when I activate it, it stops when
> the mouse is moved out of the window (does windows automaticly unhook it
?)
>
> How can I get it to keep running ?
> Is the instance handle not correct ?
> My source is based on the masm32 v8 ICZTUTES/TUTE24...
>
> It also crashes when I use QContols for the Beep; procedure now I use
> Beep(1000,2);
> Why does it crash when using this unit ?
>
>
> Greetings,
> 0BIT
>
>
>
> library hookit;
>
> { Important note about DLL memory management: ShareMem must be the
>   first unit in your library's USES clause AND your project's (select
>   Project-View Source) USES clause if your DLL exports any procedures or
>   functions that pass strings as parameters or function results. This
>   applies to all strings passed to and from your DLL--even those that
>   are nested in records and classes. ShareMem is the interface unit to
>   the BORLNDMM.DLL shared memory manager, which must be deployed along
>   with your DLL. To avoid using BORLNDMM.DLL, pass string information
>   using PChar or ShortString parameters. }
>
> uses
>   SysUtils,
>   Classes,
>   Windows{,QControls};
>
> {$R *.res}
>
> var
>   Instance : DWORD;
>   recieve_window : HWND;
>   MouseHook : HHOOK = 0;
>
> function MouseProc(nCode : Longint; wp : WPARAM; lp : LPARAM) : DWORD;
> stdcall;
> begin
> (*  if nCode<0 then begin*)
>     {Result:=}CallNextHookEx(MouseHook,nCode,wp,lp);
> (*  end else begin
>   end;*)
>   Beep(1000,2);
>   Result:=0;
> end;
>
> function HookMouse(recieve_win : HWND) : Boolean; export;
> begin
>   if MouseHook=0 then begin
>     recieve_window:=recieve_win;
>     MouseHook:=SetWindowsHookEx(WH_MOUSE,@MouseProc,Instance,0);
>     Result:=MouseHook<>0;
>   end else
>     Result:=False;
> end;
>
> function UnHookMouse : Boolean; export;
> begin
>   if MouseHook<>0 then begin
>     if UnHookWindowsHookEx(MouseHook) then begin
>       MouseHook:=0;
>       Result:=True;
>     end else
>       Result:=False;
>   end else
>     Result:=false;
> end;
>
> exports
>   HookMouse {name 'HookMouse'},
>   UnHookMouse {name 'UnHookMouse'};
>
> begin
>   Instance:=GetModuleHandle(nil);
> end.
>
>
Re: Mouse Hook
#1677
Author: KLinZ
Date: Sat, 26 Apr 2003 13:16
33 lines
2772 bytes
0BIT wrote:

> I've created a mouse hook dll, but when I activate it, it stops when
> the mouse is moved out of the window (does windows automaticly unhook
> it ?)

No, windows even hooks it for another process. See the note about shared
memory below.

> How can I get it to keep running ?
> Is the instance handle not correct ?
> My source is based on the masm32 v8 ICZTUTES/TUTE24...
>
> It also crashes when I use QContols for the Beep; procedure now I use
> Beep(1000,2);
> Why does it crash when using this unit ?

I experienced similar crashes when developing such an DLL.

But I solved them all :-) Have a look at
http://www.zenobits.com/projects.php/1#TIdleDetector

Hence the usage of shared mem (not the pascal unit! but the
CreateFileMapping/MapOfView thingie) in the DLL. It's needed because the DLL
get mapped into every process that receives messages. And the variables in
your DLL will be lost otherwise (not really, but you can't access them from
the current process).

About your crashes: I think it has something to do with the mouseproc code
not being re-entrant or something like that.

--
www.zenobits.com
Re: Mouse Hook
#1678
Author: 0BIT
Date: Sun, 27 Apr 2003 10:02
41 lines
2843 bytes
Thanks for your code,
0BIT

"KLinZ" <klinz@NOSPAMMERTSzenobits.com> wrote in message
news:hEvqa.3606$KF1.87288@amstwist00...
> 0BIT wrote:
>
> > I've created a mouse hook dll, but when I activate it, it stops when
> > the mouse is moved out of the window (does windows automaticly unhook
> > it ?)
>
> No, windows even hooks it for another process. See the note about shared
> memory below.
>
> > How can I get it to keep running ?
> > Is the instance handle not correct ?
> > My source is based on the masm32 v8 ICZTUTES/TUTE24...
> >
> > It also crashes when I use QContols for the Beep; procedure now I use
> > Beep(1000,2);
> > Why does it crash when using this unit ?
>
> I experienced similar crashes when developing such an DLL.
>
> But I solved them all :-) Have a look at
> http://www.zenobits.com/projects.php/1#TIdleDetector
>
> Hence the usage of shared mem (not the pascal unit! but the
> CreateFileMapping/MapOfView thingie) in the DLL. It's needed because the
DLL
> get mapped into every process that receives messages. And the variables in
> your DLL will be lost otherwise (not really, but you can't access them
from
> the current process).
>
> About your crashes: I think it has something to do with the mouseproc code
> not being re-entrant or something like that.
>
> --
> www.zenobits.com
>
Re: Mouse Hook
#1679
Author: 0BIT
Date: Sun, 27 Apr 2003 11:13
49 lines
3116 bytes
Your example code doesn't work if it gets started more than once....
It's because a dll only gets started only once (AFAIK).
So you can't hook twice...
Maybe you can solve this ?
At least this code doesn't crash... :-)

Happy coding,
0BIT



"KLinZ" <klinz@NOSPAMMERTSzenobits.com> wrote in message
news:hEvqa.3606$KF1.87288@amstwist00...
> 0BIT wrote:
>
> > I've created a mouse hook dll, but when I activate it, it stops when
> > the mouse is moved out of the window (does windows automaticly unhook
> > it ?)
>
> No, windows even hooks it for another process. See the note about shared
> memory below.
>
> > How can I get it to keep running ?
> > Is the instance handle not correct ?
> > My source is based on the masm32 v8 ICZTUTES/TUTE24...
> >
> > It also crashes when I use QContols for the Beep; procedure now I use
> > Beep(1000,2);
> > Why does it crash when using this unit ?
>
> I experienced similar crashes when developing such an DLL.
>
> But I solved them all :-) Have a look at
> http://www.zenobits.com/projects.php/1#TIdleDetector
>
> Hence the usage of shared mem (not the pascal unit! but the
> CreateFileMapping/MapOfView thingie) in the DLL. It's needed because the
DLL
> get mapped into every process that receives messages. And the variables in
> your DLL will be lost otherwise (not really, but you can't access them
from
> the current process).
>
> About your crashes: I think it has something to do with the mouseproc code
> not being re-entrant or something like that.
>
> --
> www.zenobits.com
>
Re: Mouse Hook
#1680
Author: KLinZ
Date: Sun, 27 Apr 2003 11:28
12 lines
1914 bytes
0BIT wrote:

> Your example code doesn't work if it gets started more than once....
> It's because a dll only gets started only once (AFAIK).
> So you can't hook twice...
> Maybe you can solve this ?
> At least this code doesn't crash... :-)

Well, don't start it twice than :-) Why would you like to start it twice? 

-- 
www.zenobits.com
Re: Mouse Hook
#1681
Author: Maynard Philbrook
Date: Sun, 27 Apr 2003 18:51
85 lines
3927 bytes
No, you must use a MappedFIle to store the Window Handle in ..
and retrive it from that mappedfile each time you want to send data to your
host window


0BIT wrote:

> I've created a mouse hook dll, but when I activate it, it stops when
> the mouse is moved out of the window (does windows automaticly unhook it ?)
>
> How can I get it to keep running ?
> Is the instance handle not correct ?
> My source is based on the masm32 v8 ICZTUTES/TUTE24...
>
> It also crashes when I use QContols for the Beep; procedure now I use
> Beep(1000,2);
> Why does it crash when using this unit ?
>
> Greetings,
> 0BIT
>
> library hookit;
>
> { Important note about DLL memory management: ShareMem must be the
>   first unit in your library's USES clause AND your project's (select
>   Project-View Source) USES clause if your DLL exports any procedures or
>   functions that pass strings as parameters or function results. This
>   applies to all strings passed to and from your DLL--even those that
>   are nested in records and classes. ShareMem is the interface unit to
>   the BORLNDMM.DLL shared memory manager, which must be deployed along
>   with your DLL. To avoid using BORLNDMM.DLL, pass string information
>   using PChar or ShortString parameters. }
>
> uses
>   SysUtils,
>   Classes,
>   Windows{,QControls};
>
> {$R *.res}
>
> var
>   Instance : DWORD;
>   recieve_window : HWND;
>   MouseHook : HHOOK = 0;
>
> function MouseProc(nCode : Longint; wp : WPARAM; lp : LPARAM) : DWORD;
> stdcall;
> begin
> (*  if nCode<0 then begin*)
>     {Result:=}CallNextHookEx(MouseHook,nCode,wp,lp);
> (*  end else begin
>   end;*)
>   Beep(1000,2);
>   Result:=0;
> end;
>
> function HookMouse(recieve_win : HWND) : Boolean; export;
> begin
>   if MouseHook=0 then begin
>     recieve_window:=recieve_win;
>     MouseHook:=SetWindowsHookEx(WH_MOUSE,@MouseProc,Instance,0);
>     Result:=MouseHook<>0;
>   end else
>     Result:=False;
> end;
>
> function UnHookMouse : Boolean; export;
> begin
>   if MouseHook<>0 then begin
>     if UnHookWindowsHookEx(MouseHook) then begin
>       MouseHook:=0;
>       Result:=True;
>     end else
>       Result:=False;
>   end else
>     Result:=false;
> end;
>
> exports
>   HookMouse {name 'HookMouse'},
>   UnHookMouse {name 'UnHookMouse'};
>
> begin
>   Instance:=GetModuleHandle(nil);
> end.
Re: Mouse Hook
#1682
Author: Maynard Philbrook
Date: Sun, 27 Apr 2003 18:52
52 lines
3390 bytes
Simple test for your mapped file and if it exist reject the loading of the DLL.


0BIT wrote:

> Your example code doesn't work if it gets started more than once....
> It's because a dll only gets started only once (AFAIK).
> So you can't hook twice...
> Maybe you can solve this ?
> At least this code doesn't crash... :-)
>
> Happy coding,
> 0BIT
>
> "KLinZ" <klinz@NOSPAMMERTSzenobits.com> wrote in message
> news:hEvqa.3606$KF1.87288@amstwist00...
> > 0BIT wrote:
> >
> > > I've created a mouse hook dll, but when I activate it, it stops when
> > > the mouse is moved out of the window (does windows automaticly unhook
> > > it ?)
> >
> > No, windows even hooks it for another process. See the note about shared
> > memory below.
> >
> > > How can I get it to keep running ?
> > > Is the instance handle not correct ?
> > > My source is based on the masm32 v8 ICZTUTES/TUTE24...
> > >
> > > It also crashes when I use QContols for the Beep; procedure now I use
> > > Beep(1000,2);
> > > Why does it crash when using this unit ?
> >
> > I experienced similar crashes when developing such an DLL.
> >
> > But I solved them all :-) Have a look at
> > http://www.zenobits.com/projects.php/1#TIdleDetector
> >
> > Hence the usage of shared mem (not the pascal unit! but the
> > CreateFileMapping/MapOfView thingie) in the DLL. It's needed because the
> DLL
> > get mapped into every process that receives messages. And the variables in
> > your DLL will be lost otherwise (not really, but you can't access them
> from
> > the current process).
> >
> > About your crashes: I think it has something to do with the mouseproc code
> > not being re-entrant or something like that.
> >
> > --
> > www.zenobits.com
> >
Re: Mouse Hook
#1683
Author: KLinZ
Date: Mon, 28 Apr 2003 18:36
20 lines
2313 bytes
Maynard Philbrook wrote:

> 0BIT wrote:
>
>> Your example code doesn't work if it gets started more than once....
>> It's because a dll only gets started only once (AFAIK).
>> So you can't hook twice...
>> Maybe you can solve this ?
>> At least this code doesn't crash... :-)
>>
> Simple test for your mapped file and if it exist reject the loading
> of the DLL.
>

.. and you have to send the messages (I use messages for communicating with
the main app) to the second app too. Maybe I fix it one day :-) Until then I
make sure there is only one instance of my application.

--
www.zenobits.com
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads