Page tree
Skip to end of metadata
Go to start of metadata

C#.NET | VB.NET

Introduction

This article shows you how to use an example code project as the basis for adding Nalpeiron Licensing and/or Analytics to your own application using Visual Studio and C# or VB.NET. You may select your preferred language at the top of this page which will filter the code examples accordingly.

Before following this documentation, please be sure to first read sections Introduction which provide important details relating to integration. You may also want to review section Avoiding Potential Pitfalls - NSL. You should also create and download your custom Licensing/Analytics DLL - see section Download the required Nalpeiron library for how to do this.

Only want Licensing?

This example that combines the Software Licensing with Analytics collection. If you only wish to do licensing or do not have any analytics capabilities then you can choose the  .NET NSL example to start with.


Download the VB Example Application

You can download the required VB example project below:

 

Download the example project (filechck 1.9.x)
Download the example project (filechck 2.1.x)
Download the example project (filechck 2.2.x+)  
Release Notes (Please always check the release notes for the latest updates)


Download the C# Example Application

You can download the required C# example project below:

 

Download the example project (filechck 1.9.x)
Download the example project (filechck 2.1.x)
Download the example project (filechck 2.2.x)
Release Notes (Please always check the release notes for the latest updates)

Getting Started with Visual Studio

 

Download the required example application from the above link and extract to a suitable folder.



Double-clicking the project file ShaferExample.csproj will open the project in Visual Studio and create a solution from this project.
Double-clicking the project file VBShaferExample.vbproj will open the project in Visual Studio and create a solution from this project.

 

Before building this solution, ensure that the ShaferFilechck.dll library is in the same folder as your application's executable.  In Visual Studio, this will, by default, be either the /bin/release or /bin/debug folder depending on whether your build configuration is or debug or release.

 



 

Code Integration Walkthrough - Licensing

 

1. Create your Public Class


The ShaferExample class is our example public class - scrolling to the top of this class you will find a number of variables which should be initialized correctly. Please refer to section Overview for an explanation of variables CustomerID, ProductID and the three security constants: You should also instantiate the Shafer class and assign this to a class variable:

 

VB Example - Initialising the class variables
    Public Class ShaferExample
        Private oShafer As New Shafer()
        Private Username As String = ""
        Private ShaferError As Integer = 0
        Private CustomerID As Integer = 3183
        '**** REPLACE WITH YOUR CUSTOMERID 
        Private ProductID As Integer = 100
        '**** REPLACE WITH YOUR PRODUCTID
        Private SecurityConstantX As UInt32 = 100
        '**** REPLACE WITH YOUR Security Constant X
        Private SecurityConstantY As UInt32 = 600
        '**** REPLACE WITH YOUR Security Constant Y
        Private SecurityConstantZ As UInt32 = 400
        '**** REPLACE WITH YOUR Security Constant Z
        Private UserLoginTxnID As UInt32 = 0
        Private Shared timer As New Timer()
        'Timer to periodically check License
        Public JustExit As [Boolean] = False
        Public AppStarted As [Boolean] = False

 

C# Example - Initialising the class variables
    public partial class ShaferExample : Form
    {
        Shafer oShafer = new Shafer();
        string Username = "";
        int ShaferError = 0;
        int CustomerID = 3183; //**** REPLACE WITH YOUR CUSTOMERID 
        int ProductID = 100; //**** REPLACE WITH YOUR PRODUCTID
        UInt32 SecurityConstantX = 100; //**** REPLACE WITH YOUR Security Constant X
        UInt32 SecurityConstantY = 600; //**** REPLACE WITH YOUR Security Constant Y
        UInt32 SecurityConstantZ = 400; //**** REPLACE WITH YOUR Security Constant Z
        UInt32 UserLoginTxnID = 0;
        Timer timer = new Timer(); //Timer to periodically check License
        public Boolean JustExit = false;
        public Boolean AppStarted = false;

Within your Form Load or Application Initialize Event, add code to call the following functions located in the Shafer.cs (oShafer Object) class

2. Call your Initialise Method

 

VB Example - Load / Init Event
       	Public Function init(ByVal CustomerID As Integer, ByVal ProductID As Integer, ByVal X As UInt32, ByVal Y As UInt32, ByVal Z As UInt32, ByRef BadImage As Boolean) As Integer  

       	Example:

       	'Initialise
       	ShaferError = oShafer.init(CustomerID, ProductID, SecurityConstantX, SecurityConstantY, SecurityConstantZ,
       								BadImage)
       	If BadImage = True Then
       		Dim buildplatform As String = GetBuildPlatform()
	        Dim DLLplatform = ""
            If (buildplatform = "x86") Then
            	DLLplatform = "x64"
            Else
            	DLLplatform = "x86"
            End If
            Dim badimagemsg As String = "Failure.  You seem to be running this application in " + buildplatform +
                                        " mode but your downloaded DLL (ShaferFileChck.dll) has been configured for " +
                                        DLLplatform + ".  Please re-download your DLL for " + buildplatform +
                                        " or change your application target platform to " + DLLplatform + "."
            MessageBox.Show(badimagemsg)
            JustExit = True
            Application.Exit()
        End If
        If ShaferError <> 0 Then
        	If ShaferError = -1112 Then
            	JustExit = True
            End If
            ShowError(ShaferError)
        End If
        

 

C# Example - Load / Init Event
       
       public int init(int CustomerID, int ProductID, UInt32 X, UInt32 Y, UInt32 Z, ref bool BadImage)

       Example:

         //Initialise
         ShaferError = oShafer.init(CustomerID, ProductID, SecurityConstantX, SecurityConstantY, SecurityConstantZ,
                ref BadImage);
         if (BadImage)
         {
         	var buildplatform = GetBuildPlatform();
            var DLLplatform = "";
            if (buildplatform == "x86")
            	DLLplatform = "x64";
            else
            	DLLplatform = "x86";
            var badimagemsg = "Failure.  You seem to be running this application in " + buildplatform +
                              " mode but your downloaded DLL (ShaferFileChck.dll) has been configured for " +
                              DLLplatform + ".  Please re-download your DLL for " + buildplatform +
                              " or change your application target platform to " + DLLplatform + ".";
            MessageBox.Show(badimagemsg);
            JustExit = true;
            Application.Exit();
         }
         if (ShaferError != 0)
         {
                
         	if (ShaferError == -1112)
            	JustExit = true;
            ShowError(ShaferError);
         }
The BadImage flag is returned as true if an x64 ShaferFileChck DLL is loaded into an x86 application or vice versa, otherwise false.  The actual build platform of the application is returned by the GetBuildPlatform function:

 

C# Example - GetBuildPlatform
       
        private string GetBuildPlatform()
        {
            var result = "";
            if (IntPtr.Size == 8)
                result = "x64";
            else
                result = "x86";
            return result;
        }
 

 

VB Example - GetBuildPlatform
Private Function GetBuildPlatform() As String
Dim result = ""
If IntPtr.Size = 8 Then
	result = "x64"            
Else
	result = "x86"
End If
Return result
End Function

        

The ShowError function performs the frequently-needed task of obtaining the error message from the error code passed in.  There is a second, Boolean parameter passed to this function whose purpose is to specify whether or not the application should exit.

 

C# Example - ShowError
//Takes the error message id and retrieves the apropriate error message to present to the user
public static void ShowError(int shaferError, bool exitApplication)
{
	string errMess = "";
	Shafer.GetErrorMessage(shaferError, ref errMess);
	MessageBox.Show(errMess);
	if (exitApplication)
	{
		Application.Exit();
	}
} 

 

VB Example - ShowError
'Takes the error message id and retrieves the apropriate error message to present to the user
Protected Shared Sub ShowError(errorId As Int32, Optional exitApplication As Boolean = True)
    Dim ErrMess = ""
    Shafer.GetErrorMessage(errorId, ErrMess)
    MessageBox.Show(ErrMess)
    If exitApplication Then
        Application.[Exit]()
    End If
End Sub

3. Retrieve and Check the License Status

 

VB Example - GetLicenseStatus
       
      'Get License Status
      ShaferError = oShafer.GetLicenseStatus(LicenseStatus)
      If ShaferError <> 0 Then
      	ShowError(ShaferError)
      End If
        

 

C# Example - GetLicenseStatus
       
        //Get License Status
        ShaferError = oShafer.GetLicenseStatus(ref LicenseStatus);
        if (ShaferError != 0)
        {
        	ShowError(ShaferError);
        }
        

4. Additional Security Checks


Add security code to check the license status: if the license status shows that the client computer has been backdated then show message and exit application.  This will give the user the prompt to correct their computer's date and time before running the application again:

 

C# Example - Security Check
       
        //SECURITY: If computer has been BACK Dated to get more time for the trial or Subscription then Exit Application
        if (LicenseStatus == (int) Shafer.LicenseStatus.PROD_BTCOUNTER)
        {
        	ShowError(ShaferError);
        }
        

 

VB Example - Security Check
       
        'SECURITY: If computer has been BACK Dated to get more time for the trial or Subscription then Exit Application
        If LicenseStatus = CInt(Shafer.LicenseStatus.PROD_BTCOUNTER) Then
        	ShowError(ShaferError)
        End If
        

5. Retrieve License from Server


Add the following code to check the license status: if a positive value is returned then the license status is good for your application to continue running.  It will be in either trial or activated (with a license code) mode.
If the value is negative or 0 then the client will need to attempt to get a refreshed license from the activation server via the function GetLicenseFromServer(), which defined and explained in Step 7.)

 

VB Example - Get License from Server
            'If License Status is negative then try to get license from server, if not then prompt for manual activation
            If LicenseStatus <= 0 Then
                LicenseStatus = GetLicenseFromServer(False)
            Else
                'Extra security procedure: If License Then table overwritten On an online system, the license table will refresh itself.
                'Don't try License Refresh if Network LTCO, Daemon doen't allow LTCO Refesh's for security reasons
                If LicenseStatus <> (Int(Shafer.LicenseStatus.PROD_NETWORK_LTCO)) Then
                    'To enable the license to be refreshed (update local entitlements from the server) on application start, we need to attempt a license refresh.  If offline then continue running with the local entitements.  If online then refresh from the server. 
                    LicenseStatus = GetLicenseFromServer(True)
                End If
            End If

 

C# Example - Get License from Server
            //If License Status is negative then try to get license from server, if not then prompt for manual activation
            if (LicenseStatus <= 0)
            {
                LicenseStatus = GetLicenseFromServer(false);
            }
            else
            {
                // Extra security procedure: if license table overwritten on an online system, the license table will refresh itself.
                // Don't try License Refresh if Network LTCO - daemon doesn't allow LTCO Refreshes for security reasons
                if (LicenseStatus != (int)Shafer.LicenseStatus.PROD_NETWORK_LTCO)
                {
                    // To enable the license to be refreshed (update local entitlements from the server) on application start, we need to attempt a license refresh.  If offline then continue running with the local entitlements.  If online then refresh from the server. 
                    LicenseStatus = GetLicenseFromServer(true);
                }
            }
See section  Retrieving a License From the Activation Server for a detailed walkthrough of the GetLicenseFromServer method.

6.  Get the License Information


There are two kinds of license information which determine the kind of license being used:
  • License Type: e.g. whether the license is trial or activation (with a license code), subscription or perpetual, node-locked or concurrent.
  • Activation Type: e.g. whether the license was activated on or offline.
The GetLicenseInfo function obtains this information and is wrapped in method ShowLicenseStatus which can be found following the calls to GetLicenseFromServer in your form Load event. The code for GetLicenseInfo is as follows:

 

C# Example - Get License Info
       
        private void GetLicenseInfo(ref string licensetype, ref string activationtype)
       	{
        	uint ltype = 0;
            uint atype = 0;
            ShaferError = oShafer.GetLicenseInfo(ref ltype, ref atype);
            if (ShaferError == 0)
            {
                licensetype = Shafer.GetLicenseType(ltype);
                activationtype = Shafer.GetActivationType(atype);
            }
            else
            {
                ShowError(ShaferError);
            }
        }
          

 

VB Example - Get License Info
       
        Private Sub GetLicenseInfo(ByRef licensetype As String, ByRef activationtype As String)
            Dim ltype As UInt32
            Dim atype As UInt32
            ltype = 0
            atype = 0
            ShaferError = oShafer.GetLicenseInfo(ltype, atype)
            If ShaferError = 0 Then
                licensetype = Shafer.GetLicenseType(ltype)
                activationtype = Shafer.GetActivationType(atype)
            Else
                ShowError(ShaferError)
            End If
        End Sub
          
Further information about the values this function can return is provided in this document.   

7. Setup your application to check the license status at regular intervals whilst running


This step is required so your running application will check with the application server to refresh its local license from the activation server at regular intervals while it is running.

The local Client License is only valid for the set Client Lease period for the trial or for the activated License code.

When the client Lease has expired the client is required to retrieve a new license from the server.

This allows the client to expire (subscription or trial) during the applications execution, instead of just at application start
At the end of the VBShaferExample_Load(object sender, EventArgs e) event handler, when a connection to Nalpeiron has been established and the licensing is verified as operational, insert a call to StartTimer() where this function is given by the following:
At the end of the ShaferExample_Load(object sender, EventArgs e) event handler, when a connection to Nalpeiron has been established and the licensing is verified as operational, insert a call to StartTimer() where this function is given by the following:

 

VB Example - Start Timer
       
        Public Sub StartTimer()
            AddHandler timer.Tick, AddressOf timer_Tick
            ' timer.Tick += New EventHandler(timer_Tick)
            ' Everytime timer ticks, timer_Tick will be called
            timer.Interval = (1000) * (120)
            ' Timer will tick evert 2 mins
            timer.Enabled = True
            ' Enable the timer
            ' Start the timer
            timer.Start()
        End Sub
          

 

C# Example - Start Timer
       
        public void StartTimer()
        {
            timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
            timer.Interval = (1000) * (120);            // Timer will tick evert 2 mins
            timer.Enabled = true;                       // Enable the timer
            timer.Start();
        }
          
Then add the timer handler to call the following code, to check the License status and retrieve new license if required.

 

C# Example - Timer tick event
       
        private void timer_Tick(object sender, EventArgs e)
        {
            var LicenseStatus = 0;
            //Get License Status
            ShaferError = oShafer.GetLicenseStatus(ref LicenseStatus);
            if ((ShaferError != 0) && (ShaferError != -1094)) //No license
            {
                ShowError(ShaferError);
            }
            //SECURITY: If computer has been BACK Dated to get more time for the trial or Subscription then Exit Application
            if (LicenseStatus == (int) Shafer.LicenseStatus.PROD_BTCOUNTER)
            {
                ShowError(ShaferError);
            }
            //If License Status is negative then try to get license from server
            if (LicenseStatus <= 0)
                GetLicenseFromServer(false);
            //  ShowLicenseStatus(LicenseStatus);
        }
          

 

VB Example - Timer tick event
               
        Private Sub timer_Tick(sender As Object, e As EventArgs)
            Dim LicenseStatus = 0
            'Get License Status
            ShaferError = oShafer.GetLicenseStatus(LicenseStatus)
            If ShaferError <> 0 AndAlso ShaferError <> -1094 Then 'No license
                ShowError(ShaferError)
            End If
            'SECURITY: If computer has been BACK Dated to get more time for the trial or Subscription then Exit Application
            If LicenseStatus = CInt(Shafer.LicenseStatus.PROD_BTCOUNTER) Then
                ShowError(ShaferError)
            End If
            'If License Stauts is negative then try to get license form server
            If LicenseStatus <= 0 Then
                GetLicenseFromServer(False)
            End If
            ShowLicenseStatus(LicenseStatus)
        End Sub
          

Code Integration Walkthrough - Analytics

The following code examples are required for generating analytics data. 

ApplicationStart Logging


Add code at the bottom of your Load event to call method ApplicationStart which you can find in the Shafer.cs class. This method takes the following parameters:
Parameters
Description
Username (string)This is the recorded username of user starting the application, if not required or available set to empty string.
Applanguage (string)Set to the ISO 2 digit language code for the applications running Language.
Version (String)Set to your applications Major version number.
Build (String)Set to your applications Build Number.
Edition (String)Set to your applications Edition.
LicenseStatus (String)Set to your applications License Status. If using NSL use return value from GetLicenseStatus as shown below
C# Example - Application Start
       
            System.Reflection.Assembly thisAssembly = this.GetType().Assembly;
            CultureInfo ci = CultureInfo.InstalledUICulture;

            //Check the privacy settings if required
            ShaferError = oShafer.GetPrivacy();
            if (ShaferError < 0)
            {

                string ErrMess = "";
                Shafer.GetErrorMessage(ShaferError, ref ErrMess);
                MessageBox.Show(ErrMess);
                Application.Exit();
            }
            else if ((Shafer.PrivacyValues)ShaferError == Shafer.PrivacyValues.NOT_SET)
            {
                //If privacy not set then prompt and set
                DialogResult dialogResult = MessageBox.Show("You are not currently participating in our product improvement program, participate in the direction of future versions of our products by clicking Yes, below"
                       , "Privacy", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    //Set to allow data transmission
                    ShaferError = oShafer.SetPrivacy(Shafer.PrivacyValues.OFF);
                }
                else
                {
                    //Set to disallow data transmission
                    ShaferError = oShafer.SetPrivacy(Shafer.PrivacyValues.ON);
                }
                if (ShaferError != 0)
                {

                    string ErrMess = "";
                    Shafer.GetErrorMessage(ShaferError, ref ErrMess);
                    MessageBox.Show(ErrMess);
                    Application.Exit();
                }
            }
            //If privacy already set then don't ask again

            //Record the application Start
            if (txtUsername.Text == "")
            {
                txtUsername.Text = "Username";
            }
            ShaferError = oShafer.ApplicationStart(Username, ci.Name, thisAssembly.GetName().Version.Major.ToString(), "ENT", thisAssembly.GetName().Version.Minor.ToString(), LicenseStatus == (int)Shafer.LicenseStatus.PROD_INTRIAL ? "Trial" : "Activated");
            if (ShaferError != 0 && ShaferError != -1079 && ShaferError != -1080) // Forbidden by privacy setting
            {
                string ErrMess = "";
                Shafer.GetErrorMessage(ShaferError, ref ErrMess);
                MessageBox.Show(ErrMess);
                Application.Exit();
            }
            else
            {
                AppStarted = true;
            }
          

 

VB Example - Application Start
       
            Dim thisAssembly As System.Reflection.Assembly = Me.[GetType]().Assembly
            Dim ci As CultureInfo = CultureInfo.InstalledUICulture

            'Check the privacy settings if required
            ShaferError = oShafer.GetPrivacy()
            If ShaferError < 0 Then

                Dim ErrMess As String = ""
                Shafer.GetErrorMessage(ShaferError, ErrMess)
                MessageBox.Show(ErrMess)
                Application.[Exit]()
            ElseIf CType(ShaferError, Shafer.PrivacyValues) = Shafer.PrivacyValues.NOT_SET Then
                'If privacy not set then prompt and set
                Dim dialogResult__1 As DialogResult = MessageBox.Show("You are not currently participating in our product improvement program, participate in the direction of future versions of our products by clicking Yes, below", "Privacy", MessageBoxButtons.YesNo)
                If dialogResult__1 = DialogResult.Yes Then
                    'Set to allow data transmission
                    ShaferError = oShafer.SetPrivacy(Shafer.PrivacyValues.OFF)
                Else
                    'Set to disallow data transmission
                    ShaferError = oShafer.SetPrivacy(Shafer.PrivacyValues.ON)
                End If
                If ShaferError <> 0 Then

                    Dim ErrMess As String = ""
                    Shafer.GetErrorMessage(ShaferError, ErrMess)
                    MessageBox.Show(ErrMess)
                    Application.[Exit]()
                End If
            End If

            If txtUsername.Text = "" Then
                txtUsername.Text = "Username"
            End If
            'Record the application Start
            ShaferError = oShafer.ApplicationStart(Username, ci.Name, thisAssembly.GetName().Version.Major.ToString(), "ENT", thisAssembly.GetName().Version.Minor.ToString(), If(LicenseStatus = CInt(Shafer.LicenseStatus.PROD_INTRIAL), "Trial", "Activated"))
            If ShaferError <> 0 AndAlso ShaferError <> -1079 AndAlso ShaferError <> -1080 Then ' Forbidden by privacy setting
                Dim ErrMess As String = ""
                Shafer.GetErrorMessage(ShaferError, ErrMess)
                MessageBox.Show(ErrMess)
            Else
                AppStarted = True
            End If
          

ApplicationEnd Cleanup

 

When your application ends the following cleanup tasks must be carried out:

  • For a cloud or network concurrent license, the license must be returned.
  • Any floating feature seats must be returned.

The following example shows code to return a concurrent license on trapping of the form close event.  Code for returning a floating feature seat is shown further below. 

 

C# Example - Application End
       
        private void ShaferExample_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!JustExit)
            {
                int LicenseStatus = 0;

                //ON Exit Check to see If concurrency License if so then return the license to the server to acheive concurrency
                ShaferError = oShafer.GetLicenseStatus(ref LicenseStatus);
                if (ShaferError != 0)
                {

                    string ErrMess = "";
                    Shafer.GetErrorMessage(ShaferError, ref ErrMess);
                    MessageBox.Show(ErrMess);
                    Application.Exit();
                }

                if (LicenseStatus == (int)Shafer.LicenseStatus.PROD_CONCURRENT || LicenseStatus == (int)Shafer.LicenseStatus.PROD_NETWORK)
                {
                    //Return the license to the server to free up the activation for another client
                    string LicenseCode = GetLicenseCode();

                    if (LicenseCode == "")
                    {
                        MessageBox.Show("Licensecode is required to return the license");
                    }

                    ShaferError = oShafer.ReturnLicense(LicenseCode);
                    if (ShaferError != 0 && ShaferError != -1079 && ShaferError != -1080) // Forbidden by privacy setting
                    {

                        string ErrMess = "";
                        Shafer.GetErrorMessage(ShaferError, ref ErrMess);
                        MessageBox.Show(ErrMess);
                    }
                }
            }
            else
            {
                Environment.Exit(0);          }
        }
    

 

VB Example - Application End
       
        Private Sub ShaferExample_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            If Not JustExit Then
                Dim LicenseStatus As Integer = 0

                'ON Exit Check to see If concurrency License if so then return the license to the server to acheive concurrency
                ShaferError = oShafer.GetLicenseStatus(LicenseStatus)
                If ShaferError <> 0 Then

                    Dim ErrMess As String = ""
                    Shafer.GetErrorMessage(ShaferError, ErrMess)
                    MessageBox.Show(ErrMess)
                End If

                If LicenseStatus = CInt(Shafer.LicenseStatus.PROD_CONCURRENT) Then
                    'Return the license to the server to free up the activation for another client
                    Dim LicenseCode As String = GetLicenseCode()

                    If LicenseCode = "" Then
                        MessageBox.Show("Licensecode is required to return the license")
                    End If

                    ShaferError = oShafer.ReturnLicense(LicenseCode)
                    If ShaferError <> 0 AndAlso ShaferError <> -1079 AndAlso ShaferError <> -1080 Then ' Forbidden by privacy setting

                        Dim ErrMess As String = ""
                        Shafer.GetErrorMessage(ShaferError, ErrMess)
                        MessageBox.Show(ErrMess)
                    End If
                End If
                If (AppStarted = True) Then
                    ShaferError = oShafer.ApplicationEnd(Username)
                    If ShaferError <> 0 AndAlso ShaferError <> -1079 AndAlso ShaferError <> -1080 Then ' Forbidden by privacy setting

                        Dim ErrMess As String = ""
                        Shafer.GetErrorMessage(ShaferError, ErrMess)
                        MessageBox.Show(ErrMess)
                    End If
                End If
            Else
                Environment.Exit(0)
            End If
        End Sub
    

Exception Logging

 

Add code to call method Exception which you can find in the Shafer.cs class. This method takes the following parameters:
Function
Parameters
Description
ExceptionUsername (String)This is the recorded username of user running the application, if not required or available set to empty string.
 ExceptionCode (string)This is the errorcode of the exception thrown.
 Description (String)This is the description of the error thrown.
C# Example - Exception Logging
       
        private void ThrowError_Click(object sender, EventArgs e)
        {
            try
            {
                throw new System.ArgumentException("NSA Example Exeception", "NSA Example Application");
            }
            catch (Exception ex)
            {
                ShaferError = oShafer.Exception(Username, System.Runtime.InteropServices.Marshal.GetExceptionCode().ToString(), ex.Message.ToString());
                if (ShaferError != 0 && ShaferError != -1079 && ShaferError != -1080) // Forbidden by privacy setting
                {
                    string ErrMess = "";
                    Shafer.GetErrorMessage(ShaferError, ref ErrMess);
                    MessageBox.Show(ErrMess);
                }
            }
        }
    

 

VB Example - Exception Logging
       
        Private Sub ThrowError_Click(sender As Object, e As EventArgs) Handles ThrowError.Click
            Try
                Throw New System.ArgumentException("NSA Example Exeception", "NSA Example Application")
            Catch ex As Exception
                ShaferError = oShafer.Exception(Username, System.Runtime.InteropServices.Marshal.GetExceptionCode().ToString(), ex.Message.ToString())
                If ShaferError <> 0 AndAlso ShaferError <> -1079 AndAlso ShaferError <> -1080 Then ' Forbidden by privacy setting

                    Dim ErrMess As String = ""
                    Shafer.GetErrorMessage(ShaferError, ErrMess)
                    MessageBox.Show(ErrMess)
                End If
            End Try
        End Sub
    

Feature Use Logging


Add code to call the following functions which you can find in the Shafer.cs class to each feature in your application you would like to be recorded.
Function
Parameters
Description
GetFeatureStatusFeatureCode (String)The feature code setup on the NSA server to check for availability.
 TxnID (Uint32)Set to 0
FeatureStartUsername (String)This is the recorded username of user running the application, if not required or available set to empty string.
 FeatureCode (String)The feature code setup on the NSA server which records the required feature usage.
 TxnID (ref Uint32)Returned is the Transaction ID specific for this FeatureStart and must be passed to the corresponding FeatureEnd
FeatureEndUsername (String)This is the recorded username of user running the application, if not required or available set to empty string.
 FeatureCode (String)The feature code setup on the NSA server which records the required feature usage.
 TxnID (Uint32)The Transaction ID returned from the associated FeatureStart
  • If you just need to record a feature being triggered use FeatureStart, without FeatureEnd
  • If you need to be able to calculate how long a feature has ran for use FeatureStart AND FeatureEnd together as in the example shown
  • Use CheckoutFloatingFeature/CheckinFloatingFeature  to check out/in floating feature seats.
VB Example - Feature Logging
       
        Private Sub feature2ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles feature2ToolStripMenuItem.Click

            Dim FeatureStatus As Integer = 0
            Dim TxnID As UInt32 = 0
            'Get the license code for the Feature Checkout/Checkin
            Dim LicenseCode As String = GetLicenseCode()
            ShaferError = oShafer.CheckoutFloatingFeature(LicenseCode, "FEAT2", FeatureStatus)
            If ShaferError <> 0 Then

                Dim ErrMess As String = ""
                Shafer.GetErrorMessage(ShaferError, ErrMess)
                MessageBox.Show(ErrMess)
                Return
            End If

            If FeatureStatus <> 1 Then
                Select Case FeatureStatus
                    Case CInt(Shafer.FEATSTATUS.FEATSTATUS_DENIED)
                        MessageBox.Show("No seats available")
                        Return
                    Case Else
                        MessageBox.Show("Function not activated")
                        Return
                End Select
            End If

            ShaferError = oShafer.FeatureStart(Username, "FEAT2", TxnID)
            If ShaferError <> 0 AndAlso ShaferError <> -1079 AndAlso ShaferError <> -1080 Then ' Forbidden by privacy setting

                Dim ErrMess As String = ""
                Shafer.GetErrorMessage(ShaferError, ErrMess)
                MessageBox.Show(ErrMess)
                Return
            End If

            MessageBox.Show("Feature2Running")

            'Waste some time

            ShaferError = oShafer.CheckinFloatingFeature(LicenseCode, "FEAT2")
            If ShaferError <> 0 Then

                Dim ErrMess As String = ""
                Shafer.GetErrorMessage(ShaferError, ErrMess)
                MessageBox.Show(ErrMess)
            End If

            ShaferError = oShafer.FeatureEnd(Username, "FEAT2", TxnID)
            If ShaferError <> 0 AndAlso ShaferError <> -1079 AndAlso ShaferError <> -1080 Then ' Forbidden by privacy setting

                Dim ErrMess As String = ""
                Shafer.GetErrorMessage(ShaferError, ErrMess)
                MessageBox.Show(ErrMess)
            End If
        End Sub
    

 

C# Example - Feature Logging
     
        private void feature1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int FeatureStatus = 0;
            UInt32 TxnID = 0;
            ShaferError = oShafer.GetFeatureStatus("FEAT1", ref FeatureStatus, 0);

            if (ShaferError != 0 && FeatureStatus != -4)
            {

                string ErrMess = "";
                Shafer.GetErrorMessage(ShaferError, ref ErrMess);
                MessageBox.Show(ErrMess);
            }

            if (FeatureStatus != 1)
            {
                MessageBox.Show("Function not activated");
                return;
            }
            ShaferError = oShafer.FeatureStart(Username, "FEAT1", ref TxnID);
            if (ShaferError != 0 && ShaferError != -1079 && ShaferError != -1080) // Forbidden by privacy setting
            {

                string ErrMess = "";
                Shafer.GetErrorMessage(ShaferError, ref ErrMess);
                MessageBox.Show(ErrMess);
            }

            //DO Function Code

            ShaferError = oShafer.FeatureEnd(Username, "FEAT1", TxnID);
            if (ShaferError != 0 && ShaferError != -1079 && ShaferError != -1080) // Forbidden by privacy setting
            {

                string ErrMess = "";
                Shafer.GetErrorMessage(ShaferError, ref ErrMess);
                MessageBox.Show(ErrMess);
            }
        }  
    

User Logging


 Add code to call the following functions located in the Shafer.cs class in your code recording a user logon and logoff.
Function
Parameters
Description
LogUserOnUsername (String)This is the recorded username of the user logging on.
 TxnID (Uint32)Returned is the Transaction ID specific for this LogUserOn and must be passed to the corresponding LogUserOff
LogUserOffUsername (String)This is the recorded username of the user  logging off.
 TxnID (Uint32)The Transaction ID returned from the associated LogUserOn
C# Example - User Logging
     
        private void btnLogon_Click(object sender, EventArgs e)
        {
            ShaferError = oShafer.LogUserOn(Username, ref UserLoginTxnID);
            if (ShaferError != 0 && ShaferError != -1079 && ShaferError != -1080) // Forbidden by privacy setting
            {

                string ErrMess = "";
                Shafer.GetErrorMessage(ShaferError, ref ErrMess);
                MessageBox.Show(ErrMess);
            }
        }

        private void btnLogoff_Click(object sender, EventArgs e)
        {
            ShaferError = oShafer.LogUserOff(Username, UserLoginTxnID);
            if (ShaferError != 0 && ShaferError != -1079 && ShaferError != -1080) // Forbidden by privacy setting
            {

                string ErrMess = "";
                Shafer.GetErrorMessage(ShaferError, ref ErrMess);
                MessageBox.Show(ErrMess);
            }
        }
    

 

VB Example - User Logging
        Private Sub btnLogon_Click(sender As Object, e As EventArgs) Handles btnLogon.Click

            ShaferError = oShafer.LogUserOn(Username, UserLoginTxnID)
            If ShaferError <> 0 AndAlso ShaferError <> -1079 AndAlso ShaferError <> -1080 Then ' Forbidden by privacy setting

                Dim ErrMess As String = ""
                Shafer.GetErrorMessage(ShaferError, ErrMess)
                MessageBox.Show(ErrMess)
            End If

        End Sub

        Private Sub btnLogoff_Click(sender As Object, e As EventArgs) Handles btnLogoff.Click
            ShaferError = oShafer.LogUserOff(Username, UserLoginTxnID)
            If ShaferError <> 0 AndAlso ShaferError <> -1079 AndAlso ShaferError <> -1080 Then ' Forbidden by privacy setting

                Dim ErrMess As String = ""
                Shafer.GetErrorMessage(ShaferError, ErrMess)
                MessageBox.Show(ErrMess)
            End If

        End Sub
    

Build, Run and Test your Application

For trouble-shooting please see section Avoiding Potential Pitfalls - NSL  

 

Ensure the ShaferFilechck.dll library is in the same folder as your application's executable and then move on to the next step. In Visual Studio, this will, by default, be either the /bin/release or /bin/debug folder depending on whether your build configuration is or debug or release.

 

You have now seen how Nalpeiron licensing works within a demo application. The next step is to examine the licensing logic of this application in more depth so that you will have a greater understanding of how to implement it within your own application.

 

Licensing Logic of Your Application

THE SOFTWARE IS PROVIDED "AS IS." TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, NALPEIRON DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT WITH RESPECT TO THE SOFTWARE AND THE ACCOMPANYING DOCUMENTATION. YOU ASSUME RESPONSIBILITY FOR SELECTING THE SOFTWARE TO ACHIEVE YOUR INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED FROM THE SOFTWARE. WITHOUT LIMITING THE FOREGOING PROVISIONS, NALPEIRON MAKES NO WARRANTY THAT THE SOFTWARE WILL BE ERROR-FREE OR FREE FROM INTERRUPTIONS OR OTHER FAILURES OR THAT THE SOFTWARE WILL MEET YOUR REQUIREMENTS.

View the full Licence Agreement (English) - With the download or use of the software you agree to the licence agreement.

  • No labels