Developer Reference Monitoring the Dongle

The ENCX licensing system (either a hardware or software Dongle) runs on another thread. This allows applications to start up smoothly even if they are licensed by a slow dongle - perhaps a network dongle on a remote machine.

The asynchronous nature of the dongle does make many applications more complex though. For example, you will not be able to open a chart until you have an active dongle and the dongle may become active some seconds after the rest of the application is synchronised.

The sample application receives change events from the dongle and displays an appropriate message. You might actually want to use these facilities to disable/enable menu options and so on in your application.

The dongle has three states: nonexistent, existent and active. The normal behaviour on a properly licensed system is for it to go through these states in order and then remain Active. It is possible to go from Active to nonexistent if a hardware dongle falls out of its socket or a DFS file is deleted.

The dongle state, an enumeration, is accessed from the Dongle object (accessed from the Library object). To get change notifications you will need to use a DongleObserver object.

Example code in C#

The sample is called DotNetDongleMonitoring - receiving events in .NET languages is magically simple and Visual Studio actually helps cast the magic spells.

The form in the sample holds a DongleObserver object (m_notify). By calling Register it asks to receive notifications from the dongle object owned by the library object (m_library).

The next bit is magic - it adds a handle to the dongle change event to the DongleObserver object so that m_notify_OnDongleChangeEvt() is called when the dongle changes state. Visual Studio takes care of adding this code:

  • Type m_notify.
  • Select OnDongleChangeEvt from the list and type +=
  • Press tab a few times to add the rest.
private void Form1_Load(object sender, System.EventArgs e)
{
    m_library = new ENCX.Library();
    m_notify = new ENCX.DongleObserver();
    m_library.Construct("","");
    m_notify.Register(m_library.Dongle);
    // Magic!
    m_notify.OnDongleChangeEvt += new ENCX._IDongleObserverEvents_OnDongleChangeEvtEventHandler(m_notify_OnDongleChangeEvt);
    UpdateDongleState();
}

private void m_notify_OnDongleChangeEvt()
{
    UpdateDongleState();
}

private void UpdateDongleState()
{
    switch(m_library.Dongle.State)
    {
        case ENCX.DongleState.DS_NotFound:
            labDongleState.Text = "No Dongle found, please wait"; break;
        case ENCX.DongleState.DS_Exists:
            labDongleState.Text = "A Dongle has been found, please wait"; break;
        case ENCX.DongleState.DS_Active:
            labDongleState.Text = "Dongle is Active"; break;
    }
}

Example code in VB.NET

In VB.NET we can use the WithEvents and Handles keyword instead of the "magic" C# bit.

Public Class Form1

    Private library As New ENCX.Library
    Private WithEvents observer As New ENCX.DongleObserver

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        library.Construct("", "")
        observer.Register(library.Dongle)
        DisplayDongleState()
    End Sub

    Private Sub DongleChanged() Handles observer.OnDongleChangeEvt
        DisplayDongleState()
    End Sub

    Private Sub DisplayDongleState()
        Select Case library.Dongle.State
            Case ENCX.DongleState.DS_NotFound
                Label1.Text = "No Dongle found, please wait"
            Case ENCX.DongleState.DS_Exists
                Label1.Text = "A Dongle has been found, please wait"
            Case ENCX.DongleState.DS_Active
                Label1.Text = "Dongle is Active"
        End Select
    End Sub

End Class