Listing 1: Initial SENS Event Code

static void Main(string[] args) {
  SENSClass sens = new SENSClass();
  sens.DisplayLock += new ISensLogon_DisplayLockEventHandler(sens_DisplayLock);
  sens.DisplayUnlock += new ISensLogon_DisplayUnlockEventHandler(sens_DisplayUnlock);
  Console.WriteLine("Registered For SENS Events");
  Console.ReadLine();
}
private static void sens_DisplayLock(string bstrUserName) {
  Console.WriteLine("Screen Locked: " + bstrUserName);
}
private static void sens_DisplayUnlock(string bstrUserName) {
  Console.WriteLine("Screen Unlocked: " + bstrUserName);
}

Listing 2: Event System Interop Code

//Using Statements
using System.Diagnostics;
using System.Runtime.InteropServices;
using EventSystemLib;
//In the ManagesSENS namespace
[ComImport, Guid("4E14FBA2-2E22-11D1-9964-00C04FBBB345")]
class EventSystem {}
[ComImport, Guid("7542E960-79C7-11D1-88F9-0080C7D771BF")]
class EventSubcription {}
[ComImport, Guid("AB944620-79C6-11d1-88F9-0080C7D771BF")]
class EventPublisher {}
[ComImport, Guid("cdbec9c0-7a68-11d1-88f9-0080c7d771bf")]
class EventClass {}

Listing 3: EventSystemRegistrar Code

private const string PROGID_EventSubscription = "EventSystem.EventSubscription";
static EventSystemRegistrar(){}

private static IEventSystem es = null;
private static IEventSystem EventSystem {
  get {
    if(es == null)
      es = new EventSystem() as IEventSystem;
    return es;
  }
}

public static void SubscribeToEvents(string description, string subscriptionName, string
 subscriptionID, object subscribingObject, Type subscribingType) {
  // activate subscriber
  try {
    //create and populate a subscription object
    IEventSubscription sub = new EventSubcription() as IEventSubscription;
    sub.Description = description;
    sub.SubscriptionName = subscriptionName;
    sub.SubscriptionID = subscriptionID;
    //Get the GUID from the ISensLogon interface
    sub.InterfaceID = GetInterfaceGuid(subscribingType);
    sub.SubscriberInterface = subscribingObject;
    //Store the actual Event.
    EventSystem.Store(PROGID_EventSubscription, sub);
  } catch(Exception ex) {
    Console.WriteLine(ex);
  }
}

private static string GetInterfaceGuid(Type theType) {
  object[] attributes = theType.GetCustomAttributes(typeof(GuidAttribute), true);
  if(attributes.Length > 0) {
    return "{" + ((GuidAttribute)attributes[0]).Value + "}";
  } else {
    throw new ArgumentException("GuidAttribute not present on the Type.", "theType");
  }
}

public static void UnsubscribeToEvents(string subscriptionID) {
  try {
    string strCriteria = "SubscriptionID == " + subscriptionID;
    int errorIndex = 0;
    EventSystem.Remove("EventSystem.EventSubscription", strCriteria, out errorIndex);
  } catch(Exception ex) {
    Console.WriteLine(ex);
  }
}

Listing 4: SensLogonInterop inner helper class

private class SensLogonInterop : ISensLogon, IDisposable
{
  private const string SubscriptionViewerName = "ManagedSENS.SensLogonInterop";
  private static string SubscriptionViewerID = "{" +
   typeof(SensLogonInterop).GUID.ToString().ToUpper() + "}"; // generate a subscriptionID 
   private const string SubscriptionViewerDesc = "ManagedSENS Event Subscriber";

  private bool registered;

  public SensLogonInterop() {
    registered = false;
    EventSystemRegistrar.SubscribeToEvents(SubscriptionViewerDesc, SubscriptionViewerName,
	 SubscriptionViewerID, this, typeof(ISensLogon));
    registered = true;
  }

  #region Cleanup Code

  ~SensLogonInterop() {
    this.Dispose(false);
  }

  public void Dispose() {
    this.Dispose(true);
  }

  protected void Dispose(bool isExplicit) {
    this.Deactivate();
  }

  private void Deactivate() {
    if(registered) {
      EventSystemRegistrar.UnsubscribeToEvents(SubscriptionViewerID);
      registered = false;
    }
  }

  #endregion

  #region ISensLogon Members

  public void DisplayLock(string bstrUserName) {
    SensLogon.OnDisplayLock(bstrUserName);
  }
  public void DisplayUnlock(string bstrUserName) {
    SensLogon.OnDisplayUnlock(bstrUserName);
  }
  //...More ISensLogon memmbers
  #endregion
}

Listing 5: SensLogon Class

public delegate void SensLogonEventHandler(string userName);
public class SensLogon
{
  private static SensLogonInterop eventCatcher;
  static SensLogon() {}

  //...SensLogonInterop goes here

#region Event Registration Code

  private static int registerCount = 0;
  private static bool IsRegistered {
    get {
      return (registerCount > 0);
    }
  }

  private static SensLogonEventHandler RegisterEvent(SensLogonEventHandler original,
   SensLogonEventHandler newDel) {
    bool shouldRegister = (original == null);
    original = original + newDel;
    if(shouldRegister) {
      if(registerCount <= 0) {
        if(SensLogon.eventCatcher == null)
          SensLogon.eventCatcher = new SensLogonInterop();
        registerCount = 1;
      } else {
        //Just count them.
        registerCount ++;
      }
    }
    return original;
  }

  private static SensLogonEventHandler UnregisterEvent(SensLogonEventHandler original,
   SensLogonEventHandler oldDel) {
    original = original - oldDel;
    if(original == null) {
      registerCount --;
      if(registerCount == 0) {
        //unregister for those events.
        SensLogon.eventCatcher.Dispose();
        SensLogon.eventCatcher = null;
      }
    }
    return original;
  }

  #endregion

  #region ISensLogon Event Raising Members

  protected static void OnDisplayLock(string bstrUserName) {
    if(SensLogon.displayLock != null)
      SensLogon.displayLock(bstrUserName);
  }
  protected static void OnDisplayUnlock(string bstrUserName) {
    if(SensLogon.displayUnlock != null)
      SensLogon.displayUnlock(bstrUserName);
  }
  ///...

  #endregion

  #region Event Declarations

  private static SensLogonEventHandler displayLock = null;
  private static SensLogonEventHandler displayUnlock = null;
  ///...

  public static event SensLogonEventHandler DisplayLock {
    add {
      SensLogon.displayLock = SensLogon.RegisterEvent(SensLogon.displayLock, value);  }
    remove {
      SensLogon.displayLock = SensLogon.UnregisterEvent(SensLogon.displayLock, value);  }
  }
  public static event SensLogonEventHandler DisplayUnlock {
    add {
      SensLogon.displayUnlock = SensLogon.RegisterEvent(SensLogon.displayUnlock, value);  }
	   remove {
      SensLogon.displayUnlock = SensLogon.UnregisterEvent(SensLogon.displayUnlock, value);  }
  }
  ///...
  #endregion
}

Listing 6: SensLogon Class

static void Main(string[] args) {
  ManagedSENS.SensLogon.DisplayLock += new
   ManagedSENS.SensLogonEventHandler(SensLogon_DisplayLock);
  ManagedSENS.SensLogon.DisplayUnlock += new
   ManagedSENS.SensLogonEventHandler(SensLogon_DisplayUnlock);
  Console.WriteLine("Registered For SENS Events");
  Console.ReadLine();
}
private static void SensLogon_DisplayLock(string userName) {
  Console.WriteLine("Screen Locked: " + userName);
}
private static void SensLogon_DisplayUnlock(string userName) {
  Console.WriteLine("Screen Unlocked: " + userName);
}