Help‎ > ‎

ProductKey


Steps to create Product key validation DLL

Start Visual C++ 2008 Express edition. Select "Win32" from the project types and "Win32 project" from Visual Studio installed templates. Enter any name for the project.


Now the win32 project wizard starts. Click "Next"


Advance to the second page and select a Dll project type with the Export Symbols option checked and click "Finish".
[Image: image with few options in second page of project wizard]

This creates a DLL project which is ready for customization:



Before going any farther it is best to remove the wizard generated sample code in ProductKeyValidationExample.cpp. The following has to be removed:

// ProductKeyValidationExample.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "ProductKeyValidationExample.h"


// This is an example of an exported variable
PRODUCTKEYVALIDATIONEXAMPLE_API int nProductKeyValidationExample=0;

// This is an example of an exported function.
PRODUCTKEYVALIDATIONEXAMPLE_API int fnProductKeyValidationExample(void)
{
    return 42;
}

// This is the constructor of a class that has been exported.
// see ProductKeyValidationExample.h for the class definition
CProductKeyValidationExample::CProductKeyValidationExample()
{
    return;
}

And also following code
in ProductKeyValidationExample.h

// This class is exported from the ProductKeyValidationExample.dll

class PRODUCTKEYVALIDATIONEXAMPLE_API CProductKeyValidationExample {
public:
    CProductKeyValidationExample(void);
    // TODO: add your methods here.
};

extern PRODUCTKEYVALIDATIONEXAMPLE_API int nProductKeyValidationExample;

PRODUCTKEYVALIDATIONEXAMPLE_API int fnProductKeyValidationExample(void);

Make following changes in Property window. Right click project node in "Solution Explorer" > select "Properties". This will show up a window like below


Now change Character Set property to "Not Set"

Adding .DEF file

Visual C++ express doesn't come with .DEF template. But .DEF file is similar to normal text file.
1. Add a CPP file like shown below.


Change extension to ".def" in the Solution Explorer

Add following to the file created above
LIBRARY    "ProductKeyValidationExample"
EXPORTS
    PIDKEYValidation

Where

1. ProductKeyValidationExample
is the Dll name (exclude ".dll" extension). By default this is the Project Name unless it is changed in project properties.
2. PIDKEYValidation is the public method exported by this DLL.

The project is now ready for you to add validation code. In this sample we're going to do a simple product id/serial number validation routine. Basically check if the PIDKEY property is all 1's or not. Add the following code to ProductKeyValidationExample.cpp:

#include "stdafx.h"
#include "ProductKeyValidationExample.h"
#include <windows.h>
#include <iostream>
#include <fstream>
#include <msi.h>
#include <msiquery.h>
#include <stdlib.h>
#pragma comment(lib, "msi.lib")

using namespace std;

UINT __stdcall PIDKEYValidation(MSIHANDLE hInstaller)
{
    UINT rc;
    //
    // Get the PIDKEY property
    //
    char PIDKEY[256];
    DWORD PIDKEYSize = sizeof(PIDKEY);
    rc = MsiGetProperty(hInstaller, "PIDKEY", &PIDKEY[0], &PIDKEYSize);
    if (rc != ERROR_SUCCESS)
    {
        ::MessageBox(NULL, "MsiGetProperty(PIDKEY) failed.", "Error", MB_OK);
        return ERROR_INSTALL_FAILURE;
    }

    //
    // Validate the PIDKEY by checking if it is all ones
    //    - This should be more complicated
    //
    bool ValidPID = true;
    for (UINT i = 0; i < strlen(PIDKEY); i++)
    {
        if (PIDKEY[i] != 'x' && PIDKEY[i] != '-')
        {
            ValidPID = false;
            break;
        }
    }


    //
    // Let the installer know how it went
    //
    if (!ValidPID)
    {
        //
        // Clear the ProductID property
        //    - The ProductID property is used to determine whether the
        //      PIDKEY was valid or not.
        //
        rc = MsiSetProperty(hInstaller, "PIDACCEPTED", "0");
        if (rc != ERROR_SUCCESS)
        {
            ::MessageBox(NULL, "MsiSetProperty(ProductId) failed.", "Error", MB_OK);
            return ERROR_INSTALL_FAILURE;
        }
    }
    else
    {
        //
        // Save the product id
        //
        rc = MsiSetProperty(hInstaller, "PIDACCEPTED", "1");
        if (rc != ERROR_SUCCESS)
        {
            ::MessageBox(NULL, "MsiSetProperty(ProductId) failed.", "Error", MB_OK);
            return ERROR_INSTALL_FAILURE;
        }
    }


    //
    // Let the installer know we completed successfully
    //
    return 0;
}



Now make another change in project properties to set DEF file as shown below



Now build the project.

Using project key validation DLL with Nvn Installer


1. Set all details of your software package in product information tab, components tab etc
2. Browse Product key validation DLL.
3. DLL entry should be the function exported by the product key validation DLL, like "PIDKEYValidation" in the above example.
4. Property Name can be any property set using MsiSetProperty function, like "PIDACCEPTED" in the above example.
5. Set True Value and False Value which used for the product code validation, like in above example value "1" marks validation as successful and "0" marks validation as unsuccessful.

Download Sample


For any questions, suggestions or bug reports, please drop a mail to support@nvninstaller.com