Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!decwrl!ucbvax!hoptoad!tim From: tim@hoptoad.uucp (Tim Maroney) Newsgroups: comp.sys.mac.programmer Subject: Re: Calling Code resources? Message-ID: <6917@hoptoad.uucp> Date: 7 Apr 89 22:48:37 GMT References: <11817@ut-emx.UUCP> Reply-To: tim@hoptoad.UUCP (Tim Maroney) Distribution: usa Organization: Eclectic Software, San Francisco Lines: 36 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 Pascal, you're probably going to need to link in some assembly language or at least include some INLINEs. In C, as long as the code resource follows C coding standards, you can do it all in the high level language. Get the resource and lock it down first. You then have a handle. The handle indicates a pointer. Take that pointer and convert it to a function pointer. Then call the function pointer. Here's an example in MPW C: void (*f)(); Handle res = GetResource('code', 1); if (res == 0) return; HLock(res); f = (void (*))*res; (*f)(name); And here's one in Lightspeed C, which seems to have some problems with casting things to function pointers (actually, K&R seems ambiguous on the proper syntax, so I guess we can't blame them): long (*f)(); Handle h = GetResource(TheType, TheID); if (h) { HLock(h); BlockMove(h, &f, 4); return (*f)(select, TheID, where, h, p, multi, off); } -- Tim Maroney, Consultant, Eclectic Software, sun!hoptoad!tim This message does represent the views of Eclectic Software.