hawley@adobe.COM (Steve Hawley) (09/28/90)
Several days ago Tracy Narine (tracyn@dgp.toronto.edu) posted a question on how to solve the problem of recovering a handle to yourself in an INIT in MPW C without having to resort to writing an assembly language stub to load a resource genertaed by C. In think C, you have the benefit of an inline assembler, so you can do something like: Handle this_is_me; main() { asm { _RecoverHandle /* a0 is set up already */ move.l a0, this_is_me } HLock(this_is_me); /* or whatever */ } In discussing this with her I came up with the following code: Handle this_is_me; typedef Handle (*FuncReturningHandle)(); static unsigned short FooledYou[] = { 0xa128, /* trap word for _RecoverHandle */ 0x2008, /* code for move.l a0, d0 */ 0x4e75 /* code for rts */ }; main() { this_is_me = (*((FuncReturningHandle)FooledYou))(); /* This treats the static array, FooledYou, as a function and * executes it. Being a good citizen we follow C and Macintosh * conventions of leaving the return value of the function in * register d0, which gets assigned to this_is_me. */ HLock(this_is_me); /* etc */ } I'd appreciate it if someone would test this. I don't have MPW. I checked it on a Sun 3/80, eliminating the trap word and stepping through the code an instruction at a time, and it worked like a gem. Hopefully, getting the address of the static array shouldn't get in the way. I realize this is an abuse of the sleazy handling of C pointers, but I think this is a clever way around the problem. Enjoy. Steve Hawley hawley@adobe.com -- "I'm sick and tired of being told that ordinary decent people are fed up with being sick and tired. I know I'm certainly not, and I'm sick and tired of begin told that I am." -Monty Python