Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / Windows Forms

Windows Services Made Simple

Rate me:
Please Sign up or sign in to vote.
4.62/5 (10 votes)
27 Jun 2007CPOL10 min read 94.5K   6.9K   69  
Describes how to build a Windows Service using the Pegasus Library.
using System;
using System.Drawing;
using System.Runtime.InteropServices;

using Pegasus.Windows.API;
using Pegasus.Windows.API.Enumerations;
using Pegasus.Windows.API.Interfaces;
using Pegasus.Windows.API.Structures;

namespace Pegasus.Windows.Shell
{
	/// <summary>
	/// This class is the base class for all windows shell items.
	/// </summary>
	public abstract class ShellItem : IDisposable
	{
		// Local Instance Values
		private Pidl m_pidl = null;

		private string m_name = null;
		private Icon m_icon = null;
		private int m_imageIndex = -1;

		/// <summary>
		/// Initializes a new instance of the <see cref="ShellItem"/> class.
		/// </summary>
		public ShellItem( Pidl pidl )
		{
			InitizlizeShellItem( pidl );
		}

		/// <summary>
		/// Gets the point to the item identifier list (PIDL).
		/// </summary>
		/// <value>The PIDL.</value>
		protected Pidl Pidl
		{
			get
			{
				return m_pidl;
			}
		}

		/// <summary>
		/// Gets the name of the item.
		/// </summary>
		/// <value>The name.</value>
		public virtual string Name
		{
			get
			{
				return m_name;
			}
		}

		/// <summary>
		/// Gets the icon for this object.
		/// </summary>
		/// <value>The icon.</value>
		public virtual Icon Icon
		{
			get
			{
				return m_icon;
			}
		}

		/// <summary>
		/// Gets the index of the image in the System Image Lists.
		/// </summary>
		/// <value>The index of the image.</value>
		public virtual int ImageIndex
		{
			get
			{
				return m_imageIndex;
			}
		}

		/// <summary>
		/// Gets the root shell folder.
		/// </summary>
		/// <value>The root shell folder.</value>
		protected virtual IShellFolder RootShellFolder
		{
			get
			{
				return WindowsShell.RootShellFolder;
			}
		}

		/// <summary>
		/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
		/// </summary>
		public virtual void Dispose()
		{
			if( m_pidl != null )
			{
				m_pidl.Dispose();
				m_pidl = null;
			}
		}

		/// <summary>
		/// Initizlizes the shell item.
		/// </summary>
		/// <param name="pidl">The pidl for this item.</param>
		protected virtual void InitizlizeShellItem( Pidl pidl )
		{
			m_pidl = pidl;

			// Get the information about this object.
			SHFILEINFO fileInfo = new SHFILEINFO();
			Shell32.SHGetFileInfo( m_pidl, FILE_ATTRIBUTE.NONE, ref fileInfo, Marshal.SizeOf( typeof( SHFILEINFO ) ), 
				SHGFI.PIDL | SHGFI.DISPLAYNAME | SHGFI.ICON );

			// Initialize all the instance values
			m_name = fileInfo.DisplayName;
			m_icon = Icon.FromHandle( fileInfo.hIcon );
			m_imageIndex = fileInfo.IconIndex;

			// Make sure that the system lists are updated correctly.
			WindowsShell.NewShellItem( this );
		}
	}
}

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)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions