Example of SaveString, GetString, LoadString Library Routines (59286)






This article was previously published under Q59286

SUMMARY

The following program example shows how to invoke SaveString, LoadString, and GetString, which are QuickBASIC Toolbox library (MBLC) routines described on Pages 484, 487, and 492 of the "Microsoft QuickBASIC for Apple Macintosh: Language Reference" manual.

MORE INFORMATION

SaveString, LoadString, and GetString can only use strings up to 255 characters in length.

Note that SaveString, LoadString, and GetString require calls to other library (MBLC) routines (such as OpenResFile, CloseResFile) in order to be used.

SaveString does NOT overwrite (replace) an existing resource (if any) that has the same ID number. Instead, SaveString places another resource with the same ID in the file. This poses a problem with running the example below more than once, since a duplicate resource with ID=0 is added to the resource file each time. You can avoid writing two resources with the same ID number in a resource file by executing GetRes to get the handle of the existing resource, then executing RemoveRes to delete it before saving the new resource. Or, you can simply delete the resource file and make a new one. The OpenResFile statement automatically creates a new resource file if the specified file does not exist. If the file exists, OpenResFile opens its resource fork.

Code Example

ref% = 0       'Must initialize ref% and id%, otherwise you'll get a
id% = 0        ' "TYPE MISMATCH" error
a$ = "This is a test of SaveString"
openresfile "stringres",ref% 'If the file "stringres" doesn't exist,
                             'it is created and assigned a ref% number

'The following SaveString statement saves a$ in the resource file as a
'named resource, "teststring". (Giving a name is optional.)
'(ResEdit can later view the string stored in the resource file):
savestring ref%,id%,a$,"teststring"

closeresfile ref%     'Close "stringres" resource file
WHILE MOUSE(0)<>1:WEND
b$=""
ref% = 0
id% = 0
openresfile "stringres", ref%   'Opens "stringres" resource file

' 1. Here is one way to retrieve the string:
loadstring ref%,id%,b$
PRINT b$    ' Prints the string retrieved with LoadString

' 2. Here is another way to retrieve the string:
getstring ref%,id%,h&        'Gets string resource with id% = 0 and
                             'assigns it the handle h&
drawstring h&amp;    'Draws the string associated with handle h&

closeresfile ref%
END
				

Modification Type: Minor Last Reviewed: 1/8/2003
Keywords: KB59286