home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / MyGestaltValue / MyGestaltValue.c next >
Encoding:
Text File  |  1994-11-11  |  1.2 KB  |  35 lines  |  [TEXT/MMCC]

  1. // MyGestaltValue was written by François Pottier (pottier@dmi.ens.fr).
  2. // Please let me know if you find bugs in this code, or if you improve on it!
  3.  
  4. // InstallGestaltValue looks for the 0xDEADBEEF constant in the selector's code and replaces it with the actual value.
  5. // Then it flushes the processor caches. Note that it looks for the constant only on 4-byte boundaries, so the constant had better
  6. // be there!
  7.  
  8. #include "MyGestaltValue.h"
  9.  
  10. static Boolean HWPrivAvailable (void)
  11. {
  12.     return (GetOSTrapAddress(_HWPriv) != GetToolTrapAddress(_Unimplemented));
  13. }
  14.  
  15. OSErr InstallGestaltValue (OSType signature, long value)
  16. {
  17.     Handle        selector;
  18.     Ptr            p;
  19.     
  20.     selector = Get1Resource('GSel', 0);                        // Load the resource containing the selector's code
  21.     if (!selector)                                        // Don't use ResError() because it sometimes returns noErr
  22.         return memFullErr;
  23.     DetachResource(selector);
  24.     
  25.     p = *selector;                                        // Look for the magic cookie
  26.     while (* (long*) p != 0xDEADBEEF) p++;
  27.     * (long*) p = value;                                    // Replace it with the actual value
  28.     
  29.     if (HWPrivAvailable()) {                                // Flush the processor caches, it's safer
  30.         FlushDataCache();
  31.         FlushInstructionCache();
  32.     }
  33.     
  34.     return NewGestalt(signature, (SelectorFunctionUPP) *selector);
  35. }