[comp.sys.mac.programmer] How do you execute a CODE resource?

engber@gumball.ils.nwu.edu (Mike Engber) (08/14/90)

Let's say you have a few CODE resources and you want to execute
them (ala XCMDS in Hypercard). How do you do it?

I can get as far as opening the resource file, getting handles
to the CODE resources, but I don't know where to go from there?

I have actually been able to do this from Macintosh Allegro Common
Lisp because they provide the function, ff-call, which takes a
CODE resource handle and executes it. But, I don't know how ff-call
works or how I could accomplish this from C or Pascal.

-ME

mxmora@unix.SRI.COM (Matt Mora) (08/14/90)

In article <1348@anaxagoras.ils.nwu.edu> engber@gumball (Mike Engber) writes:
>Let's say you have a few CODE resources and you want to execute
>them (ala XCMDS in Hypercard). How do you do it?
>
>I can get as far as opening the resource file, getting handles
>to the CODE resources, but I don't know where to go from there?
>

Try something like this in pascal;


	procedure DoJsr (addr: ProcPtr);
	inline
		$205F, $4E90;



procedure executecode(myhandle:handle);
	
var
   addr:procptr;

begin
	HLock(myhandle);
	addr := procptr(myhandle^);
	DoJsr(addr);
	hunlock(myhandle);
end;




The key of course is the inline procedure.

I found the Dojsr from the hpercard interface stuff I think.







-- 
___________________________________________________________
Matthew Mora                |   my Mac  Matt_Mora@sri.com
SRI International           |  my unix  mxmora@unix.sri.com
___________________________________________________________

rmh@apple.com (Rick Holzgrafe) (08/15/90)

From C, something like this:

    Handle h;                              /* Handle to resource */
    int (*myFuncPtr) (int arg1, int arg2);    /* Ptr to function */
    int theResult;    /* Save the return value from the function */

    /* Get handle to code resource */
    h = GetResource ('myFN', 128);
    /* Lock it down - Very important! */
    HLock (h);
    /* Get appropriately typed function pointer */
    myFuncPtr = (int (*)(int, int))*h;
    /* Call the function */
    theResult = (*myFuncPtr)(arg1, arg2);

See? You de-reference the handle to get a pointer, and typecast that into 
a pointer to the kind of function you are going to call. Then just call it.

After the call, you can clean up in whatever way is appropriate: release 
the resource, if you don't need it any more; or save the handle but unlock 
it until you need it again, to avoid heap fragmentation.

I don't know how to do this in Pascal, I'm afraid. Hope this helps.

==========================================================================
Rick Holzgrafe              |    {sun,voder,nsc,mtxinu,dual}!apple!rmh
Software Engineer           | AppleLink HOLZGRAFE1          rmh@apple.com
Apple Computer, Inc.        |  "All opinions expressed are mine, and do
20525 Mariani Ave. MS: 3-PK |    not necessarily represent those of my
Cupertino, CA 95014         |        employer, Apple Computer Inc."