showing executable console

Did you ever try to write to the standard output using STDOUT but never seen your output in the console?

This is a little known thing about PE executable on windows that were compiled to be GUI.

The PE header contains information that indicate a mode of execution: console or GUI.

So even if your favorite compiler such as PowerBuilder (classic or .Net) doesn’t support the console application, you can turn it on just by switching 1 byte.

Here is a Perl 5.8+ script that can switch between console/GUI one or more file, (useful in a post-build process when available).

Enable console Perl script Script

use strict;
sub patch{
  my $filename = shift;
  my $mode = shift;
  print "file $filename ";
  open my $FH, '+<', $filename or die "Unable to open file $filename!$/$!$/";
  binmode $FH;
  my ($buffer, $offset);
  read $FH, $buffer, 0x40 or die "Unable to read file $filename $/$!$/";
  die "Not a DOS executable!$/" if substr($buffer,0,2) ne 'MZ' ;
  $offset = unpack( 'L', substr($buffer,0x03C,4) ) or die "Invalid PE address!";
  seek ($FH,  $offset,  0) or die "Unable to move at byte $offset in file $filename $!$/";
  read $FH, $buffer, 0x2 or die "Unable to read file $filename $/$!$/";
  die "Invalid PE format!$/" if substr($buffer,0,2) ne 'PE' ;
  $offset += 0x5C;
  seek ($FH,  $offset,  0) or die "Unable to move at byte $offset in file $filename $!$/";
  print $FH pack('c', $mode);
  close $FH;
  print " => OK.$/";
print <<USAGE unless @ARGV;
  $0 [-mode=gui|console] file1 file2 file3...
  The default mode is set to console
my $mode = 0x03;
    die "Unknow mode $1!$/" if $1 ne 'gui' && $1 ne 'console';
    print "set mode to $1$/";
    $mode = 0x02 if $1 eq 'gui';
  foreach my $f ( glob $_ ){
    patch $f, $mode;

Turn to console all executable of the current folder:

perl "*.exe"

Turn to GUI an executable:

perl --mode=gui "..\build\my_app.exe"

You can try to switch IDE so it will display it console (helpfull to debug without compiling):

perl "C:\Program Files\Sybase\PowerBuilder 10.5\PB105.EXE"

BE CAREFUL: if you close the console, it will kill your application too with no way to cancel or save your pending data!

MIT LICENSE: you have a brain, take your responsibility, and do backup yourself!

If you wonder how your Powerbuilder application can write to console: