| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Hooks 1/3 |
JS> Can anyone post a working example, of how to set a hook ?
Area IBM.Os2pro, Msg#299, Jan-08-95 04:40:30
From: Peter Fitzsimmons
To: Mike Shillingford
Subject: subclassing ALL windows on desktop
I don't know what got into me, but I spent quite a bit of time writing an
example for you (it was a good exercise, I have written many 16bit os/2
hooks, but this was the first 32bit one). I've had someone bugging me to
write a magazine article; maybe this will be the topic:-)
-----------------------------------------------------------------------
Here is a sample program for a System Hook. It waits for new window frames
(WC_FRAME) to be created, then adds a "close" button to the
title bar. This may seem like a silly example, but I am starting to like
the idea of having a way to "single-click" a window closed.
This program uses two types of hooks: A WinSendMsg Hook and an Input Hook.
The SendMsg Hook is required to intercept the WM_CREATE message,which is
sent to the window directly, not placed in the message queue (ie: posted)
of the window. The Input Hook is used to monitor the message queue for a
special WM_USER message that is posted by the SendMsg Hook. This is done
because during the WM_CREATE, the title bar and system menu don't not
exist yet.
System input hooks MUST be in a dll. They are called in the context of the
program being hooked --- this is very significant detail. If the hook dll
has any "MULTIPLE" data segments, and/or an
"INITINSTANCE" attribute (both of which are required to use the C
runtime in your dll),these actions will be carried out for _every_ program
that is started. In other words, the DLL would be reloaded/reinitialized
for every program that the system launches. If you wanted to open a log
file, for example, you would have to open it again and again as each
program is started.
This example program (and all the other system hooks I have written) does
not use ANY C runtime functions. It can, therefore, be
"INITGLOBAL" with "SINGLE" data; hence, it is not
reinitialized for each program that is started. Furthermore, it does not
cause the system to create a new DGROUP each time a program is started. As
a nice side effect, the code is small and fast (the 16bit version of this
dll is only 970 bytes!)
If the purpose of your hook is too complicated to avoid the C
runtime, try to split the program in two --- one simple hook dll
and one PM program that it posts messages too, where you can use as
much CRT as you like.
Keep in mind that these hooks are called for every message in
the system -- don't waste any time passing on the messages
you are not interested in.
Another reason to keep them small is System Hooks are very hard to debug
(since they are run in the context of each program being hooked, not the
program that installs the hook). It is best to test all of your code in a
normal PM program, then put it into the hook.dll.
The example dll contains the code to install the hooks itself, so all that
is required to activate it is to load it (DosLoadModule()), even from a
non-PM program. Here's a sample that will load it:
#include
#include
#define INCL_NOPMAPI
#define INCL_DOS
#include
int main(void)
{
APIRET rc;
HMODULE hm = 0;
rc = DosLoadModule(NULL, 0, "ghook", &hm);
if(rc){
printf("sys%04x: Error loading ghook.dll\n", rc);
return 1;
}
getch();
/* after a key is pressed the program will end and the hooks
* will disable themselves
*/
return 0;
}
Note: WorkPlace shell folders are not standard frames (WC_FRAME), so they
will not get the new "close" button. I didn't have time to
investigate this further.
Peter Fitzsimmons, Jan 7 1995.
sol3{at}olc.gvc.com
[makefile for IBM Cset ++ 2.x -------------------------------------------]
all: ghook.dll test.exe
ghook.obj: ghook.c
icc /Kb /Ms /Gn /O /Gs /Ssem /Rn /c ghook.c
init.obj: init.asm
masm /Mx /Zi init.asm;
ghook.dll: ghook.obj ghook.def init.obj
icc /Ge- /Gn /Gs /Ssem /Rn ghook.obj ghook.def init.obj os2386.lib
test.exe: test.c
icc /Kb /O /Ssem test.c
continued...
--- Maximus/2 2.02p1
* Origin: Sol 3/Toronto (905)858-8488 (1:259/414)SEEN-BY: 105/42 620/243 711/401 409 410 413 430 807 808 809 934 955 712/407 SEEN-BY: 712/515 628 704 713/888 800/1 7877/2809 @PATH: 259/414 400 99 250/702 3615/50 396/1 270/101 105/103 42 712/515 @PATH: 711/808 809 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.