home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / hooks1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  2.2 KB  |  82 lines

  1. ;/* hooks1.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfis -j73 hooks1.c
  3. Blink FROM LIB:c.o,hooks1.o TO hooks1 LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7.     #include <exec/types.h>
  8.     #include <exec/libraries.h>
  9.     #include <utility/hooks.h>
  10.     #include <dos.h>
  11.  
  12.     #include <clib/exec_protos.h>
  13.     #include <clib/dos_protos.h>
  14.     #include <clib/utility_protos.h>
  15.  
  16.     #include <stdio.h>
  17.  
  18.     extern struct Library *SysBase;
  19.     struct Library *UtilityBase;
  20.  
  21.     #define ASM     __asm
  22.     #define REG(x)  register __ ## x
  23.  
  24.     /* This function converts register-parameter Hook calling
  25.      * convention into standard C conventions.  It requires a C
  26.      * compiler that supports registerized parameters, such as
  27.      * SAS/C 5.xx or greater.
  28.      */
  29.  
  30.     ULONG ASM
  31.     hookEntry(REG(a0) struct Hook *h, REG(a2) VOID *o, REG(a1) VOID *msg)
  32.     {
  33.         return ((*(ULONG (*)(struct Hook *,VOID *,VOID *))(h->h_SubEntry))(h, o, msg));
  34.     }
  35.  
  36.     /* This simple function is used to initialize a Hook */
  37.     VOID InitHook (struct Hook *h, ULONG (*func)(), VOID *data)
  38.     {
  39.         /* Make sure a pointer was passed */
  40.         if (h)
  41.         {
  42.             /* Fill in the Hook fields */
  43.             h->h_Entry = (ULONG (*)()) hookEntry;
  44.             h->h_SubEntry = func;
  45.             h->h_Data = data;
  46.         }
  47.     }
  48.  
  49.     /* This function only prints out a message indicating that we are
  50.      * inside the callback function.
  51.      */
  52.  
  53.     ULONG MyFunction (struct Hook *h, VOID *o, VOID *msg)
  54.     {
  55.         /* Obtain access to the global data segment */
  56.         geta4();
  57.  
  58.         /* Debugging function to send a string to the serial port */
  59.         printf("Inside MyFunction()\n");
  60.  
  61.         return (1);
  62.     }
  63.  
  64.     int main (int argc, char **argv)
  65.     {
  66.         struct Hook h;
  67.  
  68.         /* Open the utility library */
  69.         if (UtilityBase = OpenLibrary ("utility.library", 36))
  70.         {
  71.             /* Initialize the callback Hook */
  72.             InitHook (&h, MyFunction, NULL);
  73.  
  74.             /* Use the utility library function to invoke the Hook */
  75.             CallHookPkt (&h, NULL, NULL);
  76.  
  77.             /* Close utility library now that we're done with it */
  78.             CloseLibrary (UtilityBase);
  79.         }
  80.         else printf ("Couldn't open utility.library\n");
  81.     }
  82.