Click here to Skip to main content
11,576,060 members (50,478 online)
Click here to Skip to main content
Add your own
alternative version

Monitoring desktop windows from a Windows service

, 2 Jan 2008 CPOL 45.7K 3.7K 109
Capture and save desktop windows from a Windows service.
ScreenMonitor_src.zip
ScreenMonitor
ScreenMonitorLib
Properties
ScreenMonitor
ScreenmonitorService
ScreenmonitorService
SnapShotManager
Properties
Settings.settings
DataSources
ScreenMonitorLib.SnapShotDS+ApplicationsDataTable.datasource
Debug
ScreenMonitorLib.SnapShotDS+SnapShotDataTable.datasource
SnapShotDS.xsc
ScreenMonitorLib
Settings.settings
SnapShotDS.xss
Release
Release
ScreenMonitorLib.SnapShotDS+WndSettingsDataTable.datasource
SetupMonitor
SetupMonitor.vdproj
Properties
Debug
screenmonitor_src2.zip
Settings.settings
ScreenMonitorLib.SnapShotDS+ApplicationsDataTable.datasource
ScreenMonitorLib.SnapShotDS+SnapShotDataTable.datasource
SnapShotDS.xsc
Settings.settings
SnapShotDS.xss
ScreenMonitorLib.SnapShotDS+WndSettingsDataTable.datasource
SetupMonitor.vdproj
SetupMonitor_msi.zip
SetupMonitor.msi
setupmonitor_msi2.zip
SetupMonitor.msi
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using Microsoft.Win32;
namespace ScreenmonitorService
{
    enum SystemCategory : short
    {
        None = 0,
        Devices,
        Disk,
        Printers,
        Services,
        Shell,
        SystemEvent,
        Network

    }
    public partial class ScreenMonitor : ServiceBase
    {
        //private System.Diagnostics.EventLog _eventLog;
        public ScreenMonitor()
        {
            InitializeComponent();
            //this.EventLog.Source = "Screen Monitor";
            //this.EventLog.Log = EventLog.Source;

            // These Flags set whether or not to handle that specific
            //  type of event. Set to true if you need it, false otherwise.
            //this.CanHandlePowerEvent = true;
            //this.CanHandleSessionChangeEvent = true;
            //this.CanPauseAndContinue = true;
            //this.CanShutdown = true;
            //this.CanStop = true;

            //if (!System.Diagnostics.EventLog.SourceExists(EventLog.Source))
            //    System.Diagnostics.EventLog.CreateEventSource(
            //       EventLog.Source, "Application");

            //System.Diagnostics.EventLog _eventLog =
            //   new System.Diagnostics.EventLog();
            //_eventLog.Source = "ExistingSourceString";
            //_eventLog.WriteEntry("TestEntry2");


        }

#if(DEBUG)
        public void StartDebugService()
        {
            //start debugger if needed
            if (!System.Diagnostics.Debugger.IsAttached)
            {

                System.Diagnostics.Debugger.Launch();
            }
            //System.Diagnostics.Debugger.Log(1, "info", "starting with the debugger attached");
            //using (RegistryKey ckey = Registry.LocalMachine.OpenSubKey(
            //    string.Format(@"SYSTEM\CurrentControlSet\Services\{0}", this.ServiceName), true))
            //{
            //    // Good to always do error checking!
            //    if (ckey != null)
            //    {
            //        // Ok now lets make sure the "Type" value is there, 
            //        //and then do our bitwise operation on it.
            //        if (ckey.GetValue("Type") != null)
            //        {
            //            ckey.SetValue("Type", ((int)ckey.GetValue("Type") | 256));
            //        }
            //    }
            //}
            OnStart(Environment.GetCommandLineArgs());
        }
#endif
        protected override void OnStart(string[] args)
        {
            //EventLog.WriteEntry("plain string entry");
            //EventLog.WriteEntry(this.ServiceName, " 'service name = this.ServiceName' +plain string entry + EventLogEntryType.Warning", EventLogEntryType.Warning);
            //EventLog.WriteEntry(this.ServiceName, " 'service name = this.ServiceName' +plain string entry + EventLogEntryType.Warning event id =1, categ id = 0", EventLogEntryType.Warning, 1, 0);
            //EventLog.WriteEntry(this.ServiceName, " 'service name = this.ServiceName' +plain string entry + EventLogEntryType.Warning event id =1, categ id = 1", EventLogEntryType.Warning, 1, 1);
            //EventLog.WriteEntry(this.ServiceName, " 'service name = this.ServiceName' +plain string entry + EventLogEntryType.Warning event id =1, categ id = 2", EventLogEntryType.Warning, 1, 2);
            //EventLog.WriteEntry(this.ServiceName, " 'service name = this.ServiceName' +plain string entry + EventLogEntryType.Warning event id =1, categ id = 3", EventLogEntryType.Warning, 1, 3);
            //EventLog.WriteEntry(this.ServiceName, " 'service name = this.ServiceName' +plain string entry + EventLogEntryType.Error event id =2, categ id = 4", EventLogEntryType.Error, 1, 4);
            //EventLog.WriteEntry(this.ServiceName, " 'service name = this.ServiceName' +plain string entry + EventLogEntryType.error event id =3, categ id = 5", EventLogEntryType.Error, 1, 5);
            //EventLog.WriteEntry("other source", " 'service name = other source' +plain string entry + EventLogEntryType.SuccessAudit", EventLogEntryType.SuccessAudit);

            //for (SystemCategory i = SystemCategory.None; i <= SystemCategory.Network; i++)
            //{
            //    EventLog.WriteEntry("test source id", " service name = 'test source id' + EventLogEntryType.Information event id = " + i.ToString() + ", categ id = " + i.ToString(), EventLogEntryType.Information, Convert.ToInt32(i), Convert.ToInt16(i));
            //}
            if(ScreenMonitorLib.SnapShot.SaveSnapShot())
                EventLog.WriteEntry(this.ServiceName, "Got the image", EventLogEntryType.Information, 1, Convert.ToInt16(SystemCategory.Shell));
            else
                EventLog.WriteEntry(this.ServiceName, "did not get the image", EventLogEntryType.Error, 1, Convert.ToInt16(SystemCategory.Shell));

        }

        /// <summary>
        /// OnStop: Put your stop code here
        /// - Stop threads, set final data, etc.
        /// </summary>
        protected override void OnStop()
        {
            base.OnStop();
        }

        /// <summary>
        /// OnPause: Put your pause code here
        /// - Pause working threads, etc.
        /// </summary>
        protected override void OnPause()
        {
            base.OnPause();
        }

        /// <summary>
        /// OnContinue: Put your continue code here
        /// - Un-pause working threads, etc.
        /// </summary>
        protected override void OnContinue()
        {
            base.OnContinue();
        }

        /// <summary>
        /// OnShutdown(): Called when the System is shutting down
        /// - Put code here when you need special handling
        ///   of code that deals with a system shutdown, such
        ///   as saving special data before shutdown.
        /// </summary>
        protected override void OnShutdown()
        {
            base.OnShutdown();
        }

        /// <summary>
        /// OnCustomCommand(): If you need to send a command to your
        ///   service without the need for Remoting or Sockets, use
        ///   this method to do custom methods.
        /// </summary>
        /// <param name="command">Arbitrary Integer between 128 & 256</param>
        protected override void OnCustomCommand(int command)
        {
            //  A custom command can be sent to a service by using this method:
            //#  int command = 128; //Some Arbitrary number between 128 & 256
            //#  ServiceController sc = new ServiceController("NameOfService");
            //#  sc.ExecuteCommand(command);

            base.OnCustomCommand(command);
        }

        /// <summary>
        /// OnPowerEvent(): Useful for detecting power status changes,
        ///   such as going into Suspend mode or Low Battery for laptops.
        /// </summary>
        /// <param name="powerStatus">The Power Broadcase Status (BatteryLow, Suspend, etc.)</param>
        protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
        {
            return base.OnPowerEvent(powerStatus);
        }

        /// <summary>
        /// OnSessionChange(): To handle a change event from a Terminal Server session.
        ///   Useful if you need to determine when a user logs in remotely or logs off,
        ///   or when someone logs into the console.
        /// </summary>
        /// <param name="changeDescription"></param>
        protected override void OnSessionChange(SessionChangeDescription changeDescription)
        {
            base.OnSessionChange(changeDescription);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

dmihailescu
Software Developer (Senior)
United States United States
Decebal Mihailescu is a software engineer with interest in .Net, C# and C++.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 2 Jan 2008
Article Copyright 2007 by dmihailescu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid