How to create PowerShell wrapper for .Net trial.dll

Overview

Although Advanced Installer trial licensing offers extensive support for integrating the licensing support with various applications, (predefined support for apps written in C++, C#, VB.NET or Java) there might be apps where might be challenges to load dll files.

If you are in such a situation, you can create a PowerShell script that will load the trial.dll and where you can interrogate the trial.dll from PowerShell. Inside your application, you will launch the PowerShell script instead of loading the dll file.

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 it 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.

Specifications

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 PowerShell script:

$dllPath = "C:\Program Files (x86)\Caphyon\Trial Test Example .NET\Trial.dll"

Add-Type -TypeDefinition @'
    using System;
    using System.Runtime.InteropServices;
    using System.Text;


    public static class ReadSettingsStr
    {
        [DllImport(@"C:\Program Files (x86)\Caphyon\Trial Test Example .NET\Trial.dll", EntryPoint = "ReadSettingsRetStr", CharSet = CharSet.Ansi)]
        public static extern short InitRndm(string key_dev, int window_enable);

        [DllImport("Trial.dll", EntryPoint = "GetPropertyValue", CharSet = CharSet.Unicode)]
        static extern uint GetPropertyValue(String aPropName, StringBuilder aResult, ref UInt32 aResultLen);
        
        
        public static string GetProperty(string name)
        {       
                StringBuilder val = new StringBuilder();
                UInt32 len2 = (UInt32) val.Capacity;
                if ( GetPropertyValue( name, val, ref len2) == 234 )
                {
                  val.EnsureCapacity( (Int32) len2 );
                  GetPropertyValue( name, val, ref len2);
                }
                return val.ToString();
       }
    }
'@


$initLicenseCode = [ReadSettingsStr]::InitRndm('3C770255172DB4CE6E38BFCDD0930341F7F22BC405A25E57DF01816FD99D287D3DCEE5C3F5E0', 0) 

# the dll initialization will return a value greater or equal to 4 when the trial expired and unlicensed

If ($initLicenseCode -ge 4)
{
    Write-Host("The trial expired and unlicensed")
}

How to interrogate the licensing library to get license information?

The licensing library defines some properties that you can read from your application. Check the Trial and Licensing Properties article for the properties that are set by the trial library.

After you have initialized the licensing library, use the GetPropertyValue licensing library functions.

$trialLeft = [ReadSettingsStr]::GetProperty("TrialLeft") 

If ($trialLeft -gt 0)
{
    Write-Host("Trial is valid, there are " + $trialLeft + " days left of trial!")
    Write-Host("AddIn should be enabled!")
}
else
{
    Write-Host("No trial days left!")
    Write-Host("AddIn should be disabled!")
}