In Part 1 of this series, we looked at how to create a configuration file in both Basic MSI and InstallScript projects. In Part 2, we will show to read values from a configuration file in both project types.
In a Basic MSI project, there is no built-in functionality in the Windows Installer that will automatically read values from a configuration file. So, we will have to write a Custom Action (CA), and more specifically, we will want to write an InstallScript CA.
Let's remind ourselves about the configuration file we created in Part 1. Here are the contents of the Mega View.ini file:
[general]
title=Mega View – The Ultimate Viewer
Next, we will go to the Behavior and Logic section, InstallScript view, and create an InstallScript file. In the middle pane, right-click Files and select New Script File. You can name the file setup.rul. Then, we need to create an InstallScript function by adding a prototype and adding the function skeleton. Here is the prototype:
export prototype ReadConfigFile();
We'll call the function ReadConfigFile() and it will have no parameters. Here is the code for the function:
///////////////////////////////////////////////////////////////////////////////
//
// Function: ReadConfigFile()
//
///////////////////////////////////////////////////////////////////////////////Function ReadConfigFile(hMSI)
STRING szPath;
STRING szInstallFolder;
STRING szSectionName;
STRING szKeyName;
NUMBER nResult
begin
// build path to config fileszPath = szInstallFolder ^ "Mega View.ini";
// read string value from config file
szSectionName = "general";
szKeyName = "title";
GetProfString(szPath, szSectionName, szKeyName, szResult);// do something with this value
Do something with szResult;
return ERROR_SUCCESS;
end;
The function is pretty simple. We just build a path to the config file. Then we call GetProfString() to get the value from the file. We set the Section to "general" and the Key to "title", since that was where the value was in the config file we created in Part 1.
Now, we need to call the function by creating a Custom Action. Just to to the Custom Actions and Sequences view under the Behavior and Logic section, in the middle pane right-click Custom Actions and select Custom Action Wizard. We will define a CA with these characteristics:
Function Name: GetConfigFileData
Return Processing: Synchronous (Ignores exit code)
In-Script Execution: Immediate Execution
Execution Scheduling: Always execute
Install UI Sequence: After CostFinalize
We will just call the CA at the beginning of the UI Sequence after CostFinalize since we want to read the value from the config file before we actually do anything in the installation program.
That takes care of the Basic MSI example. The InstallScript example is actually a lot easier. We can just call the GetProfString() function directly from the script.
In the InstallScript view, we will add our code near the beginning of the OnFirstUIBefore event handler, and we'll do it between Dlg_Start and Dlg_SdWelcome. Here is the code:
Dlg_Start:
// build path to config fileszPath = TARGETDIR ^ "Mega View.ini";
// read string value from config file
szSectionName = "general";
szKeyName = "title";
GetProfString(szPath, szSectionName, szKeyName, szResult);// do something with this value
Do something with szResult;
Dlg_SdWelcome:
You see it is basically the same code as in the Basic MSI example. The Section Name is "general" and the Key Name is "title" and the value is stored in the szResult variable. Now realize that you will have to declare the variables earlier in the script. But, you probably knew that already.
That wraps up our example of creating a configuration file in both Basic MSI and InstallScript projects.