home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sys / mac / programm / 20288 < prev    next >
Encoding:
Text File  |  1992-12-24  |  3.1 KB  |  77 lines

  1. Path: sparky!uunet!olivea!apple!grobbins
  2. From: grobbins@Apple.COM (Grobbins)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: Re: Gestalt Selectors in APPL
  5. Message-ID: <75922@apple.apple.COM>
  6. Date: 24 Dec 92 06:19:14 GMT
  7. References: <BzqC31.FAM@world.std.com>
  8. Organization: Experimental System Software Investigations
  9. Lines: 66
  10.  
  11. In article <BzqC31.FAM@world.std.com> zobkiw@world.std.com (Joe Zobkiw) writes:
  12. >1) Can I perform a "6-byte-xdef" type trick on this 
  13. >code? I'm not sure I can since even if I mark the 
  14. >6-byte resource as sysHeap the code it points to is 
  15. >still in my application heap.
  16.  
  17. It should work so long as the application is running, but since the
  18. application may quit and there is no way to remove a gestalt selector,
  19. you will need at least one totally stand-alone block of code.  So the
  20. 6-byte trick won't solve this problem.
  21.  
  22. (This limitation doesn't apply to things like time manager tasks; since
  23. they can be removed, it is appropriate to simply make the task code a
  24. function of the application, or to put an initial jump instruction in
  25. the system heap, and remove the task before the app quits.)
  26.  
  27. You can copy a function from the application into a block in the
  28. system heap.  If the function were called MySelectorFunction and
  29. it were followed by a procedure called NullProc, then this would
  30. work:
  31.  
  32.  mySelectorFunctionSize := Ord4(@NullProc) - Ord4(@MySelectorFunction);
  33.  { mySelectorFunctionSize should be something reasonable, definitely >0 }
  34.  
  35.  { make space in the system heap }
  36.  mySelectorFunctionPtr := NewPtrSys(mySelectorFunctionSize);
  37.  IF mySelectorFunctionPtr <> NIL THEN
  38.     BEGIN
  39.       { copy the function into the system heap }
  40.       BlockMove(@MySelectorFunction, mySelectorFunctionPtr, 
  41.                 mySelectorFunctionSize);
  42.       retCode := NewGestalt(kMySelector, mySelectorFunctionPtr);
  43.     END;
  44.  
  45. This is more fragile than just compiling a stand-alone code resource
  46. (on which you'd call set the SysHeap bit, call GetResource, DetachResource,
  47. HLock; see IM VI 3-43) since the BlockMove method makes the assumption
  48. that the functions in the compiled code are in the same relative order
  49. in memory as they are in the source file.  Check the compiler output
  50. with a disassembler (like ResEdit's code viewer) to make certain that
  51. functions are sequential as expected in the final application code.
  52.  
  53.  
  54. >2) If I place my Gestalt selector function in a separate 
  55. >CODE resource, can I mark that resource as sysHeap and 
  56. >have it work properly? This would be WITHOUT any 6-byte 
  57. >tricks.
  58.  
  59. Yep, that's the canonical way.  Be sure it's locked in memory before
  60. installation, too.
  61.  
  62. >3) How can I remove the Gestalt selector once the app 
  63. >quits? Will
  64. >    err = ReplaceGestalt(selector, nil, &oldGestaltFunction);
  65. >work or will this cause the System to jump to location 0 
  66. >the next time someone calls my selector?
  67. >What's the easiest way? Thanks in advance! :)
  68.  
  69. Rather than try to remove the selector, just make a simple replacement
  70. selector function that returns zero or something else distinct and
  71. strand it in the System heap.
  72.  
  73.  
  74. Grobbins              grobbins@apple.com
  75.  
  76. Usual disclaimers apply.
  77.