by Jon Ripley, January 2007, April 2007

The following code demonstrates how to write a console mode BBC BASIC program that will also run from the IDE:

      IF FNInConsoleMode THEN
        SYS "GetStdHandle",-10 TO @hfile%(1):*INPUT 13
        SYS "GetStdHandle",-11 TO @hfile%(2):*OUTPUT 14
        SYS "SetConsoleMode",@hfile%(1),0
      ENDIF
 
      PRINT "Hello world!"'
      INPUT "What is your name ",name$
      PRINT '"Hello ";name$"."
 
      IF FNInConsoleMode THEN QUIT

At the start of the program we check the current environment the program is running in and only redirect I/O to the console if the program has been compiled - assuming that the program will be compiled with the 'Console mode' option set. When running in console mode you must end your program using QUIT. The code for FNInConsoleMode is at the end of this article.

Similar code is used to write CGI (Common Gateway Interface) programs in BBC BASIC program that will also run from the IDE:

      IF FNInConsoleMode THEN SYS "GetStdHandle",-11 TO @hfile%(2):*OUTPUT 2
 
      PRINT "Content-type: text/plain"
      PRINT
      PRINT "Hello world!"
 
      IF FNInConsoleMode THEN SYS "SetEndOfFile", @hfile%(2):QUIT

The differences here are that we do not need to redirect program input and CGI programs must close the output file handle before quitting.

Routines

      DEF FNInConsoleMode
      LOCAL base%
      SYS "GetModuleHandle", 0 TO base%
      = (base%?(base%!60+92) = 3)