InstallShield 2010, InstallScript, Directory Properties and

16
InstallShield 2010, InstallScript, Directory Properties and Windows Environment Variables By David Roossien [email protected] updated 4/2010 That’s quite a title, but this tutorial explains how to use all of these. I hope it helps you create an installer that works for you. The task: Create a Basic MSI installer using InstallShield 2010 (or 12) that reads a Windows Environment Variable to determine where to install some files. This sounds like a simple task, but it sure helps to have a 16 page example. The approach: Create a Directory Property that we can tie to the Destination computer’s folder in Application Data- >Files and Folders. We will use InstallScript to get the value of an environment variable from the Destination computer. During installation we will change the value of the Directory Property using InstallScript. When we change the value of the Directory Property the Destination for that folder will change dynamically during runtime. Example project (test): 1. In Windows Explorer, create a folder called c:\test. 2. In InstallShield 2010 (or 12). Create a new Basic MSI project. I called mine test and put it in c:\test\test. 3. Create a text file called test.txt. Place it in c:\test\test. 4. Go to the Installation Designer Tab. 5. Go to Organization->Setup Design.

Transcript of InstallShield 2010, InstallScript, Directory Properties and

Page 1: InstallShield 2010, InstallScript, Directory Properties and

InstallShield 2010, InstallScript, Directory Properties and Windows Environment Variables

By David [email protected]

updated 4/2010

That’s quite a title, but this tutorial explains how to use all of these. I hope it helps you create an installer that works for you.

The task:Create a Basic MSI installer using InstallShield 2010 (or 12) that reads a Windows Environment Variable to determine where to install some files. This sounds like a simple task, but it sure helps to have a 16 page example.

The approach:Create a Directory Property that we can tie to the Destination computer’s folder in Application Data->Files and Folders.

We will use InstallScript to get the value of an environment variable from the Destination computer. During installation we will change the value of the Directory Property using InstallScript. When we change the value of the Directory Property the Destination for that folder will change dynamically during runtime.

Example project (test):

1. In Windows Explorer, create a folder called c:\test.

2. In InstallShield 2010 (or 12). Create a new Basic MSI project. I called mine test and put it in c:\test\test.

3. Create a text file called test.txt. Place it in c:\test\test.

4. Go to the Installation Designer Tab.

5. Go to Organization->Setup Design.

Page 2: InstallShield 2010, InstallScript, Directory Properties and

6. Right click on Setup Design and click New Feature. Call it “FilesFeature”. I called mine “FilesFeature” because all this simple installer does is copy files onto the target machine.

7. Change the Display Name to “FilesFeature” as well.

8. IMPORTANT: Change the Destination from “[INSTALLDIR] to {temp}.

a. This creates a Directory Property called DIRPROPERTY1 with a value of {temp}.

b. This is represented in the Destination box by {DIRPROPERTY1}{temp}

c. You can verify that a Directory Property was created by going to Behavior and Logic->Property Manager. You should see {DIRPROPERTY1}{temp}

Figure 1 FilesFeature with a Destination that has been changed from [INSTALLDIR] to a directory property called DIRPROPERTY1 with a value of {temp}

Page 3: InstallShield 2010, InstallScript, Directory Properties and

Figure 2 DIRPROPERTY1 with a value of {temp}

9. Go to Behavior and Logic->Property Manager. Locate DIRPROPERTY1 and change its value to the folder c:\test. This will now be the default Destination folder for FilesFeature.

Page 4: InstallShield 2010, InstallScript, Directory Properties and

Figure 3 Property Manager with Directory Property

10. Save your project.

11. Go back to Setup Design. You should see that {temp} has been replaced with the value c:\test.

Page 5: InstallShield 2010, InstallScript, Directory Properties and

Figure 4 Setup Design with Destination changed

12. Go to Application Data->Files and Folders.

13. Right click on Destination Computer under Destination computer’s folders. Select “Launch Component Wizard”.

14. Click Next. For Destination, select the Directory Property you created before.

Page 6: InstallShield 2010, InstallScript, Directory Properties and

Figure 5 Launch Component Wizard: Destination

Page 7: InstallShield 2010, InstallScript, Directory Properties and

15. Next. Click Add Files. Navigate to c:\test\test\test.txt. Open.

Figure 6 Launch Component Wizard: Files

16. Next.

Page 8: InstallShield 2010, InstallScript, Directory Properties and

Figure 7 Launch Component Wizard: Finish

17. Finish.

18. Now you should see a folder called c:\test with an “AllOtherFiles” component. Test.txt should be listed with that component.

Page 9: InstallShield 2010, InstallScript, Directory Properties and

Figure 8 Files and Folders

19. If you return to Organization->Setup Design you should see “AllOtherFiles” listed below the FilesFeature we created earlier.

Page 10: InstallShield 2010, InstallScript, Directory Properties and

Figure 9 Launch Component Wizard: Files

20. Save your project.

21. Now, we are close to adding our InstallScript, but first we still should go down to Media->Releases and make a New Product Configuration. For this example, we will create a product configuration that builds a single compressed setup.exe. Our file, test.txt, will be compressed inside the setup.exe.

22. Go to Media->Releases. Right click on Releases and select “New Product Configuration”. Accept “Product Configuration 1” as the name.

23. Right click on “Product Configuration 1” and select “New Release”. Accept “Release 1” as the name.

24. Click on Release 1. On the Build tab, under Compression, select “Compressed”.

25. Save your project.

Page 11: InstallShield 2010, InstallScript, Directory Properties and

InstallScript

Before continuing onto programming the InstallScript we will assume that the Destination computer will have a Windows System environment variable called “test”. Its value will be c:\test or any other folder you wish to designate.

1. Go to Behavior and Logic. Right click on Files below InstallScript. Select “New Script Files”. This will create setup.rul below Files and MyFunction below Functions.

2. Below Functions, click on MyFunction. This will open setup.rul in the editor.

3. Create the following method below MyFunction:

#define FXN_NAME "setInstallFolder"

#define ENV_VAR "test" // must exist on the target machine

#define SUB_FOLDER "\\testsubfolder " //sub folder to concatenate

export prototype setInstallFolder(HWND);

///////////////////////////////////////////////////////////////////////////////

// Function: setInstallFolder

//

// Purpose: This function reads a Windows environment variable, concatenates

// it with the string "SUB_FOLDER", and uses it to change the

// Directory Property that is tied to the installation folder.

//

///////////////////////////////////////////////////////////////////////////////

function setInstallFolder(hMSI)

Page 12: InstallShield 2010, InstallScript, Directory Properties and

NUMBER nvSize, nReturnValue;

STRING svEnvVar, svNewValue;

begin

// Get the value of the environment variable specified by ENV_VAR

if (GetEnvVar (ENV_VAR, svEnvVar) < 0) then

// Report the error.

MessageBox ("Environment variable " + ENV_VAR + " not found. Please create it and set its value before running this setup program..", SEVERE);

else

// Display the value of the environment variable

SprintfBox (INFORMATION, FXN_NAME, "%s = %s", ENV_VAR, svEnvVar);

// concatenate the value of the env variable and sub folder. This will be the Destination

// for our installation

svNewValue = svEnvVar + SUB_FOLDER;

// set the value of DIRPROPERTY1

if( MsiSetProperty(hMSI, "DIRPROPERTY1", svNewValue) == ERROR_SUCCESS ) then

SprintfBox(INFORMATION, "DIRPROPERTY1 set = %s", svNewValue);

endif;

Page 13: InstallShield 2010, InstallScript, Directory Properties and

endif;

end;

4. Save your project. Right click on setup.rul and select “Compile”.

5. Go to Behavior and Logic->Custom Actions and Sequences. Right click on Custom Actions. Select “Custom Action Wizard”. Next. For Name, give it “setInstallFolder”.

Figure 10 Custom Action Wizard: Basic Information

6. Next. Under type, select “Run InstallScript code”. Next.

7. Under Source, select “setInstallFolder”. Next.

Page 14: InstallShield 2010, InstallScript, Directory Properties and
Page 15: InstallShield 2010, InstallScript, Directory Properties and

Figure 11 Custom Action Wizard: Action Parameters

8. Next. Next. Next. On the Insert Into Sequence dialog, under Install Execute Sequence, select “After InstallInitialize” (we will change this in a minute). Next. Finish.

9. Select the setInstallFolder Custom Action you just made and make the following changes:

a. Change the In-Script Execution to “Immediate Execution”.

b. Change the Execution Scheduling to “Always Execute”

c. Change Install Exec Sequence to “<First Action>”

Page 16: InstallShield 2010, InstallScript, Directory Properties and

Figure 12 Custom Action: setInstallFolder parameters

10. Save your project.

11. Go to top menu. Select Build->Build.

12. Copy Setup.exe from C:\test\test\Product Configuration 1\Release 1\DiskImages\DISK1 onto your test environment, set your “test” environment variable and execute the setup program.

13. I like to use a virtual machine for testing installations. This allows me to safely install and use Add/Remove Programs to uninstall the application.

ConclusionI hope this example helps you get started with InstallShield, InstallScript, properties and environment variables. Let me know if it does!