[comp.sys.mac.programmer] More Vol RefNum + WD ID Help Needed

topix@gpu.utcs.utoronto.ca (R. Munroe) (06/09/91)

By now I feel fairly brain dead.  I got lots of help when I posted
my last message about saving file info into a prefs file so I could
open the files from a menu in my app.  Most replies said to save
the volume name, working directory ID, and file name.  I think that
I have successfully done that.  I now am trying to use that info to
open up the file.  No luck.  Here is the code I use to store the info:

    WDPBRec    wdpb;

    if (reply.good)
    {
        wdpb.ioCompletion = NIL_POINTER;
        wdpb.ioNamePtr = (StringPtr)name; /* just used as a place holder */
        wdpb.ioVRefNum = reply.vRefNum;
        wdpb.ioWDIndex = 0;
        wdpb.ioWDDirID = 0;
				
        error = PBGetWDInfo (&wdpb, FALSE);
	
        /* ..... save wdpb.ioNamePtr, wdpb.ioWDDirID, and reply.fName
                 into prefs file ..... */
    }	

And here is what I'm doing to open my stored working directory ID:

    WDPBRec    wdpb;
    HFileInfo  pb;

    {
        /* ..... read wdpb.ioNamePtr (volumeName), wdpb.ioWDDirID (wdID),
                 and reply.fName (fileName) from prefs file ..... */

        wdpb.ioCompletion = NIL_POINTER;
        wdpb.ioNamePtr = (StringPtr)volumeName;
        wdpb.ioWDDirID = wdID;

        error = PBOpenWD (&wdpb, FALSE);

        pb.ioNamePtr = (StringPtr)fileName;
        pb.ioVRefNum = wdpb.ioVRefNum;
        pb.ioFDirIndex = 0;
        pb.ioDirID = 0;

        error = PBGetFInfo (&pb, FALSE);
    }

PBOpenWD always returns fnfErr (file not found).  What am I doing
wrong?  If I print out my variables, I find that, sure enough, my
volume name, working directory ID, and file name are just as I had
stored them - so I don't think the problem is with that.

I've been struggling with this for two straight days and have tried 
countless combinations and permutations of low-level File Manager
calls.  Any help would be most^32 appreciated.

Bob Munroe
topix@utcs.utoronto.ca

andyp@treehouse.UUCP (Andy Peterman) (06/10/91)

In article <1991Jun9.062859.3306@gpu.utcs.utoronto.ca> topix@gpu.utcs.utoronto.ca (R. Munroe) writes:
>And here is what I'm doing to open my stored working directory ID:
>
>    WDPBRec    wdpb;
>    HFileInfo  pb;
>
>    {
>        /* ..... read wdpb.ioNamePtr (volumeName), wdpb.ioWDDirID (wdID),
>                 and reply.fName (fileName) from prefs file ..... */
>
>        wdpb.ioCompletion = NIL_POINTER;
>        wdpb.ioNamePtr = (StringPtr)volumeName;
>        wdpb.ioWDDirID = wdID;
>
>        error = PBOpenWD (&wdpb, FALSE);
>
>        pb.ioNamePtr = (StringPtr)fileName;
>        pb.ioVRefNum = wdpb.ioVRefNum;
>        pb.ioFDirIndex = 0;
>        pb.ioDirID = 0;
>
>        error = PBGetFInfo (&pb, FALSE);
>    }
>
>PBOpenWD always returns fnfErr (file not found).  What am I doing
>wrong?  If I print out my variables, I find that, sure enough, my
>volume name, working directory ID, and file name are just as I had
>stored them - so I don't think the problem is with that.

Why do you need to open a WD?  Apple has been trying to get people to
stop using working directories and you can probably get by without them.

First, get the volumeRefNum:

        pb.ioNamePtr = (StringPtr)volumeName;
	pb.ioVRefNum = 0;
        pb.ioVolIndex = -1;
	error = PBGetVolInfo (&pb, FALSE);

This returns ioVRefNum, along with a bunch of other information (to be
sure you have the correct volume, you could have also saved the creation
date of the volume and check it at this time).

Now, to access the file, use any of the PBH calls, for example:

	pb.ioNamePtr = (StringPtr)fileName;
	pb.ioVRefNum = volumeRefNum;  // Leave out if same pb from last call	
	pb.ioFDirIndex = 0;
	pb.ioDirID = wdID;
	error = PBHGetFInfo (&pb, FALSE);

Note that as long as you call the File Manager routines synchronously
(with the second parameter FALSE), you do not need to clear ioCompletion
- the trap dispatcher does that for you.

-- 
Andy Peterman                       |  Everything you know 
treehouse!andyp@gvgpsa.gvg.tek.com  |       is wrong! 
(916) 273-4569                      |               FST 

mullerd@prism.cs.orst.edu (Douglas Muller) (06/10/91)

In article <1991Jun9.062859.3306@gpu.utcs.utoronto.ca> topix@gpu.utcs.utoronto.ca (R. Munroe) writes:
>By now I feel fairly brain dead.  I got lots of help when I posted
>my last message about saving file info into a prefs file so I could
>open the files from a menu in my app.  Most replies said to save
>the volume name, working directory ID, and file name.  I think that
>I have successfully done that.  I now am trying to use that info to
>open up the file.  No luck.  Here is the code I use to store the info:
>
>    WDPBRec    wdpb;
>
>    if (reply.good)
>    {
>        wdpb.ioCompletion = NIL_POINTER;
>        wdpb.ioNamePtr = (StringPtr)name; /* just used as a place holder */
>        wdpb.ioVRefNum = reply.vRefNum;
>        wdpb.ioWDIndex = 0;
>        wdpb.ioWDDirID = 0;
>				
>        error = PBGetWDInfo (&wdpb, FALSE);
>	
>        /* ..... save wdpb.ioNamePtr, wdpb.ioWDDirID, and reply.fName
>                 into prefs file ..... */
>    }	

	You need to save wdpb.ioNamePtr, wdpb.ioVRefNum, wdpb.ioWDDirID
	and use THEM later! (pretty sure)
>Bob Munroe
>topix@utcs.utoronto.ca

Stephen Roderick
mullerd@prism.cs.orst.edu