Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!jarthur!jwinterm From: jwinterm@jarthur.Claremont.EDU (Jim Wintermyre) Newsgroups: comp.sys.mac.programmer Subject: Re: How do you create new resources from within program? Keywords: help Message-ID: <10983@jarthur.Claremont.EDU> Date: 26 Feb 91 11:48:27 GMT References: <22599@hydra.gatech.EDU> Organization: Harvey Mudd College, Claremont, CA 91711 Lines: 69 To: gt4586c@prism.gatech.EDU Subject: Re: How do you create new resources from within program? Newsgroups: comp.sys.mac.programmer In-Reply-To: <22599@hydra.gatech.EDU> Organization: Harvey Mudd College, Claremont, CA 91711 Cc: Bcc: In article <22599@hydra.gatech.EDU> you write: >i'm trying to add a resource to a file being written out by my program. i'm >creating the resource file associated with the data file already written out >and i want to put a 'STR ' resource into it. i think my problem is at the >point of AddResource and the handle to the data. what is the contents of the >handle supposed to be? thanks in advance. > > >-- >thomas willett >Georgia Institute of Technology, Atlanta >gt4586c@prism.gatech.edu >"Violence is the last refuge of the incompetent." - Salvor Hardin (Foundation) The data for a 'STR ' resource should be a Pascal string (length byte at beginning). The StringHandle data type (IM 1-78) is particularly convenient for adding resources of type 'STR '. The following code fragment shows how to add a resource of this type. First you set a variable of type Str255 to whatever it is you want to put into the resource. Then you can use the Toolbox Utilities function NewString (IM 1-468) to allocate space for a StringHandle to the string. A neat feature of this function is that the amount of space it allocates is determined by the length of the string. So, your resource won't take up 256 bytes when the string is only 2 characters long. procedure AddResStr; var theText: Str255; myString: StringHandle; resErr:integer; begin theText:= 'This is how you do it.' myString := NewString(theText); AddResource(Handle(myString), 'STR ', 200, 'String Resource'); resErr := ResError; if resErr <> noErr then DoResError; end; {AddRes} With other resource types it might not be so easy. If the StringHandle data type weren't defined, and there was no NewString function, you'd have to do something more like this. The first thing you would do is create a new handle the size of the string + 1 byte for the length byte. To make it easier to manipulate the data, you could create your own data type to cast the handle to: type myString = array[0..0] of char; {to allow for differing lengths} myStrPtr = ^myString; myStrHandle = ^myPtr; var theStuff:myStrHandle; Then: theStuff := myStrHandle(NewHandle({LENGTH OF STRING + 1})); theStuff^^[0]:=length; {length of string} .... then fill out the rest of the array.... and call AddResource. I hope that this made sense, and that it helped you at least a little. P.S. This is the first time I've used rn, so I hope this makes it back! - Jim Wintermyre