[comp.sys.mac.programmer] SAMPLE cdev in C

osborn@ux1.lbl.gov (James R Osborn) (02/06/89)

Greetings Wise Ones,

	I have been attempting to do the SAMPLE cdev given in IM-V p.323
using LSC 3.01p3.  I was simply trying to do a direct translation from the
example in Pascal to C.  When I try to run the damn thing (in the control
panel), the ICN# shows up properly and I can select it.  Sometime soon
after it has drawn in the gray background and the specified nrct, it crashes
(causing my MacsBug to take over) with an address error at 0x3.  The thing
crashes before the dialog items are drawn, but I don't know if the control
panel calls my cdev function first or not.  My resources appear to be in
ship shape.
	My first thought was that my function is not using the proper
calling conventions or something along these lines.  I can see no problems
however, and here is how I declared the function:


pascal long main (message, Item, numItems,
				CPanelID, theEvent, cdevValue, CPDialog)
int		message, Item, numItems, CPanelID;
EventRecord	*theEvent;
long		cdevValue;
DialogPtr	CPDialog;


	I don't know how to prod around with MacsBug very effectively, so
I don't really know where the thing is bombing.  Aside from any suggestions
on this problem, can anyone offer any tips on debugging code resources with
LSC?  I could not find any information about this in the manuals.  The only
thing I could think of (in this particular case) would be to write an 
application that would call this thing and mimic the control panel's inter-
action with it.

	BTW, if people who don't know how to write cdev's are interested in
this C example, I shall post it (when it works) in (I think):

				 comp.sys.mac.sources

THANKS in advance for any help,

				James R. Osborn
				osborn@ux1.lbl.gov
				Lawrence Berkeley Laboratory

rae@geaclib.UUCP (Reid Ellis) (02/06/89)

osborn@ux1.lbl.gov (James R Osborn) writes:
|	I have been attempting to do the SAMPLE cdev given in IM-V p.323
|using LSC 3.01p3.  I was simply trying to do a direct translation from the
|example in Pascal to C.  When I try to run the damn thing (in the control
|panel), the ICN# shows up properly and I can select it.  Sometime soon
|after it has drawn in the gray background and the specified nrct, it crashes

I, too was bitten with this.  The problem is that the example, as written,
will not work with newer ROMs.  To determine if there is sufficient RAM
available, it calls GetResource to load a PACK [or is it PKGE?] resource.
With the new ROM, this package is in ROM and thus the call to GetResource
returns NULL [the resource isn't in any open file, after all].  To fix
this, either change the call to NGetResource [I think that's what it's
called -- I don't have IM V handy, sorry] which checks ROM resources as
well, or else re-write the code entirely and just check for enough free
memory for the cdev.

I wrote a cdev that used this same GetResource thing and got bitten
when the new ROMs came out.  Alas, I lost the source [at least I couldn't
find it last time I checked] so it is lost forever.  It just diddled with
intl0 and intl1 resources, along the lines of "Simon" [but it let you
change the short date separator too -- I don't like "/"].

				Hope this helps,
					Reid

"Will Vooper and Honest Crocus obliterate Ylum in a
 vicious arms race?  Let's all stay up late worrying
 about it!"

ted@hpwrce.HP.COM ( Ted Johnson) (02/07/89)

>BTW, if people who don't know how to write cdev's are interested in
>this C example, I shall post it (when it works) in (I think):

I'd like to see it posted!

-Ted

osborn@ux1.lbl.gov (James R Osborn) (02/07/89)

	Thank you very much Reid Ellis!

	You are exactly right.  Simply omitting the segment which attempts
to load the (now in ROM) resource caused it to function properly.  This does
however point out a bug in the logic of the example code as printed in
IM-V Ch.18.  The whole point was to check if there was sufficient memory.
When GetResource returned NULL (since the PACK is now in ROM), the cdev should
have gracefully aborted and informed the control panel that it had insufficient
memory.  This would have resulted in a grayed out panel and NOT a crash.

	Now since   comp.sys.mac.sources   does not exist WHERE SHOULD I POST
THIS SHORT SIMPLE CDEV WRITTEN IN C?


	Thanks again -- and thanks Tim Maroney for your $0.02 too!

James R. Osborn
osborn@ux1.lbl.gov
Lawrence Berkeley Laboratory

osborn@ux1.lbl.gov (James R Osborn) (02/08/89)

	For those interested in writing cdevs in C:

	A COMPLETE listing (including resource file) has been posted on:

	-----------------
	comp.sources.mac
	-----------------

	Hope this helps people.  I know doing it helped me.

James R. Osborn
osborn@ux1.lbl.gov
Lawrence Berkeley Laboratory

thecloud@pnet06.cts.com (Ken Mcleod) (02/08/89)

osborn@ux1.lbl.gov (James R Osborn) writes:
>
>        A COMPLETE listing (including resource file) has been posted on:
>
>        -----------------
>        comp.sources.mac
>        -----------------

 Unfortunately, if you actually posted it TO comp.sources.mac, no one
will see it! Comp.sources.mac and comp.binaries.mac are *moderated*
newsgroups; submissions must be sent to the moderator.

 To submit something to comp.sources.mac or comp.binaries.mac, you
mail it to the following address:

         macintosh@felix.UUCP
or       felix!macintosh@ics.uci.edu

and after a while, it will appear in the appropriate newsgroup.
The moderator for both groups is Roger Long (bytebug@dhw68k.cts.com).

-ken
(Note: my pnet06 address dies on Feb.10 along with the MouseHole).
==========    .......    ===================================   ============
Ken McLeod   :.     .:   UUCP: {crash uunet}!pnet06!thecloud   "They mean
----------  :::.. ..:::  InterNet: thecloud@pnet06.cts.com      to win
!(C)1988       ////            or thecloud@dhw68k.cts.com       Wimbledon!"

bytebug@dhw68k.cts.com (Roger L. Long) (02/17/89)

In article <6600@hodge.UUCP> thecloud@pnet06.cts.com (Ken Mcleod) writes:
>osborn@ux1.lbl.gov (James R Osborn) writes:
>>
>>        A COMPLETE listing (including resource file) has been posted on:
>>
>>        -----------------
>>        comp.sources.mac
>>        -----------------
>
> Unfortunately, if you actually posted it TO comp.sources.mac, no one
>will see it! Comp.sources.mac and comp.binaries.mac are *moderated*
>newsgroups; submissions must be sent to the moderator.

Posting to a moderated newsgroup effectively mails it to the moderator.
-- 
	Roger L. Long
	dhw68k!bytebug