WinConsole (was: Accessing the commandline ... )

From: Self <treutwein.59.Verwaltung.Uni-Muenchen>
Date: Mon, 21 Aug 2000 09:15:23 +0200

> try this. It is based on replies I received to the same question a few
> months ago (sorry, can't remember the respondent's name to attribute it
> properly).
>

> regards,
>
> Bob
>
> MODULE Console; (* Windows specific. Must be linked as a console application
>

Hi Bob,

thanks again for the Console module -- although it does not
handle what I was interested in; it is for special applications
very useful.

But, did it work for you ?
If so I don't understand why. I had some problems getting
it working. Enclosed find a changed version, which - when
called under BlackBox - opens a console and closes it during
unloading.

The startup and test code might need some additional
polishing. I expect that it does not work, if there is already
a console open.
The test routine does not exit under NT, but under Win98.


regards
         Bernhard Treutwein

MODULE WinConsole;

IMPORT SYSTEM, WinApi, Strings;

VAR
 StdIn, StdOut : WinApi.HANDLE;

PROCEDURE Halt;
 VAR res : INTEGER;
 BEGIN
  res := WinApi.GetLastError(); HALT(0)
END Halt;

PROCEDURE Read* ( OUT string : ARRAY OF SHORTCHAR );
 VAR
  n, read : INTEGER;
 BEGIN
  string := "";
  n := LEN( string );
  IF WinApi.ReadFile( StdIn, SYSTEM.ADR(string), n, read, NIL ) = 0 THEN Halt END;
  IF read < n THEN
   string[read] := 0X
  END;
END Read;

PROCEDURE Write* ( IN string : ARRAY OF SHORTCHAR );
 VAR
  n, written : INTEGER;
 BEGIN
  n := LEN( string$ );
  IF WinApi.WriteFile( StdOut, SYSTEM.ADR(string$), n, written, NIL ) = WinApi.FALSE THEN
  Halt END;
END Write;

PROCEDURE Init;
 VAR b : WinApi.BOOL;
 BEGIN
  b := WinApi.AllocConsole();
  IF b = WinApi.FALSE THEN Halt END;
  StdIn := WinApi.GetStdHandle( WinApi.STD_INPUT_HANDLE );
  IF StdIn = WinApi.NULL THEN Halt END;
  StdOut := WinApi.GetStdHandle( WinApi.STD_OUTPUT_HANDLE );
  IF StdOut = WinApi.NULL THEN Halt END;
END Init;

PROCEDURE Close;
 VAR b : WinApi.BOOL;
 BEGIN
  b := WinApi.FreeConsole()
END Close;

PROCEDURE Test*;
 CONST CR = 0DX; LF = 0AX;
 VAR io : ARRAY 80 OF SHORTCHAR; test : ARRAY 80 OF CHAR;
 BEGIN
  Write("Hello BlackBox console !" + CR + LF + "Enter some stuff >" + CR + LF );
  LOOP
   IF (io[0] = 1BX) OR (test$ = "EXIT") OR (test$ = "QUIT") THEN EXIT END;
   Read(io);
   test := io$;
   io := "Got :" + io$;
   Write(io);
   Strings.ToUpper(test$, test)
  END;
END Test;

BEGIN
 Init
CLOSE
 Close
END WinConsole.Test
Received on Mon Aug 21 2000 - 07:15:23 UTC

This archive was generated by hypermail 2.3.0 : Thu Sep 26 2013 - 06:27:45 UTC