How to integrate the licensing support in my .NET application?

ImportantThe following article uses options that are available starting with the Enterprise edition and project type.

The Advanced Licensing Support can be easily integrated with your managed application. In this article, it will be described how to achieve this in VB.NET and C#.

Step 1 - Loading and initializing the Licensing library.

The licensing support resource file consists of a Dynamic Load Library with the following exported functions.

  • ReadSettingsStr - the DLL entry function that does the initialization and validation of the licensing support - must be called each time the application starts.
  • ReadSettingsRetStr - the DLL entry function that does the initialization of the licensing support but will NOT kill the application when the trial expired and unlicensed, instead will return a value greater or equal to 4.
  • DisplayRegistrationStr - the DLL entry function that displays (if necessary) the registration dialog - should be called when the user wants to register the application.
  • RegisterStr - the DLL entry function that you can explicitly call from your application to register the license - it can be used as an alternative to the "DisplayRegistrationStr" function.

So in order to use those functions, you must define them and the DLL file name you use.

This code needs to be added to your C# application.

using System.Runtime.InteropServices;
using System.Diagnostics;

// This function does all the work
[DllImport("Trial.dll", EntryPoint = "ReadSettingsStr", CharSet = CharSet.Ansi)]
static extern uint InitTrial(String aKeyCode, IntPtr aHWnd);

// Use this function to register the application when the application is running
[DllImport("Trial.dll", EntryPoint = "DisplayRegistrationStr", CharSet = CharSet.Ansi)]
static extern uint DisplayRegistration(String aKeyCode, IntPtr aHWnd);

// Use this function to silently register the license without displaying our registration dialog; when called the function saves the license (without validating it) on local machine and returns 0 for success or a different exit code otherwise
[DllImport("Trial.dll", EntryPoint = "RegisterStr", CharSet = CharSet.Unicode)]
private static extern uint RegisterLicense([MarshalAs(UnmanagedType.LPStr)] String aKeyCode, String aLicense);
        

NoteWhen calling the RegisterStr function, you should make sure that the aLicense string uses the CRLF line end for each of its license text lines.

This code needs be added to your VB.NET application.

' This function does all the work
<DllImport("Trial.dll", EntryPoint:="ReadSettingsStr", CharSet:=CharSet.Ansi)> _
Private Shared Function InitTrial(ByVal aKeyCode As String, ByVal aHWnd As IntPtr) As UInteger
End Function

' Use this function to register the application when the application is running
<DllImport("Trial.dll", EntryPoint:="DisplayRegistrationStr", CharSet:=CharSet.Ansi)> _
Private Shared Function DisplayRegistration(ByVal aKeyCode As String, ByVal aHWnd As IntPtr) As UInteger
End Function

' Use this function to silently register the license without displaying our registration dialog; when called the function saves the license (without validating it) on local machine and returns 0 for success or a different exit code otherwise
<DllImport("Trial.dll", EntryPoint:="RegisterStr", CharSet := CharSet.Unicode)> _
Private Shared Function RegisterLicense(ByVal aKeyCode As String, ByVal aLicense As String) As UInteger
        

Step 2 - Using the licensing support.

Immediately after the main application window is displayed, you must initialize licensing support. The InitTrial method must be executed which will display the trial messages.

The kLibraryKey string needed to start the trial is defined in the Registration Tab, “Library Key” field, in Advanced Installer.

This code should be added to your C# application.

.........
// The kLibraryKey is meant to prevent unauthorized use of the library.
// Do not share this key. Replace this key with your own from Advanced Installer
// project > Licensing > Registration > Library Key
private const string kLibraryKey = "177291AA00FE97A4A15EDE12F5FEEE6AEF848E711477E84BA999DEAD60CADA84313821C09E4C";

private static void OnInit()
{
 try
 {
   Process process = Process.GetCurrentProcess();
   InitTrial(kLibraryKey, process.MainWindowHandle);
 }
 catch (DllNotFoundException ex)
 {
   // Trial dll is missing close the application immediately.
   MessageBox.Show(ex.ToString());
   Process.GetCurrentProcess().Kill();
 }
 catch(Exception ex1)
 {
   MessageBox.Show(ex1.ToString());
 }
}
........
// Function called from the application menu, when registering it
private void RegisterApp(object sender, RoutedEventArgs e)
{
   Process process = Process.GetCurrentProcess();
   DisplayRegistration(kLibraryKey, process.MainWindowHandle);
}
        

This code should be added to your VB.NET application.

........
' The kLibraryKey is meant to prevent unauthorized use of the library.
' Do not share this key. Replace this key with your own from Advanced Installer
' project > Licensing > Registration > Library Key
Private Shared kLibraryKey As String = "177291AA00FE97A4A15EDE12F5FEEE6AEF848E711477E84BA999DEAD60CADA84313821C09E4C"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

  Try
    Dim Proc As Process = Process.GetCurrentProcess()
    InitTrial(kLibraryKey, Proc.MainWindowHandle)
  Catch ex As DllNotFoundException
    MessageBox.Show(ex.ToString())
    Process.GetCurrentProcess().Kill()
  End Try

End Sub
........
        

NoteWhen running the application make sure that the trial DLL is placed in your application folder. Also, options like "Display Frequency Percent", "Show the trial message at first run" may cause a silent switch to trial mode.

Application Manifest

Visual Studio includes a default manifest, but you need to overwrite it. The manifest file allows you to specify the execution level, list of OSes supported and enables themes for Windows common controls and dialogs.

The app.manifest file can be found in the included samples linked below. You can also create the file with the below content then add it as a new item in your VS project and set it as current manifest in the project Properties view.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="asInvoker" uiAccess="false"/>
      </requestedPrivileges>
    </security>
  </trustInfo>
    <dependency>
        <dependentAssembly>
            <assemblyIdentity
                type="win32"
                name="Microsoft.Windows.Common-Controls"
                version="6.0.0.0"
                publicKeyToken="6595b64144ccf1df"
                language="*"
                processorArchitecture="x86"/>
        </dependentAssembly>
    </dependency>
</assembly>

Sample Projects

Download the Advanced Installer samples implementing the above functionality for C# Applications and VB.NET Applications.

See also

Integrate licensing feature in VB projects