Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!uwm.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!decwrl!apple!apple.com!rmh From: rmh@apple.com (Rick Holzgrafe) Newsgroups: comp.sys.mac.programmer Subject: Re: Danger Will Robinson, traps for young player. Message-ID: <9847@goofy.Apple.COM> Date: 20 Aug 90 23:04:45 GMT References: <11591@wehi.dn.mu.oz> Sender: usenet@Apple.COM Organization: Apple Computer, Inc. Lines: 58 [I hope this isn't being posted twice - if so, I apologize. The mailer hiccuped the first time and I don't know if it went out or not.] In article <11591@wehi.dn.mu.oz> JON@wehi.dn.mu.oz (Jon Eaves) writes: > I want to extract the UserName from the Chooser. No problems, just use > tempHandle = GetString(CHOOSERNAME), then how do I get the "string" from > the tempHandle. The process I was using was > strcpy(tempString,PtoCstr((char *)(*tempHandle)); > Great, this sets tempString correctly. BUT, the Chooser Name field gets > mangled because the resource handle is now in 'C' string format. > When the application exits, the resource map is saved and the System > becomes unstable (Got the 'Cant Load the Finder', bomb !!) I doubt if the resource is being changed on disk. But the chooser name resource is probably in the System Heap, where it will stay even after your app exits, and be shared by any other apps (including Finder) that look for it. Rebooting should "unmangle" it, because the disk copy will still be OK. > Now some questions. > 1) Why the hell is the handle to the resource attached to that resource? > Doesn't it make some sort of copy? At no stage do I explicitly use > ChangedResource(tempHandle);WriteResource(tempHandle); > Why is it saved then? > My understanding from IM is that resources will not get saved unless you > explicitly use the above process. You're right, it doesn't get saved - but as I said above, it gets shared until reboot. In article <11591@wehi.dn.mu.oz> JON@wehi.dn.mu.oz (Jon Eaves) writes: > 2) What is the best way to extract stuff from 'tempHandle' so that things > like that don't happen? I presume there is a nice 'safe' way of doing > this. Any number of things you can do. Given your sample code, the simplest is probably: BlockMove ((Ptr)(*tempHandle), tempString, GetHandleSize(tempHandle)); PtoCstr(tempString); (Or you could just do a CtoPstr on (char *)(*tempHandle) after the strcpy you are currently using, to restore the original to its pristine state. But I prefer my code, since it leaves no little "windows of opportunity" for other code, say some asynchronous networking routine, to come along at just the wrong instant and find your temporarily-mangled handle.) I don't recommend strncpy over BlockMove unless you lock the handle down first; calling a library routine can move memory and invalidate the pointer you just got from the de-referenced handle. Hope this helps. ========================================================================== Rick Holzgrafe | {sun,voder,nsc,mtxinu,dual}!apple!rmh Software Engineer | AppleLink HOLZGRAFE1 rmh@apple.com Apple Computer, Inc. | "All opinions expressed are mine, and do 20525 Mariani Ave. MS: 3-PK | not necessarily represent those of my Cupertino, CA 95014 | employer, Apple Computer Inc."