[fpc-devel] Dynamic GUI/Console apptype
michalis.kambi at gmail.com
Sat Dec 26 18:19:49 CET 2009
Mimu Bunnylin wrote:
> But suppose I want a program that will continue running in a console if
> run from a console, but will not automatically create a new one if run
> from outside a console.
You can declare a program as $apptype GUI, and then try using standard
file handles. If this succeeds, then the program runs in a console (or
has one of the standard handles explicitly redirected to a pipe etc.).
I'm using such approach to get a little Unix-like behavior under Windows.
This goes like
Handle := GetStdHandle(STD_OUTPUT_HANDLE);
if (Handle <> INVALID_HANDLE_VALUE) and
(Handle <> 0) then
StdOutStream := THandleStream.Create(Handle) else
StdOutStream := nil;
Then write to StdOutStream. In my experiments, you should be prepared
that even when StdOutStream <> nil, still StdOutStream.WriteBuffer may
raise EWriteError, and then you should assume that stdout is not
available (you can free and nil StdOutStream then, as it's useless).
It would probably be possible to extend this approach, such that
standard Pascal Input / Output / ErrorOutput would use these handles
also, so that you could use standard Read[ln], Write[ln]. I was happy
enough with my StdOutStream approach, didn't bother to make Read[ln],
For concrete code that initializes all three streams (for stdin, stdout,
stderr), for both Unix (where it always succeeds) and Windows (using
above approrach), you can take a look at my code, it's at the end of
(look for InitStdStreams procedure). Look also at the comments after
"@section(Variables to read/write standard input/output using TStream
classes." in the interface.
Hope this helps,
More information about the fpc-devel