インストールの途中でプロダクトキーの入力を求め、正当なインストールかどうか調べるプログラムを作成します。
ソフトメーカーがどのようなプロダクトキー作成アルゴリズムを使っているのか、日本語の情報を見つけることができませんでしたが、Code Projectのサイトにそれらしいものがあります(私自身内容を確認していません)。
今はプロダクトキーの作成方法が論点ではないので、こちらは簡単にして、WiXでCustomActionをどのように作成して、どのように組み込むかをご説明します。
CustomActionは様々な方法で作成できます。PACKT本に詳しく書かれていますが、今回はVBでDllを作成します。
アプリケーションの注文が来たら、ユーザ名を使ったプロダクトキーを作成・返送し、ユーザがソフトをインストールするときに、名前とキーを使ってインストールが正規かどうかチェックします。
入力用のダイアログ画面ではユーザ名、会社名、プロダクトキーの入力用TextBoxがあって、ユーザが入力した値が、USERNAME、COMPANYNAME、PIDKEYグローバル・プロパティにセットされます。
これをチェックする簡単なアクションプログラムを下に示します。セットアップ本体とCustomActionとは、sessionを使って簡単にデータの授受ができます。
プロダクトキーはにユーザ名を16桁のHexで表示したものです。Hexアレイの順番を入れ替えるとか、ダミーの文字を入れるとかするともう少しもっともらしくなります。
VBのプロジェクトを以下のように作成します。Visual Studio 2013で「新しいプロジェクト」をクリック、Windows Installer XMLタブから、VB Custom Action Projectをクリックします。

自動生成されたメインのファイル(CustomAction.vb)を以下のように変更し、ビルドするとCheckPID.CA.dllファイルが作成されます
Imports System.IO
Imports System.Text
Imports Microsoft.Deployment.WindowsInstaller
Public Class CustomActions2
<CustomAction()> _
Public Shared Function CheckPID(ByVal session As Session) As ActionResult
Dim strName As String = session("USERNAME")
Dim strPid As String = session("PIDKEY")
Dim O_CheckID = New clsProductKey2("Shift_JIS")
session("PIDACCEPTED") = O_CheckID.CheckKey(strName, strPid)
Return ActionResult.Success
End Function
End Class
Public Class clsProductKey2
Private enc As Encoding
Private Const strPpack As String = "m6bk9UoUXzgTvmwv"
Public Sub New(ByVal encStr As String)
enc = Encoding.GetEncoding(encStr)
End Sub
Public Function CheckKey(P_Name As String, P_ProductKey As String) As String
Dim strFromName As String = MakeKey(P_Name)
If strFromName.Equals(DelDelimiter(P_ProductKey, " ")) _
Or strFromName.Equals(DelDelimiter(P_ProductKey, "-")) Then
Return "1"
Else
Return "0"
End If
End Function
Public Function DelDelimiter(p_strHyphen As String, p_strDelimiter As String) As String
Dim strArray() As String = p_strHyphen.Split(p_strDelimiter)
Dim strHex As String = ""
For Each strOneChar As String In strArray
strHex &= strOneChar
Next
Return strHex
End Function
Public Function MakeKey(p_Name As String) As String
Dim strPackedName As String = p_Name & strPpack
Dim nameBytes() As Byte = enc.GetBytes(strPackedName)
Dim str As String = ""
Dim i As Integer
For i = 0 To nameBytes.Length - 1
If i < 8 Then
str &= String.Format("{0:X2}", nameBytes(i))
Else
Exit For
End If
Next
Return str
End Function
End Class
次回は、プロダクトキー入力ダイアログと、これらをどのようにセットアッププログラムに組み込むか、ご説明いたします。