Executing external modules


Revision as of 20:26, 3 April 2009 by Markus (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search
This article is currently work in progress. You can help to improve the ISXKB by extending this article.



This article covers the execution of an external module from Inno Setup as defined in the lpApplicationName parameter of the CreateProcess () Windows API function. The Windows API function GetBinaryType () can be consulted to find out if a file can be executed or not.

On Windows, executable modules usually have one of the file name extensions .exe, .com, or .scr. A text file for example (file name extension .txt) is not an executable module. To open a file with a particular file name extension with its associated executable module have a look at Shell executing external files.

The [Run] section

The easiest way to execute an external program from Inno Setup is to create a [Run] section entry. This example has been taken from Inno Setup's help file:

Filename: "{app}\INIT.EXE"; Parameters: "/x"

To make it easier for you Inno Setup Help - 'Run' and 'UninstallRun' sections contains a copy of Inno Setup's help entry '[Run] & [UninstallRun] sections'. Since it may not be up-to-date it's far better to refer to Inno Setup's help file.

Note that a common bug in applications is that they require configuration files or direct link libraries (DLLs) in the same directory as the executable module but fail to retrieve their own path properly before using these files. Application considerations shows both, a way to work around this problem and how to fix it completely.

There are many reasons why an installer would want to execute external programs, the most common one probably being 'Launch My Program' at the end of a successful installation.

The picture has been created with the script wizard. The required bits from the script are here:

#define MyAppName "My Program"
#define MyAppExeName "MyProg.exe"

Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#MyAppName}}"; Flags: nowait postinstall skipifsilent

The flag nowait specifies that Setup can carry on without waiting for the external process to terminate. In this case the installer would terminate as soon as the user presses the 'Finished' button.

The flag postinstall ensures that the entry is created on that page (the 'Finished' page).

The flag skipifsilent is actually what is forgotten very often. If the installer is executed in silent mode (/SILENT or /VERYSILENT parameters) the application will not be started at the end of the installation - which is exactly how at least 99.9999 percent of users and administrators like and want it. Don't use the flag skipifsilent to take care of that one user who wants it to annoy the 999,999 others (Installation considerations, postinstall).

The compiler's Exec () function

Although any executable module can be executed with a [Run] section entry, there are cases where this is not sufficient enough. For example, it's not possible to catch the return code (exit code) from an external process, and the [Run] section has its place in Inno Setup's installation order.

Both issues can be addressed by using Inno Setup's Exec () function in a [Code] section.

A preliminary quick note on the help file:

To find the Exec () function in Inno Setup's help file search for 'Exec'. When the preprocessor is installed both help files are merged, and the help entry 'Exec function' is the one for the preprocessor (ISPP = Inno Setup Preprocessor).

'Exec': Inno Setup's Exec () function. This will be executed at runtime.

'Exec function': ISPP's Exec () function. This will be executed at compile time.

The compiler's Exec () function declaration:

function Exec(const Filename, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; var ResultCode: Integer): Boolean;

A more detailed description of the Exec () function can be found in Inno Setup's help file online (Exec).

To run 'MyExecutable.exe /par1 /par2' as the first step of the installation use the following code:

procedure CurStepChanged (CurStep: TSetupStep);
   WorkingDir:   String;
   ReturnCode:   Integer;
   if (ssInstall = CurStep) then
     WorkingDir := ExpandConstant ('{tmp}');
     Exec ('MyExecutable.exe', '/par1 /par2', WorkingDir, SW_SHOW, ewWaitUntilTerminated, ReturnCode);

This executes MyExecutable.exe for instance before the [Files] section entries are processed.

Executing Windows command line scripts

By using the Inno Setup constant {cmd} it's not more complicated to run a Windows command line script.

ExtractTemporaryFile ('MyBatchFile.cmd');
Exec (ExpandConstant ('{cmd}'), '/C MyBatchFile.cmd', ExpandConstant ('{tmp}'), SW_SHOW, ewWaitUntilTerminated, ReturnCode);

The preprocessor's Exec() function

See also

External links

Personal tools