[comp.sys.mac.programmer] CDEF's with Lightspeed C

quesnel@ireqs3.UUCP (Roger Quesnel 7075) (02/10/89)

I am currently working on a CDEF using Lightspeed C. Does anyone know how
to write a control definition as part of an application instead of as a 
separate resource? It would be a lot easier to debug !

			 Thanks,  
					  Rene Quesnel

topping@dinghy.cis.ohio-state.edu (brian e. topping) (02/12/89)

One trick I use with LDEFs is to define a resource that consists of the bytes
to hold a JMP instruction to a procedure you have compiled in the middle of
your program.  Since you don't know ahead of time where that procedure will
be loaded, you have to load the resource and then store the address of the
procedure into the JMP instruction.  If this resource is, in my case, 'LDEF',
and I load it, lock it, store the procPtr in the resource (in memory only),
and call _LNew with the number of the resource that I have already loaded and
locked, the list manager calls the resource manager looking for the requested
'LDEF' and is returned a handle to the already loaded 'LDEF'.  Then, when
the LM executes this fake 'LDEF' resource, it just jumps to the code I have
in my main procedure.  Here is some code in C as an example:


/**********************************/

typedef struct jumpRecord
	{
	short instruction;
	void (*function)();
	} jumpRecord,**jumpHandle;


theOpenProc()
{
jumpHandle	listDefJump;

	listDefJump = (jumpHandle)GetResource('LDEF',FAKELDEFID);
	(*listDefJump)->function = ourLDEF;
	.
	.
	/* more init code */
}

In this example, I have the locked bit of the resource set, but if you planned
on releasing the software this way you would want to load it unlocked with
_GetResource, call _MoveHHi to move the handle block somewhere it wont be in
the way, finally calling _HLock and the second line of this code to finish
up.  This will keep you from making an "island in the heap", so to speak.
Hope this helps... -bri