.NET example
A simple .NET application that checks-out a single license
2025-12-08
QuidLM staff
There are only two essential steps in using QuidLM to implement licensing in your application. The security, validity, and license content are managed by QuidLM. To use that funtionality, you need to
-
Initialize QuidLM. This is done by creating an
Apiobject from theQlmnamespace. You can use this opportunity to provide the user's credentials. They come into play because licenses can be configured as available only to a subset of users. You are free to set the user email and password tonullif you do not intend to use this functionality. If you initialize the license system without specifying user email and password, QuidLM will never give those licenses to your application.string user_email = null, password = null; var api = new Qlm.Api(user_email, password); // can also omit the arguments - they are null by defaultIf the initialization encounters an error, the
Qlm.LicenseInitErrorexception is thrown, and its.Messageproperty explains the issue. -
Grab the license. The
api.CheckoutLicensemethod takes two inputs - the license name and version. The license name is typically just the name of your product. The license version, if provided, should be in themajor.minorformat. The QuidLM will treat this input as the minimal license version that your current application is willing to accept. You normally pass the current version of your application for this parameter. That way, newer licenses enable older applications, but not vice versa, which is typically what you want. If for some reason that is not what you want, you can hard-code that version to "1.0" or some other constant.string product_name = "MyProduct", min_version = "3.1"; Qlm.License license = api.CheckoutLicense(product_name, min_version);If the requested license is not available,
Qlm.LicenseCheckoutErrorexception will be thrown, and your application may want to catch it to explain the error to the user and exit. Otherwise, theQlm.Licenseobject contains the useful information about the license, such as its expiration date (license.expiry) and thelicense.propertiesproperty holds a Dictionary of license parameters - a set of key-value pairs, which you, the developer, can use to set the application's behavior. For example, you can enable some features based on the presence of certain parameters: e.g.,trial-license=truemay prompt your application to display the "trial copy" badge somewhere. One other typical use of such a parameter is to limit the application performance, as inmax-threads=10.The license is released when the
licenseobject is destroyed or itsDisposemethod is called.
The above are the necessary parts, but you can take advantage of optional features, such as logging and
license revocation. Those additional features are demonstrated in sample applications shipped with QuidLM.
The dotnet_client application is a more full-fledged example, which can also
be used for testing. It is a command line application that grabs the license specified
on the command line for the specified number of seconds and prints the licensing information. The
comments in the source code explain the standard functionality, such as the initialization and the license checkout
mentioned above, and introduce the logging and the license revocation. The example is cross-platform - QuidLM comes
with libqlm_client.so and qlm_client.dll libraries to make it work on both Windows and UNIX systems.
/// Dotnet_client application: grab the specified license for
/// the specified length of time and print the licensing information.
using System;
using System.Threading; // for Thread.Sleep
namespace QlmExample
{
class LicenseClient
{
static int Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine(
"Expected some inputs on the command line.\n\n" +
" product_name: the product that you need the license for [required]\n" +
" min_version: the minimum version of the product, in the major.minor format [1.0]\n" +
" duration: exit application after this many seconds [2]\n");
return 1;
}
string product_name = args[0];
string minimum_product_version = args.Length < 2 ? "1.0" : args[1];
int duration = args.Length < 3 ? 2 : int.Parse(args[2]);
// log licensing errors
// Qlm.Api.SetLogLevel(Qlm.LogLevel.err);
// alternatively, set the environment variable: QLM_LOG_LEVEL=err
// Licenses can be configured as available only to a set of users.
// If you initialize the license system without specifying user email and password, you will never receive those licenses.
string? user_email = null, password = null;
var api = new Qlm.Api(user_email, password);
try
{
using (var license = api.CheckoutLicense(product_name, minimum_product_version))
{
Console.WriteLine($"Got license for product version {license.product_version}.");
Console.WriteLine($"Expiring {license.expiry}.\nFloating: {license.is_floating}.\nLicense properties:");
foreach (var kv in license.properties)
Console.WriteLine($" {kv.Key}: {kv.Value}");
Thread.Sleep(duration * 1000);
Console.WriteLine("Returning the license");
// the license is released when it is destroyed or its Dispose method is called
}
}
catch (Qlm.LicenseCheckoutError exc)
{
Console.WriteLine($"Exception: {exc}");
return 2;
}
return 0;
}
}
}