[comp.sys.mac.programmer] SFGetFile

ar4@sage.cc.purdue.edu (Piper Keairnes) (03/20/90)

Got a problem folks... what's wrong with this simple code???

int handleDialog(theItem, dp)
int        theItem;
DialogPtr  dp;
{
    return(theItem);
}

GetDir()
{
	Point	where = {80,80};
	SFReply reply;
	ProcPtr ppr = handleDialog;

	SFPGetFile(where, NIL, NIL, 1, "", ppr, &reply, GET_DIALOG, NIL);
}

Several problems (first time into handleDialog):
    1) when items are passed into handleDialog in their stated order
          I get a value of 9 for theItem, and dp points to boofoo land
    2) when items are passed in opposite order (dp, theItem) I get
	  a value of -1 for theItem and dp points to something reasonable
 
Are the order of the items screwed up in IM ??

When handleDialog returns I get an illegal instruction command from the
debugger and when I select from the pop-up menu the last procedure,
it indicates the position at the bracket below the Getfile.... huh?

Help me!!!!!

-----
Piper Keairnes
ar4@sage.cc.purdue.edu

rdclark@Apple.COM (Richard Clark) (03/20/90)

In article <3830@sage.cc.purdue.edu> ar4@sage.cc.purdue.edu (Piper Keairnes) writes:
>Got a problem folks... what's wrong with this simple code???
>
>int handleDialog(theItem, dp)
>int        theItem;
>DialogPtr  dp;
>{
>    return(theItem);
>}
>
>GetDir()
>{
>	Point	where = {80,80};
>	SFReply reply;
>	ProcPtr ppr = handleDialog;
>
>	SFPGetFile(where, NIL, NIL, 1, "", ppr, &reply, GET_DIALOG, NIL);
>}
>
OK, we'll take it from the top. First, C passes paramaters and function results
in a different way than Pascal (C reverses the order of the parms, and uses
register D0 for function results (Pascal uses the stack.)) The simple fix for
this is to use the "pascal" keyword thusly:
  pascal int handleDialog(int theItem, DialogPtr dp)
 
The reason you have to do this is that the Toolbox uses the Pascal calling
conventions, and expects callback/filter routines to do the same.

Another (potential) problem is the use of "int". Pascal INTEGERs are 16 bits.
C integers are 16 or 32 bits on the Macintosh depending on which C compiler
you use (THINK C: 16 bits; MPW C: 32 bits.)  For this reason, you should use 
"short" which is _always_ 16 bits.
 
Now, on to the SFP statement. For the TypeList, you really do have to declare a
variable of SFTypeList and fill in 1 (or more) of the fields, then pass the
address of that structure. If you want "all files", then pass 0 for the type
count and NIL for the pointer to the type list.
 
Hope this helps.



-- 
-----------------------------+-----------------------------------------------
Richard Clark                | "If you don't know where you're going,
Instructor/Designer          |  don't go there" -- Sybalski's Law	
Apple Developer University   +-----------------------------------------------
AppleLink, GEnie, Delphi, MCI, Internet: rdclark  CI$: 71401, 2071

keith@Apple.COM (Keith Rollin) (03/20/90)

In article <39665@apple.Apple.COM> rdclark@Apple.COM (Richard Clark) writes:
> 
>Now, on to the SFP statement. For the TypeList, you really do have to declare a
>variable of SFTypeList and fill in 1 (or more) of the fields, then pass the
>address of that structure. If you want "all files", then pass 0 for the type
>count and NIL for the pointer to the type list.
> 

Two out of three, Richard. You have to pass -1 as the count if you want "all
files". 

>-----------------------------+-----------------------------------------------
>Richard Clark                | "If you don't know where you're going,
>Instructor/Designer          |  don't go there" -- Sybalski's Law	
>Apple Developer University   +-----------------------------------------------
>AppleLink, GEnie, Delphi, MCI, Internet: rdclark  CI$: 71401, 2071
>


-- 
------------------------------------------------------------------------------
Keith Rollin  ---  Apple Computer, Inc.  ---  Developer Technical Support
INTERNET: keith@apple.com
    UUCP: {decwrl, hoptoad, nsc, sun, amdahl}!apple!keith
"Argue for your Apple, and sure enough, it's yours" - Keith Rollin, Contusions

siegel@endor.harvard.edu (Rich Siegel) (03/20/90)

In article <3830@sage.cc.purdue.edu> ar4@sage.cc.purdue.edu (Piper Keairnes) writes:
>Got a problem folks... what's wrong with this simple code???
>
>int handleDialog(theItem, dp)

	Bzzt. Dialog hooks, filters, and all other Toolbox callbacks have
to be declared "pascal", since the Toolbox doesn't know to call them with the
C conventions. Upon return of your function, the stack is getting misaligned.

	You're also getting the wrong results within your hook because the
arguments are getting passed in the reverse of what the routine expects.

R.



~~~~~~~~~~~~~~~
 Rich Siegel
 Staff Software Developer
 Symantec Corporation, Language Products Group
 Internet: siegel@endor.harvard.edu
 UUCP: ..harvard!endor!siegel

"When someone who makes four hundred and fifty dollars an hour wants to
tell you something for free, it's a good idea to listen."

~~~~~~~~~~~~~~~

rdclark@Apple.COM (Richard Clark) (03/21/90)

In article <39667@apple.Apple.COM> keith@Apple.COM (Keith Rollin) writes:
>In article <39665@apple.Apple.COM> rdclark@Apple.COM (Richard Clark) writes:
>> 
>>Now, on to the SFP statement. For the TypeList, you really do have to declare a
>>variable of SFTypeList and fill in 1 (or more) of the fields, then pass the
>>address of that structure. If you want "all files", then pass 0 for the type
>>count and NIL for the pointer to the type list.
>> 
>
>Two out of three, Richard. You have to pass -1 as the count if you want "all
>files". 
>

That's what I get for not looking at the documentation. Does anybody know
how to get red text (to match the color of my face?)



-- 
-----------------------------+-----------------------------------------------
Richard Clark                | "If you don't know where you're going,
Instructor/Designer          |  don't go there" -- Sybalski's Law	
Apple Developer University   +-----------------------------------------------
AppleLink, GEnie, Delphi, MCI, Internet: rdclark  CI$: 71401, 2071