Microsoft KB Archive/253683

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

Article ID: 253683

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft Visual Studio Installer 1.0
 * Microsoft Visual Studio Installer 1.1

-



This article was previously published under Q253683



SUMMARY
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



MORE INFORMATION
 Create an empty installer project with VSI. Add a Customer Information dialog box to the user interface. Do not change the default properties for the dialog box. Build the project. 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.

 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.  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; }                    Open the Microsoft Windows installer package (.msi) file that you built with VSI in Orca.</li> Add the DLL to the Binary table and name it VerifyPIDDll.</li> Add a new custom action to the Custom Action Table:

Name - MyPIDChecker;

Type - 1; Source - VerifyPIDDll; Target - VerifyPID

</li> 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: <ol style="list-style-type: lower-alpha;"> Replace the ValidateProductID Event with DoAction. On the same row, replace the Argument {} with MyPIDChecker.</li> Make sure that the value for the Condition column in the row with the EndDialog event is:

UserNameForm_NextArgs="" AND UserNameForm_ShowSerial=""

</li> 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<>""

</li> Make sure that the value for the Condition column in the row with the [UserNameForm_ShowSerialDisabled] event is:

PIDCHECK="TRUE"

</li></ol> </li> Save the .msi file and run the installation.</li></ol>

<div class="references_section">