VerSearchAndUpdateFile Example

InstallShield 2015 ยป InstallScript Language Reference

Note: To call this function in a Basic MSI setup, you must first create a custom action for the entry-point function, execute the custom action in a sequence or as the result of a dialog's control event, and then build the release.

/*--------------------------------------------------------------*\

*

* InstallShield Example Script

*

* Demonstrates the VerSearchAndUpdateFile function.

*

* The first call to VerSearchAndUpdateFile replaces the file

* specified by the constant UPDATE_FILE1 regardless of the

* version on the target system.

*

* The second call to VerSearchAndUpdateFile replaces the file

* specified by the constant UPDATE_FILE2 only if the version

* in the source directory is newer than the version in the

* target directory.

*

\*--------------------------------------------------------------*/

 

#define UPDATE_FILE1  "Example.txt"

#define UPDATE_FILE2  "Readme.txt"

 

// Include Ifx.h for built-in InstallScript function prototypes.

#include "Ifx.h"

 

export prototype ExFn_VerSearchAndUpdateFile(HWND);

 

function ExFn_VerSearchAndUpdateFile(hMSI)

    STRING szFileName, svInstalledFile, szTitle, szMsg;

    NUMBER nUpdateFlag, nResult;

    BOOL   bDone;

begin

 

    // Set up title and message parameters for call

    // to VerSearchAndUpdateFile.

    szTitle = "VerSearchAndUpdateFile Example";

    szMsg   = " was successfully updated.";

 

    // Update UPDATE_FILE1 regardless of version number.

    if (VerSearchAndUpdateFile (UPDATE_FILE1, VER_UPDATE_ALWAYS,

                               svInstalledFile) = 0) then

        SprintfBox (INFORMATION, szTitle, UPDATE_FILE1 + szMsg);

    endif;

 

    // Set indicator to control loop exit.

    bDone = FALSE;

 

    // Begin the while loop.

    while (bDone = FALSE)

        // Update UPDATE_FILE2 only if existing file is older.

        nResult = VerSearchAndUpdateFile (UPDATE_FILE2, VER_UPDATE_COND,

                                          svInstalledFile);

        switch (nResult)

            case 0:

                // VerSearchAndUpdate successful.

                SprintfBox (INFORMATION, szTitle, UPDATE_FILE2 + szMsg);

                bDone = TRUE;

            // The target file does not have a version number.

            case FILE_NO_VERSION:

                // Ask the user if the file should be updated regardless.

                if (AskYesNo ("Version number was not found.\nDo you still wish " +

                             "to update " + UPDATE_FILE2 + "?", YES) = YES) then

                    // Update the file UPDATE_FILE2 regardless of version number.

                    VerSearchAndUpdateFile (UPDATE_FILE2, VER_UPDATE_ALWAYS,

                                           svInstalledFile);

                    bDone = TRUE;

                else

                    bDone = TRUE;

                endif;

 

            // The target file is locked.

            case FILE_IS_LOCKED:

                MessageBox ("The target file is locked.\n\nPlease close all " +

                           "programs and run Setup again.", INFORMATION);

                bDone = TRUE;

            // The target file is read-only.

            case FILE_RD_ONLY:

                // Ask the user if Setup should remove the read-only attribute.

                if (AskYesNo ("File is read-only.\nShould Setup remove the " +

                             "write-protection of " + UPDATE_FILE2 + "?", YES) = YES) then

                    // Change the attribute of the target file to normal.

                    SetFileInfo (svInstalledFile, FILE_ATTRIBUTE, FILE_ATTR_NORMAL, "");

                    bDone = FALSE;

                else

                    bDone = TRUE;

                endif;

            // The target disk does not have enough space.

            case OUT_OF_DISK_SPACE:

                MessageBox ("You need more free space for this update.", SEVERE);

                bDone = TRUE;

            // The required VER.DLL file was not found.

            case VER_DLL_NOT_FOUND:

                MessageBox ("VER.DLL was not found.", SEVERE);

                bDone = TRUE;

            // Some other error occurred.

            case OTHER_FAILURE:

                MessageBox ("Update has failed.", SEVERE);

                bDone = TRUE;

            default:

                bDone = TRUE;

        endswitch;

 

    endwhile;

 

end;