4 # All the IMAGE_* structures are defined in the WINNT.H file
5 # of the Microsoft Platform SDK.
7 my %subsys = (NATIVE => 1,
13 unless (0 < @ARGV && @ARGV < 3) {
14 printf "Usage: $0 exefile [%s]\n", join '|', sort keys %subsys;
18 $ARGV[1] = uc $ARGV[1] if $ARGV[1];
19 unless (@ARGV == 1 || defined $subsys{$ARGV[1]}) {
20 (my $subsys = join(', ', sort keys %subsys)) =~ s/, (\w+)$/ or $1/;
21 print "Invalid subsystem $ARGV[1], please use $subsys\n";
25 my ($record,$magic,$signature,$offset,$size);
26 open EXE, "+< $ARGV[0]" or die "Cannot open $ARGV[0]: $!\n";
29 # read IMAGE_DOS_HEADER structure
30 read EXE, $record, 64;
31 ($magic,$offset) = unpack "Sx58L", $record;
33 die "$ARGV[0] is not an MSDOS executable file.\n"
34 unless $magic == 0x5a4d; # "MZ"
36 # read signature, IMAGE_FILE_HEADER and first WORD of IMAGE_OPTIONAL_HEADER
38 read EXE, $record, 4+20+2;
39 ($signature,$size,$magic) = unpack "Lx16Sx2S", $record;
41 die "PE header not found" unless $signature == 0x4550; # "PE\0\0"
43 die "Optional header is neither in NT32 nor in NT64 format"
44 unless ($size == 224 && $magic == 0x10b) || # IMAGE_NT_OPTIONAL_HDR32_MAGIC
45 ($size == 240 && $magic == 0x20b); # IMAGE_NT_OPTIONAL_HDR64_MAGIC
47 # Offset 68 in the IMAGE_OPTIONAL_HEADER(32|64) is the 16 bit subsystem code
48 seek EXE, $offset+4+20+68, 0;
51 my ($subsys) = unpack "S", $record;
52 $subsys = {reverse %subsys}->{$subsys} || "UNKNOWN($subsys)";
53 print "$ARGV[0] uses the $subsys subsystem.\n";
56 print EXE pack "S", $subsys{$ARGV[1]};
63 exetype - Change executable subsystem type between "Console" and "Windows"
67 C:\perl\bin> copy perl.exe guiperl.exe
68 C:\perl\bin> exetype guiperl.exe windows
72 This program edits an executable file to indicate which subsystem the
73 operating system must invoke for execution.
75 You can specify any of the following subsystems:
81 The CONSOLE subsystem handles a Win32 character-mode application that
82 use a console supplied by the operating system.
86 The WINDOWS subsystem handles an application that does not require a
87 console and creates its own windows, if required.
91 The NATIVE subsystem handles a Windows NT device driver.
95 The WINDOWSCE subsystem handles Windows CE consumer electronics
100 The POSIX subsystem handles a POSIX application in Windows NT.
106 Jan Dubois <jand@activestate.com>