[comp.sys.mac.hypercard] XFCN QUESTION

carasso@nunki.usc.edu (Roger David Carasso) (11/04/89)

The examples I have seen were too complicated.
Can someone tell me how to call an XFCN written in MPW C.


I compile it.  What do I put as it's owner, type, etc?
How do I tell HyperCard what file this function is in?  or do
specify someother way?

For example,  How would I bring this into hypercard:


int foobar(x)
 int x;
 {
  return(x + 5);
 }

-------------------------------------------------------
Thank you VERY Much,

Roger C.

mjm@eleazar.dartmouth.edu (Michael McClennen) (11/07/89)

In article <6238@merlin.usc.edu> carasso@nunki.usc.edu () writes:
>
>The examples I have seen were too complicated.
>Can someone tell me how to call an XFCN written in MPW C.
>
>I compile it.  What do I put as it's owner, type, etc?
>How do I tell HyperCard what file this function is in?  or do
>specify someother way?
>
>For example,  How would I bring this into hypercard:
>
>
>int foobar(x)
> int x;
> {
>  return(x + 5);
> }

Well, first of all, XCMDs and XFCNs are code resources.  You compile them just
as you would a WDEF, CDEF, or any other type.  You don't have to worry about
the type or creator of the file you compile them into, since you must copy them
into your stack with ResEdit (or the ResCopy XCMD) anyway.

Secondly, the main routine of your XCMD will be called with one argument: a
pointer to a param block whose structure is declared in the example file
"HyperXcmd.h".  This param block contains, among other things:  the number
of arguments passed to your XCMD from the script that invoked it and a handle
to each one.  These arguments are all C strings, even if you specified a number
in HyperTalk.  The reason for this is that HyperTalk handles numbers as strings.
So if you want your XCMD to work with numbers, you must convert your arguments
to binary format.  Fortunately, HyperCard can provide this service, and many
others, for you:

The param block passed to your XCMD also contains an address in the HyperCard
code which you can call to have HyperCard perform services for you (there are
also slots for arguments and return values for this.)  Among the services:
converting numbers to strings and back again, converting pascal to C strings
and back, manipulating the value of HyperCard global variables and fields, etc.
You don't even have to worry about the specifics of these call backs, because
the example file "XcmdGlue.inc.c" provides glue routines.  Just copy the ones
you need into your code.

A final note:  Your main routine must be declared "pascal".  If you do not
do this, HyperCard will bomb upon termination of your XCMD since it expects a
result to have been placed on the stack...

HyperXcmd.h and XcmdGlue.inc.c are part of the HyperCard developers'
toolkit available from APDA and on fine bulletin boards everywhere!


Michael McClennen (mjm@dartmouth.edu)
Dartmouth College Humanities Computing