Microsoft KB Archive/42464

From BetaArchive Wiki
Knowledge Base


OpenResFile; Resources May Save Incorrectly in Interpreter

Article ID: 42464

Article Last Modified on 1/9/2003



APPLIES TO

  • Microsoft QuickBasic Compiler for Macintosh 1.0



This article was previously published under Q42464

SUMMARY

If you are using the OpenResFile statement (to open external resource files) when developing a program in the interpreter in Macintosh QuickBASIC, you must invoke CloseResFile whenever the program ends. This includes when Stop is selected from the File menu or COMMAND+PERIOD is pressed. (Using CloseResFile like this is not required in compiled programs.)

If external resource files are left open in an interpreted program, the QuickBASIC environment may place resources in the wrong files when you Compile or Save. For example, the MBCO resource, the compiler options specific to a source file, may be attached to the wrong file (as discussed in a separate article in this KnowledgeBase; query for MBCO).

This behavior is a negative side effect of the designed feature to leave the resource file open during single-stepping (debugging) in the interpreter.

You should invoke an ON BREAK GOSUB handler to make sure CloseResFile is performed when the program breaks, as shown at the bottom of this article. (An alternative is to always manually execute CloseResFile in the COMMAND window before doing a Save or Compile in the QuickBASIC environment.)

MORE INFORMATION

Before a program does an END, STOP, RUN, CHAIN, or SubLaunch in the interpreter, you should always close external resource files that you have explicitly opened with OpenResFile.

You should include an ON BREAK handler to automatically close externally opened resource files when you press COMMAND+PERIOD (or select Stop from the File menu). Below is an outline to follow:

ON BREAK GOSUB breakTrap
BREAK ON
' Program code goes here ...
' ... OpenResFile, et cetera ... end of code.
GOTO breakTrap  ' At the end of the program, close resource files.
END
breakTrap:
   ' Close all open libraries (if any) opened with LIBRARY statement:
   LIBRARY CLOSE
   ' Do the following for every resource file you have opened. This
   ' code checks to make sure the given resource file is NOT the
   ' program source or the currently running file then closes the
   ' resource file. ref% is the number returned by OpenResFile.
   ' PEEKW(&H900) gets the value in Macintosh global CurApRefNum,
   ' which is the file reference number of the currently running
   ' application:
   IF ref% <> SYSTEM(7) AND ref% <> PEEKW(&H900) THEN
       CloseResFile ref%
   END IF
   STOP
                

The following is a complete example of using OpenResFile to open an external resource located in the Demo Resources file (located in the QB Demos folder on the Examples disk):

   ON BREAK GOSUB breakTrap : BREAK ON
   DIM r%(3)
   ref% = 0
   SetRect r%(0),10,10,42,42  'Sets rectangle to draw icon in.
   h& = 0
   '   get the file reference number:
   OpenResFile "Examples:QB Demos:Demo Resources",ref%
   n% = 5
   GetIcon ref%, n%, h&
   DrawIcon r%(0), h&
   ReleaseRes h&
   CloseResFile ref%
   END
breakTrap:  ' Here is the safest way to do CloseResFile:
   IF ref% <> SYSTEM(7) AND ref% <> PEEKW(&H900) THEN
      CloseResFile ref%
   END IF
   STOP   ' Stops the program
                


Additional query words: MQuickB

Keywords: KB42464