title: Receive SENS events in .NET
body: <p>I want to receive SENS events when the screensaver goes on/off. My code is based on <a href="http://richardarthur.sys-con.com/node/105651?page=0,1" rel="nofollow">this</a> article:</p>
  
  <pre><code>private static readonly string GUID = "{" + typeof(ScreensaverHandler).GUID.ToString() + "}";
  
  [ComImport, Guid("4E14FBA2-2E22-11D1-9964-00C04FBBB345")]
  private class EventSystem { }
  
  [ComImport, Guid("7542E960-79C7-11D1-88F9-0080C7D771BF")]
  private class EventSubcription { }
  
  [ComImport, Guid("AB944620-79C6-11d1-88F9-0080C7D771BF")]
  private class EventPublisher { }
  
  [ComImport, Guid("cdbec9c0-7a68-11d1-88f9-0080c7d771bf")]
  private class EventClass { }
  
  public ScreensaverHandler() {
      IEventSystem es = (IEventSystem) new EventSystem();
      IEventSubscription sub = (IEventSubscription) new EventSubcription();
      sub.Description = "description";
      sub.SubscriptionName = "subscriptionname";
      sub.SubscriptionID = GUID;
      sub.InterfaceID = GetInterfaceGuid(typeof(SensEvents.ISensLogon));
      sub.SubscriberInterface = this; // implements SensEvents.ISensLogon 
      es.Store("EventSystem.EventSubscription", sub);
  }
  
  private static string GetInterfaceGuid(Type type) {
      object[] attributes = type.GetCustomAttributes(typeof(GuidAttribute), true);
      return "{" + ((GuidAttribute)attributes[0]).Value + "}";
  }
  </code></pre>
  
  <p>The problem is that <code>es.Store</code> throws a System.UnauthorizedAccessException with <code>Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))</code></p>