Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!ames!oliveb!apple!han From: han@Apple.COM (Byron Han, wyl E. coyote ) Newsgroups: comp.sys.mac.programmer Subject: Re: Calling Code resources? Message-ID: <28579@apple.Apple.COM> Date: 7 Apr 89 19:56:18 GMT References: <11817@ut-emx.UUCP> <28576@apple.Apple.COM> Distribution: usa Organization: Apple R&D - Networking & Communications Lines: 55 In article <28576@apple.Apple.COM> austing@Apple.COM (Glenn L. Austin) writes: >In article <11817@ut-emx.UUCP> newman@ut-emx.UUCP (David Newman) writes: >>Here is another question for the net-gods: how do you call a >>code resource (like an XCMD or whatever) from another piece of >>code? Can it be done from a high-level language? How might it >>be done in Pascal? > >In C, it is really *relatively* simple -- all you need to do is create a >function pointer. In Pascal, however, you need to have an assembly-language >routine to do the actual call. Basically, in order to call a code resource, >you need to do the following steps: > >1) Load the resource into the heap (with your favorite GetResource call) >2) Move the resource high into the heap (with MoveHHi) >3) Lock the resource (with HLock) >4) Dereference the Handle (C: *handle, Pascal: handle^) >5) Assign the dereferenced handle to your function pointer in C. In Pascal, > it depends upon the implementation of your assembly language routine. >6) Call the function! > To wit, if you have a code resource with entry point FUNCTION foom(a, b, c, d, e, f):g you create a pascal function of the following FUNCTION callfoom(a, b, c, d, e, f, x:LONGINT):g; INLINE $205F,$4E90 To call the code resource, use callfoom instead and pass the dereferenced handle as the final argument. Doing it from assembler makes more sense to me since you have better control over register saving and assembler is more efficient. By the way if you are doing this from assembler, it is a REALLY GOOD IDEA to save registers... 2. Memory management. If the code resource has the locked bit set, the code resource will be loaded as low as possible in memory (via ResrvMem) so moving it high has no effect. If the code resource is not locked, and you don't plan to use the code resource for a "long" time, you should explicitly lock it and call MoveHHi prior to that. And HUnlock it when done. If you plan to use the resource for a long period of time and are not sure if the resource has the locked bit set, you should explictly call ResrvMem to get it as low as possible in the heap. +-----------------------------------------------------------------------------+ | Disclaimer: Apple has no connection with my postings. | +-----------------------------------------------------------------------------+ Byron Han, Communications Architect Cereal, anyone? :-) A1! Apple Computer, Inc. ------------------------------------- 20525 Mariani Ave, MS27Y Internet: han@apple.COM Cupertino, CA 95014 UUCP:{sun,voder,nsc,decwrl}!apple!han -------------------------------------- GENIE: BYRONHAN ATTnet: 408-974-6450 Applelink: HAN1 CompuServe: 72167,1664 ------------------------------------------------------------------------------