script/exe_update.pl

filter: Statementstmt 85.7% 18 / 21 Branchbran 21.4% 3 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 64.1% 25 / 39 CC 1 SCAR 0.0
CC
1
Cov
60%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Win32/Exe/ResourceEntry.pm ↑ Summary lib/Win32/Exe/DataDirectory.pm »
1 #!/usr/local/bin/perl
2 # Copyright 2004, 2006, 2010 by Audrey Tang <cpan@audreyt.org>
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Win32::Exe;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 exe_update.pl - Modify windows executable files
12  
13 =head1 SYNOPSIS
14  
15 B<exe_update.pl> S<[ B<--gui> | B<--console> ]> S<[ B<--icon> I<iconfile> ]>
16 S<[ B<--manifest> I<manifestfile> ]>
17 S<[ B<--info> I<key=value;...> ]> I<exefile>
18  
19 =head1 DESCRIPTION
20  
21 This program rewrites PE headers in a Windows executable file. It can
22 change whether the executable runs with a console window, as well as
23 setting the icons, manifest and version information associated with it.
24 In general, a PE file must have an existing resource section or you
25 cannot add icons, manifests or version info. However, on Win32 platforms
26 a new resource section will be created if none exists.
27  
28 =head1 OPTIONS
29  
30 Options are available in a I<short> form and a I<long> form. For
31 example, the three lines below are all equivalent:
32  
33 % exe_update.pl -i new.ico input.exe
34 % exe_update.pl --icon new.ico input.exe
35 % exe_update.pl --icon=new.ico input.exe
36  
37 =over 4
38  
39 =item B<-c>, B<--console>
40  
41 Set the executable to always display a console window.
42  
43 =item B<-g>, B<--gui>
44  
45 Set the executable so it does not have a console window.
46  
47 =item B<-i>, B<--icon>=I<FILE>
48  
49 Specify an icon file (in F<.ico>, F<.exe> or F<.dll> format) for the
50 executable.
51  
52 =item B<-m>, B<--manifest>=I<FILE>
53  
54 Specify a manifest file in F<.xml> format for the
55 executable.
56  
57 =item B<-N>, B<--info>=I<KEY=VAL>
58  
59 Attach version information for the executable. The name/value pair is
60 joined by C<=>. You may specify C<-N> multiple times, or use C<;> to
61 link several pairs.
62  
63 These special C<KEY> names are recognized:
64  
65 Comments CompanyName FileDescription FileVersion
66 InternalName LegalCopyright LegalTrademarks OriginalFilename
67 ProductName ProductVersion
68  
69 =item B<-A>, B<--manifestargs>=I<KEY=VAL>
70  
71 As an alternative to specifying a manifest file, specify manifest attributes.
72 The name/value pair is joined by C<=>. You may specify C<-A> multiple times,
73 or use C<;> to link several pairs. This option may be preferable to using
74 a manifest file as these attributes will be combined with any existing
75 manifest that may be in the executable.
76  
77 These special C<KEY> names are recognized:
78  
79 ExecutionLevel UIAccess ExecName Description
80 CommonControls Version
81  
82 The CommonControls key is a simple boolean value to indicate that the
83 Common Controls Version 6.0.0.0 dependency should be added to the manifest.
84  
85 e.g
86  
87 --manifestargs="ExecutionLevel=requireAdministrator;ExecName=My.App;CommonControls=1"
88 --manifestargs="Version=1.3.6.7895;UIAccess=false"
89  
90 =back
91  
92 =cut
93  
94 1 my $Options = {};
95 1 Getopt::Long::GetOptions( $Options,
96 'g|gui', # No console window
97 'c|console', # Use console window
98 'i|icon:s', # Icon file
99 'm|manifest:s', # manifest file
100 'N|info:s@', # Executable header info
101 'A|manifestargs:s@' # manifest arguments
102 );
103  
104 1 ▶my $exe = shift or die "Usage: " . basename($0) .
Branch: 1 branch
BranchTrueFalseTotal
my $exe = shift @ARGV or die 'Usage: ' . basename($0) . " [--gui | --console] [--icon file.ico] [--manifest file.xml] [--info key=value] [--manifestargs key=value ] file.exe\n" 1 0 1
105 " [--gui | --console] [--icon file.ico] [--manifest file.xml] [--info key=value] [--manifestargs key=value ] file.exe\n";
106  
107 1 ▶my $exec = Win32::Exe->new($exe) or die "Unable to open file $exe";
Branch: 1 branch
BranchTrueFalseTotal
my $exec = "Win32::Exe"->new($exe) or die "Unable to open file $exe" 1 0 1
108  
109 1 ▶if(!$exec->has_resource_section) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exec->has_resource_section) 0 1 1
110 0 ▶ die("Cannot create new resource section on this platform. Requires Win32") if $^O !~ /^mswin/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $^O =~ /^mswin/i 0 0 0
111 0 ▶ $exec->create_resource_section or die("Failed to create new resource section");
Branch: 1 branch
BranchTrueFalseTotal
$exec->create_resource_section or die "Failed to create new resource section" 0 0 0
112 0 ▶ $exec = Win32::Exe->new($exe) or die "Unable to open file $exe";
Branch: 1 branch
BranchTrueFalseTotal
$exec = "Win32::Exe"->new($exe) or die "Unable to open file $exe" 0 0 0
113 }
114  
115 $exec->update(
116 gui => $Options->{g},
117 console => $Options->{c},
118 icon => $Options->{i},
119 info => $Options->{N},
120 manifest => $Options->{m},
121 manifestargs => $Options->{A},
122 1 ▶) or die "Update of $exe failed!\n";
Branch: 1 branch
BranchTrueFalseTotal
$exec->update("gui", $Options->{"g"}, "console", $Options->{"c"}, "icon", $Options->{"i"}, "info", $Options->{"N"}, "manifest", $Options->{"m"}, "manifestargs", $Options->{"A"}) or die "Update of $exe failed!\n" 0 0 0
123  
124 __END__
« lib/Win32/Exe/ResourceEntry.pm ↑ Summary lib/Win32/Exe/DataDirectory.pm »