home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / i-43 / hooks1.c < prev   
C/C++ Source or Header  |  1992-09-04  |  2KB  |  82 lines

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