Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!uflorida!ukma!husc6!endor!singer From: singer@endor.harvard.edu (Rich Siegel) Newsgroups: comp.sys.mac.programmer Subject: Re: Executing a code segement from Lightspeed Pascal? Message-ID: <439@husc6.harvard.edu> Date: 6 Oct 88 11:11:56 GMT References: <8810051239.AA09466@decwrl.dec.com> Sender: news@husc6.harvard.edu Reply-To: singer@endor.UUCP (Rich Siegel) Organization: Symantec/THINK Technologies, Bedford, MA Lines: 77 In article <8810051239.AA09466@decwrl.dec.com> harrow@bagels.dec.com (Jeff Harrow, NMSG LKG2-2/Y10 DTN=226-5564) writes: >I've got a programming question (which will show my weakness in >Pascal... Oh well) > > >Now, I tried to convert this to Pascal (Lightspeed, of course) as >follows: > >procedure ShowHelp; > var > rsrcH : Handle; > pp : ProcPtr; > > begin > if rsrcH = GetResource('code', 2000) then > begin > HLock(rsrcH); > pp := @rsrcH; > pp; <<<< ReleaseResource(rsrcH); > end; > end; > >but I can't figure out how to actually EXECUTE the code, whose >entry point address should, I think, be in pp. (Some varients I >tried are shown above.) >I know that you have to be tricky in Pascal to do this, but what >am I missing? Two things to do: First, if you're writing your code resource in C, the main routine should be of type "pascal", as in pascal void main() Second, to call a ProcPtr, you need the following inLine: PROCEDURE CallProc(p : ProcPtr); inline $205F, $4E90; which pops the last argument off the stack, and JSRs to it. So... procedure ShowHelp; var H : Handle; hState : SignedByte; begin H := GetResource('Code', 2000); if H <> NIL) then begin hState := HGetState(h); MoveHHi(h); HLock(h); CallProc(h^); HSetState(h, hState); end end; The calls to HGetState and HSetState ensure that the resource is returned to the same state of locked-ness when you're done with it, instead of unconditionally unlocking it every time. --Rich Rich Siegel Staff Software Developer THINK Technologies Division, Symantec Corp. Internet: singer@endor.harvard.edu UUCP: ..harvard!endor!singer Phone: (617) 275-4800 x305 Any opinions stated in this article do not necessarily reflect the views or policies of Symantec Corporation or its employees.