[comp.sys.mac.programmer] CODE resources

rah0777@cec1.wustl.edu (Rexford Alan Hill) (07/10/90)

I am currently building an application in which I would like for the user
to be able to add "modules" to it.  Each module would have code for perf-
orming some specific task.  I think I want to build code resources and
install them in the resource fork of the application in order to do this,
but I'm not sure how to deal with them.  Here's what I've done so far:
  In THINK C 3.xx I build a code resource of type XFRM and gave it an
ID and a name.
  In THINK C 3.xx I wrote a quick and dirty application to open a window
and create a menu bar.  In one of the menus, I AddResMenu with a type of
'XFRM'.  This adds the name of any resources of type XFRM that the Mac finds.
Then, I load the resource.  I have a handle to it, and I typecast the handle
to be a (void **) (after locking it of course).  I have a variable named
procP that is typed as follows:
   void (*procP)();
And I set procP to point to the resource:
   procP = *handleName;
I then attempt to call procP (the code resource I built had a main with no
parameters, so I cal procP with no parameters).
   (*procP)();

  The code resource was supposed to draw a line diagonally in the window:
  main()
  {
	MoveTo(10,10);
	LineTo(50,50);
  }

  But, of course, nothing happens.  Am I declaring things correctly?  Am
I calling the thing correctly?  When the code resource is loaded in, is it
just a piece of code that I can point to, or is there something else there?
  IM gives no hints that I could find, and the THINK manual explains building
code resources, but not calling them from within another program.  Any help
would be greatly aprreciated.

P.S. I am also open to any suggestions for better ways to handle a 'growable'
application.

						-rex

Rexford A. Hill
Student, Washington University (in St. Louis)
rah0777@cec2.wustl.edu
rex@dworkin.wustl.edu
rex@wuccrc.wustl.edu

rah0777@cec1.wustl.edu (Rexford Alan Hill) (07/11/90)

Recently, I posted a question to this newsgroup about calling code resources
that I wrote from another program that I wrote.  I was trying to do this by
installing the (THINK C 3.0) compiled code resource in the (THINK C 3.0)
compiled application.  The application called the code resource by reading
it into memory using GetResource() and typecasting the returned handle
as a (** void).  I then dereferenced the handle once (after locking) and
then attempted to use this pointer to the memory-resident resource as a 
pointer to a function.  This failed, and that was the crux of my question-
why?  
  Last night, I tried fooling with it a bit and found that if I copied
the compiled resource into the .rsrc file for my application's project, the
thing worked when run from the THINK compiler/editor, but failed when I 
built an application and then ran it.  What's the deal here?  Is my resource
fork for the application open when I run it or what?  Here's the program snip
I use to call the code resource:

Handle	theHand;
void	*theProc;

main()
{
  /* some initialization stuff */

  theHand = GetResource('XFRM',1);
  HLock(theHand);
  theProc = *((void **)theHand);
  (*theProc)();
  HUnlock(theHand); 

  /* other stuff */
}

Perhaps I'm not setting some flags for the resource?  Perhaps it's not being
read in?  If you have any suggestions/flames, please email or post.
Thanks very much.

					-rex

Rexford A. Hill
Washington University in St. Louis
rah0777@wucec1.wustl.edu
rex@dworkin.wustl.edu
rex@wuccrc.wustl.edu