Developing a DLL for serial number validation

To validate a serial number during installation, you need to provide a DLL with a function that checks the serial entered by the user. This DLL will run as a custom action. The scenario used to validate the serial number is as following. If your package is run at full UI level the validation starts when the user presses the [ Next ] button from the “User Registration” dialog.

  1. The first step of the validation is made by Windows Installer. It checks that the value entered by the user matches a template. If this validation FAILS, a message box is displayed to the user - a new serial must be entered.
  2. At the second step, the function you specified (from the DLL file) is called. This function has the following responsibilities:
    1. Retrieves the serial entered by the user - this is stored in the Windows Installer property named PIDKEY.
    2. Validates this number according to a custom logic.
    3. Sets a Windows Installer property named SERIAL_VALIDATION to the string TRUE if the serial is valid and FALSE otherwise.
  3. The installation continues if the property was set to TRUE; else, the user must enter again the serial number.

If your package is not run with full UI Level,the validation is triggered at the beginning of the installation sequence:

  1. The first step is the same as above, except that if the serial does not match the template, the installation fails.
  2. The second step is the same as the above.
  3. The third step is different. If the validation was successful, the installation continues. Otherwise, depending on the UI level, a message box is displayed - "the serial was invalid" - or just a log entry is made and the installation fails.

Below is presented a C++ skeleton code to guide you into writing your own DLL for serial number validation.

UINT __stdcall ValidateSerial_Sample(MSIHANDLE hInstall)
  DWORD dwLen = sizeof(szPidKey)/sizeof(szPidKey[0]);
  //retrive the text entered by the user
  UINT res = MsiGetProperty(hInstall, _T("PIDKEY"), szPidKey, &dwLen);
  if(res != ERROR_SUCCESS)
    //fail the installation
    return 1;
  bool snIsValid = false;
  //validate the text from szPidKey according to your algorithm
  //put the result in snIsValid
  TCHAR * serialValid;
    serialValid = _T("TRUE");
    //eventually say something to the user
    MessageBox(0, _T("Invalid Serial Number"), _T("Message"), MB_ICONSTOP);
    serialValid = _T("FALSE");
  res = MsiSetProperty(hInstall, _T("SERIAL_VALIDATION"), serialValid);
  if(res != ERROR_SUCCESS)
    return 1;
  //the validation succeeded - even the serial is wrong
  //if the SERIAL_VALIDATION was set to FALSE the installation
  //will not continue
  return 0;

Sample Code

For an easy way to develop your own validation DLL, download and unzip the Serial Number Validation DLL Sample Project. It contains a fully functional Visual Studio .NET project which creates a C++ DLL doing a trivial validation, together with an Advanced Installer project which uses the DLL.

Warning!You must customize the DLL Validation function, because the one supplied is very naive and easy to break.

Setting the SKU

If you specified the SKUs into your project, you must set the AI_SKU_ID property to the desired SKU ID.

MsiSetProperty(hInstall, _T("AI_SKU_ID"), skuID);