cdesnoyers
Posts: 6
Joined: Fri Apr 14, 2006 8:18 pm

Conditional feature install ?!?

Hi there,

Is there a way to use the serial number (entered by the user) to control which feature is available/visible (Install Level) in the Custom option of the Setup Type dialog ?

I've tried everything I can thing of, from using the validation DLL, custom actions and I cant seem to control what is seen in the Setup Type dialog box.

This would be like the MS Office suite, depending on your CD key... you get such and such features available for installation.

Thanks,
Chris
gigi
Posts: 2103
Joined: Tue Apr 11, 2006 9:55 am
Contact: Website

Hi,
Is there a way to use the serial number (entered by the user) to control which feature is available/visible (Install Level) in the Custom option of the Setup Type dialog ?
Yes you can easily use serial number to control which feature is available/visible with Advanced Installer.

If you want to learn more about what method to use with Serial Number Validation follow this link: http://www.advancedinstaller.com/user-g ... ation.html

However I suggest you to use a DLL for serial number validation. Here it is an example on how to use DLL for serial number validation:
http://www.advancedinstaller.com/user-g ... n-dll.html

For each Feature (defined in the "Organization" page) that you want to be displayed depending on serial number define a Property on "Install Parameters" page with a value of "TRUE" or "FALSE", you will modify those values in Validation function (DLL function) depending on serial number you type.

At the "Organization" page for each feature on "Install Level" choose a condition like this:

Code: Select all

Property_Name = "TRUE"
The value in "Install Level" combo box is used if the condition result is FALSE otherwise is used the value defined with the condition.

Set a Property value to "TRUE" if you want to display the Feature associated with the Property otherwise set Property value to "FALSE".
You can set a value for a Property like this, where PropValue can be a value of "TRUE" or "FALSE":

Code: Select all

::MsiSetProperty(hInstall, _T("Property_Name"), PropValue);


Hope this helps you.
All the best,
Gigi
_________________
Gheorghe Rada
Advanced Installer Team
http://www.advancedinstaller.com
Last edited by gigi on Thu Apr 20, 2006 12:24 pm, edited 1 time in total.
cdesnoyers
Posts: 6
Joined: Fri Apr 14, 2006 8:18 pm

Hi,

Thanks for the reply.
What you've explained is what I had understood from the documentation and samples... unfortunately it doesnt seem to work.
Checking the log file I can see the property change:

MSI (c) (6C!44) [11:38:24:342]: PROPERTY CHANGE: Modifying Consultation property. Its current value is 'FALSE'. Its new value: 'TRUE'.

But the conditonal check doesnt change. By default, the level is at zero, I check to see if the property is set to TRUE to set it to 1. I've tried with and without quotes around TRUE with no luck.

What am I missing here ?

Thanks,
Chris
Ionut
Posts: 605
Joined: Tue Nov 22, 2005 11:29 am
Contact: Website

Hi Chris,

The Feature conditions (stored in the "Condition" table inside the MSI database) are processed during the "CostFinalize" action. After this stage, any changes in the conditional expression evaluation do not affect the InstallLevel of the Features.

The serial validation DLL Custom Action is invoked after the "CostFinalize" action (by a ControlEvent of the "Next" button from the "UserRegistrationDlg" dialog). Therefore, the InstallLevel of the Features is not affected anymore by the modification of any Property values.

I have used a different approach. Depending on the serial entered by the user, the Custom Action will set the state of certain Features (those that will be unavailable) to INSTALLSTATE_ABSENT (which means that those Features will not be installed). Furthermore, it is also necessary to hide those Features from the "SelectionTree" control in the "CustomizeDlg" dialog, such that the user cannot change their install state. This is accomplished by modifying the "Display" column of the in-memory "Feature" table. The function presented bellow performs all these operations, with one exception: it will FAIL for Features that have Sub-Features (see the code comments).

I have omitted error handling for clarity.

Code: Select all

#define QUERY_MAXLEN  300

/*---------------------------------------------------------------------------------------*
 | This function sets the state for the specified Feature to INSTALLSTATE_ABSENT (which  |
 | means that the Feature will not be installed).                                        |
 | Then the specified Feature is hidden from the SelectionTree control in the            |
 | CustomizeDlg dialog by modifying the in-memory Feature table.                         |
 |                                                                                       |
 | Known issues:                                                                         |
 | - The function will FAIL for Features that have Sub-Features (see explanation bellow) |
 |                                                                                       |
 | @param  hInstall Handle to the installation provided to a DLL custom action           |
 | @param  lpszFeatureName A valid Feature identifier as it appears in the Feature table |
 | @return 0 for success (no error handling is currently performed)                      |
 *---------------------------------------------------------------------------------------*/
UINT __stdcall HideAndMakeFeatureAbsent(MSIHANDLE hInstall, LPCTSTR lpszFeatureName)
{
  MSIHANDLE hDatabase;
  PMSIHANDLE hViewFeature = 0;
  PMSIHANDLE hRec = 0;
  TCHAR szQuery[QUERY_MAXLEN];
  UINT res;

  // Causes a breakpoint exception - uncomment to attach the debugger
  // DebugBreak();
  
  // The Feature will not be installed
  res = MsiSetFeatureState(hInstall, lpszFeatureName, INSTALLSTATE_ABSENT);
  
  // We also need to hide the Feature in the SelectionTree control from the CustomizeDlg

  // Create the query string that will select the appropriate row from the Feature table
  wsprintf(szQuery, _T("SELECT * FROM `Feature` WHERE `Feature`.`Feature`='%s'"),
           lpszFeatureName);
  MessageBox(NULL, szQuery, "The SQL command", MB_OK | MB_ICONINFORMATION);
  
  // Get the handle to the database
  hDatabase = MsiGetActiveDatabase(hInstall);
  
  // Create a view object
  res = MsiDatabaseOpenView(hDatabase, szQuery, &hViewFeature);

  // Execute the above query - the view object should contain a single record 
  // from the Feature table
  res = MsiViewExecute(hViewFeature, 0);

  // Get the record
  res = MsiViewFetch(hViewFeature, &hRec);
  
  /** 
   * Delete the record from the in-memory Feature table - this change is temporary.
   * Note that if the Feature contains Sub-Features this call WILL FAIL (error code 1627)
   * because the referential integrity of the Feature table will not be respected anymore
   * (there will be Features that contain the deleted Feature's ID in the Feature_Parent
   * column).
   */
  res = MsiViewModify(hViewFeature, MSIMODIFY_DELETE, hRec);

  // Set the Display (5th column) to 0 such that the Feature will be hidden
  res = MsiRecordSetInteger(hRec, 5, 0);

  // Insert the temporary record
  res = MsiViewModify(hViewFeature, MSIMODIFY_INSERT_TEMPORARY, hRec);

  // Close the database handle
  MsiCloseHandle(hDatabase);
  
  // Assume the function was successful
  return 0;
}
When using this function in your code, you have to specify the Feature using its identifier. A simple method to determine this Feature ID is the following:
1. Switch to the Custom Actions page and add a new "Script Inline" CA.
2. Open the "Edit Condition" dialog by clicking the "..." button at the right of the Execution Condition -> "Expression" field.
3. Click the "Feature Action" (or "Feature State") button and select a Feature, then click OK. The Feature identifier will be inserted in the "Expression" text field from the "Edit Condition" dialog (for example, it could be "Feature_1" etc).
4. Delete the "Script Inline" Custom Action.

Then invoke the function in your code as follows:

Code: Select all

#define PIDKEY_LENGTH 100

BOOL __stdcall HideFeature(LPCTSTR lpszFeatureName, LPCTSTR lpszPidKey)
{
  //...
}

UINT __stdcall ValidateSerial(MSIHANDLE hInstall)
{
  TCHAR szPidKey[PIDKEY_LENGTH];
  DWORD dwLen = sizeof(szPidKey) / sizeof(szPidKey[0]);

  // retrieve the serial entered by the user
  UINT res = MsiGetProperty(hInstall, _T("PIDKEY"), szPidKey, &dwLen);
 
  //...
 
  if ( HideFeature(_T("Feature_1"), szPidKey) )
  {
    res = HideAndMakeFeatureAbsent(hInstall, _T("Feature_1"));
  }
 
  //...
  return 0;
}
I have not thoroughly tested this function, so please keep me informed of the results if you use this approach (or if you find another method). We also plan to add conditional Feature installation based on the serial entered by the user to the serial validation DLL that comes with AI.

Hope this helps.

Regards,
Ionut
Denis Toma
Advanced Installer Team
http://www.advancedinstaller.com/
cdesnoyers
Posts: 6
Joined: Fri Apr 14, 2006 8:18 pm

Thanks... works like a charm!!!

Chris
wathiant
Posts: 4
Joined: Mon Apr 23, 2007 8:26 am

This is the code I now use for disabling and hiding a feature and all its sub-features. The only drawback is that the table update is semi-permanent. After having removed a record, it will never return until you restart the installer (which is something I can live with, since customers should only have one serial key).

Code:

#define QUERY_MAXLEN 300
#define MAX_CHILD_FEATURES 32

UINT __stdcall HideFeature(MSIHANDLE hInstall, LPCTSTR lpszFeatureName)
{
MSIHANDLE hDatabase;
PMSIHANDLE hViewFeature = 0;
PMSIHANDLE hRec = 0;
TCHAR szQuery[QUERY_MAXLEN];
UINT res;

// Causes a breakpoint exception - uncomment to attach the debugger
// DebugBreak();
#ifdef _DEBUG
TCHAR messageText[512];
#endif

// Get the handle to the database
hDatabase = MsiGetActiveDatabase(hInstall);

// first we need to make all children absent
TCHAR childName[MAX_CHILD_FEATURES][QUERY_MAXLEN];
DWORD childNameSize;

// Create the query string that will select the appropriate row from the Feature table
wsprintf(szQuery, _T("SELECT Feature.Feature FROM Feature WHERE Feature.Feature_Parent='%s'"), lpszFeatureName);

// Create a view object
res = MsiDatabaseOpenView(hDatabase, szQuery, &hViewFeature);

// Execute the above query - the view object could contain any number of records
// from the Feature table
res = MsiViewExecute(hViewFeature, 0);

// Get the record
res = MsiViewFetch(hViewFeature, &hRec);
int count = 0;
while (res == ERROR_SUCCESS)
{
childNameSize = QUERY_MAXLEN;
res = MsiRecordGetString(hRec,1,childName[count++],&childNameSize);

#ifdef _DEBUG
wsprintf(messageText,_T("%s -> %s"),lpszFeatureName,childName[count-1]);
MessageBox(NULL, messageText, "Adding child feature to hide...", MB_OK | MB_ICONINFORMATION);
#endif
MsiCloseHandle(hRec);
res = MsiViewFetch(hViewFeature, &hRec);
}
MsiCloseHandle(hViewFeature);

for (int i=0;i<count;++i)
HideFeature(hInstall,childName);

#ifdef _DEBUG
MessageBox(NULL, lpszFeatureName, "Hiding Feature...", MB_OK | MB_ICONINFORMATION);
#endif

// The Feature will not be installed
res = MsiSetFeatureState(hInstall, lpszFeatureName, INSTALLSTATE_ABSENT);
// We also need to hide the Feature in the SelectionTree control from the CustomizeDlg

// Create the query string that will select the appropriate row from the Feature table
wsprintf(szQuery, _T("SELECT * FROM `Feature` WHERE `Feature`.`Feature`='%s'"), lpszFeatureName);

// Create a view object
res = MsiDatabaseOpenView(hDatabase, szQuery, &hViewFeature);

// Execute the above query - the view object should contain a single record
// from the Feature table
res = MsiViewExecute(hViewFeature, 0);

// Get the record
res = MsiViewFetch(hViewFeature, &hRec);

// Remove the record from the table
res = MsiViewModify(hViewFeature, MSIMODIFY_DELETE, hRec);

// Close the database handles
MsiCloseHandle(hDatabase);

// Assume the function was successful
return 0;
}

Return to “Common Problems”