Tuesday, June 23, 2009

How To Validate a Serial Number During an Installation Created with VSI

How To Validate a Serial Number During an Installation Created with VSI


SUMMARY
In a Visual Studio Installer (VSI) project, the developer has the ability to ad...

In a Visual Studio Installer (VSI) project, the developer has the ability to add a Customer Information dialog box in the user interface. By default, this dialog box has an input field for a serial number. Visual Studio Installer provides minimal support for validating this field. This article provides the steps necessary to include custom code to validate the serial number that is entered by the user. These steps include using the Windows Installer SDK tool Orca, and creating a DLL for which sample code is provided.

To download the Orca tool, see the following MSDN Web site that includes samples, tools, and documentation for the Windows Installer
http://msdn.microsoft.com/downloads/default.asp?URL=/code/sample.asp?url=/msdn-files/027/001/457/msdncompositedoc.xml (http://msdn.microsoft.com/downloads/default.asp?URL=/code/sample.asp?url=/msdn-files/027/001/457/msdncompositedoc.xml)

MORE INFORMATION
Create an empty installer project with VSI.Add a Customer Information dialog box...

  1. Create an empty installer project with VSI.
  2. Add a Customer Information dialog box to the user interface. Do not change the default properties for the dialog box.
  3. Build the project.
  4. Create a custom action DLL. NOTE: You cannot use an EXE custom action because this custom action sets a property and that can only be accomplished by using a DLL, which has a handle to the installation.

  5. On the Tools menu, click the Options Directories tab in your DLL project, and then add the path to the Windows Installer SDK's Include and Lib directories.
  6. In the Project Settings dialog box, add msi.lib to the library list. Use either a .DEF file or the __declspec(dllimport) attribute to export the DLL functions. Note that the PIDKEY property has the value of "XXX -XXXXXXX" where X is a number and there is both a space and hyphen. The following sample code uses a string comparison to determine if the serial number is valid:
    UINT __stdcall VerifyPID(MSIHANDLE hInstall)
    {
    // Local variables
    UINT nRetVal = 0;
    UINT uiMsiRc;
    TCHAR szPidKey[MAX_PATH];
    DWORD dwBuffer;

    dwBuffer = sizeof(szPidKey)/sizeof(TCHAR);

    // First Step - Get the PIDKEY property
    uiMsiRc = MsiGetProperty(hInstall, TEXT("PIDKEY"), szPidKey, &dwBuffer);

    if (ERROR_SUCCESS != uiMsiRc)
    {
    MessageBox(NULL, "PIDKEY", "Not able to retrieve PIDKEY property", MB_OK | MB_ICONEXCLAMATION);
    return 0;
    }

    //Insert code to check PIDKEY here
    int str = lstrcmp(szPidKey, "123 -4567890");

    //If PIDKEY passes check
    if (str == 0)
    MsiSetProperty(hInstall, "PIDCHECK", "TRUE");
    //If PIDKEY doesn't pass check
    else
    {
    MsiSetProperty(hInstall, "PIDCHECK", "FALSE");
    wsprintf(szText, "Please enter the correct code");
    MessageBox(NULL, szText, "PIDCHECK", MB_OK | MB_ICONINFORMATION);
    }

    return 0;
    }
  7. Open the Microsoft Windows installer package (.msi) file that you built with VSI in Orca.
  8. Add the DLL to the Binary table and name it VerifyPIDDll.
  9. Add a new custom action to the Custom Action Table:
    Name - MyPIDChecker;
    Type - 1; Source - VerifyPIDDll; Target - VerifyPID
  10. In the ControlEvent table, find the UserNameForm, which corresponds to the Customer Information dialog box from VSI. Make all changes to the existing rows with UserNameForm in the Dialog column and Next in the Control column:
    1. Replace the ValidateProductID Event with DoAction. On the same row, replace the Argument {} with MyPIDChecker.
    2. Make sure that the value for the Condition column in the row with the EndDialog event is:
      UserNameForm_NextArgs="" AND UserNameForm_ShowSerial=""
    3. Using the PIDCHECK property which was set in the precious sample code, make sure that the value for the Condition column in the row with the NewDialog event is:
      (PIDCHECK="TRUE") AND UserNameForm_NextArgs<>"" AND UserNameForm_ShowSerial<>""
    4. Make sure that the value for the Condition column in the row with the [UserNameForm_ShowSerialDisabled] event is:
      PIDCHECK="TRUE"
  11. Save the .msi file and run the installation.

3 comments:

johnnywalker said...

this is also from http://support.microsoft.com/kb/253683/en-us, however, i can't understand the part of creating a custom action dll, how do i do it?

Unknown said...

me either, i cant understand that part. can anyone tell us how to do it?or maybe enlighten us?

Charles said...

Hi, This is in reponse to Hazel's post.

Hazel, a while ago I had come across a helpful blog that explained exactly HOW to Validate a Serial Number During Installation of your Windows Installer (MSI) Using Orca and I thought I'd share it with you so it could help you figure this out.

I think the key is to write your own custom code for validating the serial number (i.e. implement it within the C function that is exported out in your DLL)

I hope this helps.

Thanks,