Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I need to find the total working hours of user on a system. So I need to monitor login, logoff, screen lock and screen unlock events. Then I can calculate the total working hours.

I found some code about register with COM and also SENS Interface methods.

But I don't know how to implement windows applications using c#.

Event Register Class

C#
#region Usage of EventSystemLib

[ComImport, Guid("4E14FBA2-2E22-11D1-9964-00C04FBBB345")]
class EventSystem { }

[ComImport, Guid("7542E960-79C7-11D1-88F9-0080C7D771BF")]
class EventSubscription { }

[ComImport, Guid("AB944620-79C6-11d1-88F9-0080C7D771BF")]
class EventPublisher { }

[ComImport, Guid("cdbec9c0-7a68-11d1-88f9-0080c7d771bf")]
class EventClass { }

#endregion
class EventRegister
{
    private const string PROGID_EventSubcription = "EventSystem.EventSubription";
    static EventRegister() { }

    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 subscriptionObject, Type subscriptionType)
    {
        //activate the subscriber
        try
        {
            //Create and populate a subscription object
            IEventSubscription sub= new EventSubscription () as IEventSubscription;
            sub.Description = description;
            sub.SubscriptionName = subscriptionName;
            sub.SubscriptionID = subscriptionID;

            //Get the GUID from the ISensLogon Interface
            sub.InterfaceID = GetInterfaceGuid(subscriptionType);
            sub.SubscriberInterface = subscriptionObject;

            //Store the actual Event
            EventSystem.Store(PROGID_EventSubcription, sub);

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

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

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


Here is my SensLogin Class

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

        #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();
                    }
                }
                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 = null;
                }
            }

            return original;
        }

        #endregion

        #region ISensLogon Event Raising Members

        internal  static void OnDisplayLock(string bstrUserName)
        {
            if (SensLogon.displayLock != null)
            {
                SensLogon.displayLock(bstrUserName);
            }
        }

        internal  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

        #region Main Method

        static void Main(string[] args)
        {
            SensLogon.DisplayLock += new SensLogonEventHandler(SensLogon_DisplayLock);

            SensLogon.DisplayUnlock += new SensLogonEventHandler(SensLogon_DisplayUnlock);

            Console.WriteLine("Registered For SENS Events");
            Console.ReadLine();
        }

        static void SensLogon_DisplayUnlock(string userName)
        {
            Console.WriteLine("Screen Locked:" + userName);            
        }

        static void SensLogon_DisplayLock(string userName)
        {
            Console.WriteLine("Screen UnLocked:" + userName);            
        }

        #endregion

    }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900