PGSLIC.lib  3.0
LicenseHelper.cs
/*
* LicenseHelper.cs
*
* An example C# wrapper for using PGSLIC in .NET applications.
*
* This example is for an application that has a license setting called
* MaxLimit.
*
*/
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Text;
namespace RomanNumerals
{
public class LicenseHelper
{
const string PWD = "---Replace with your Developer Key---";
[DllImport("pgslicmt.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern pgslic_status checkLicense(byte[] path, byte[] pwd, byte[] key, byte[] appName, byte[] appInfo, byte[] cxnOpts);
[DllImport("pgslicmt.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern System.IntPtr getAppName();
[DllImport("pgslicmt.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern System.IntPtr getClientName();
[DllImport("pgslicmt.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern System.IntPtr getExpiryDate();
[DllImport("pgslicmt.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern bool isPerpetual();
[DllImport("pgslicmt.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern System.IntPtr getSetting(byte[] settingName);
[DllImport("pgslicmt.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern System.IntPtr getLastError();
public bool CheckLicense(string licDir, string key, string appName)
{
switch (checkLicense(Encoding.ASCII.GetBytes(licDir), Encoding.ASCII.GetBytes(PWD), Encoding.ASCII.GetBytes(key), Encoding.ASCII.GetBytes(appName), Encoding.ASCII.GetBytes(""), Encoding.ASCII.GetBytes("")))
{
case pgslic_status.LICENSE_OK:
break;
case pgslic_status.LICENSE_ERROR:
System.Windows.Forms.MessageBox.Show("License not valid: " + Marshal.PtrToStringAnsi(getLastError()), "License", MessageBoxButtons.OK, MessageBoxIcon.Hand);
return false;
case pgslic_status.LICENSE_EXPIRED:
System.Windows.Forms.MessageBox.Show("License expired.\nClick on OK to activate or Cancel to exit.", "License", MessageBoxButtons.OKCancel, MessageBoxIcon.Hand);
return false;
default:
throw new Exception("Unexpected license status value"); // Never happens
}
return true;
}
public void ShowLicenseInfo()
{
string licenseInfo = Marshal.PtrToStringAnsi(getAppName()) + " 1.0\n";
licenseInfo += "Licensed to " + Marshal.PtrToStringAnsi(getClientName()) + "\n";
if (isPerpetual())
{
licenseInfo += "License never expires\n";
}
else
{
licenseInfo += "License expires on " + Marshal.PtrToStringAnsi(getExpiryDate()) + "\n";
}
licenseInfo += "Maximum number = " + Marshal.PtrToStringAnsi(getSetting(Encoding.ASCII.GetBytes("MaxLimit")));
System.Windows.Forms.MessageBox.Show(licenseInfo, "License Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public int GetMaxLimit()
{
return Convert.ToInt32(Marshal.PtrToStringAnsi(getSetting(Encoding.ASCII.GetBytes("MaxLimit"))));
}
}
}
PGSLIC_API const char * getSetting(const char *settingName)
PGSLIC_API int isPerpetual()
PGSLIC_API const char * getExpiryDate()
PGSLIC_API const char * getAppName()
@ LICENSE_STALE
Definition: pgslic.h:96
@ LICENSE_WARNING
Definition: pgslic.h:96
@ LICENSE_EXPIRED
Definition: pgslic.h:96
@ LICENSE_OK
Definition: pgslic.h:96
@ LICENSE_ERROR
Definition: pgslic.h:96
PGSLIC_API const char * getClientName()
PGSLIC_API const char * getLastError()
PGSLIC_API LicenseStatus checkLicense(const char *licPath, const char *pwd, const char *key, const char *appName, const char *appInfo="", const char *cxnOpts="")