Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » How To » Downloads
 
Add your own
alternative version
Go to top

Enumerate and Host Control Panel Applets using C#.

, 15 Feb 2004
Demonstrates how to enumerate and host Windows Control Panel Applets using C# and unmanaged C++.
appletviewer_demo.zip
AppletViewer.exe
AppletEngine.dll
AppletProxy.dll
appletviewer_src.zip
AppletEngine
AppletEngine.csproj.user
bin
Debug
AppletEngine.dll
obj
Debug
temp
TempPE
AppletProxy
Debug
Exports.def
AppletViewer
App.ico
AppletViewer.csproj.user
bin
Debug
AppletEngine.dll
AppletProxy.dll
Release
AppletEngine.dll
AppletProxy.dll
AppletViewer.exe
obj
Debug
temp
TempPE
Release
temp
TempPE
using System;
using System.Drawing;
using System.Collections;
using System.Runtime.InteropServices;

namespace AppletEngine
{
	public enum BOOL
	{
		FALSE = 0,
		TRUE  = 1
	}	

	/// <summary>
	/// The AppletLibrary class exposes methods for loading and describing the individual Applets found in a Windows Control Panel library (*.cpl)
	/// </summary>
	public class AppletLibrary : DisposableObject
	{
		private IntPtr _library;
		private IntPtr _appletProc;
		private IntPtr _hWndCpl;
		private string _path;
		private ArrayList _applets;

		#region P/Invoke

		[DllImport("Kernel32")]
		public static extern IntPtr LoadLibrary(string path);

		[DllImport("Kernel32")]
		public static extern bool FreeLibrary(IntPtr hModule);

		[DllImport("Kernel32")]
		public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
		
		[DllImport("AppletProxy")]
		public static extern int ForwardCallToApplet(IntPtr appletProc, IntPtr hWndCpl, AppletMessages message, IntPtr lParam1, IntPtr lParam2);

		#endregion

		public override void Dispose(bool disposing)
		{
			base.Dispose (disposing);

			if (disposing)
			{

			}

			if (!base.IsNullPtr(_library))
			{
				/// as the control panel closes it sends the CPL_CLOSE message to each applet
				foreach(Applet applet in _applets)
					applet.Close();

				this.CPlApplet(AppletMessages.Exit);
				
				foreach(Applet applet in _applets)
					applet.Dispose(disposing);

				if (!FreeLibrary(_library))
				{
					System.Diagnostics.Trace.WriteLine("Failed to free the library '" + _path + "'");
				}

				_library = IntPtr.Zero;
				_appletProc = IntPtr.Zero;
			}
		}

		/// <summary>
		/// Initializes a new instance of the AppletLibrary class
		/// </summary>
		/// <param name="path"></param>
		/// <param name="hWndCpl"></param>
		public AppletLibrary(string path, IntPtr hWndCpl)
		{
			_path = path;
			_hWndCpl = hWndCpl;
			_applets = new ArrayList();

			if (!System.IO.File.Exists(path))
				throw new System.IO.FileNotFoundException("No applet could be found in the specified path.", path);

			_library = LoadLibrary(path);
			if (base.IsNullPtr(_library))
				throw new Exception("Failed to load the library '" + _path + "'");
			
			_appletProc = GetProcAddress(_library, "CPlApplet");
			if (base.IsNullPtr(_appletProc))
				throw new Exception("Failed to load CPlApplet proc for the library '" + _path + "'");	

			
			this.Initialize();
		}
        
		public IntPtr LibraryHandle
		{
			get
			{
				return _library;
			}
		}

		public string Path
		{
			get
			{
				return _path;
			}
		}

		public ArrayList Applets
		{
			get
			{
				return _applets;
			}
		}

		public int CPlApplet(AppletMessages message, IntPtr lParam1, IntPtr lParam2)
		{
			return ForwardCallToApplet(_appletProc, _hWndCpl, message, lParam1, lParam2);
		}

		public int CPlApplet(AppletMessages message)
		{
			return ForwardCallToApplet(_appletProc, _hWndCpl, message, IntPtr.Zero, IntPtr.Zero);
		}

		public void Initialize()
		{
			if (this.CPlApplet(AppletMessages.Initialize, IntPtr.Zero, IntPtr.Zero) == (int)BOOL.TRUE)
			{
				int count = this.CPlApplet(AppletMessages.GetCount, IntPtr.Zero, IntPtr.Zero);
			
		//				System.Diagnostics.Trace.WriteLine(string.Format("{0} applets found in '{1}'", count, _path));				
						
				for(int i = 0; i < count; i++)
				{
					Applet applet = new Applet(this, i);
					System.Diagnostics.Trace.WriteLine(applet.ToString());
					_applets.Add(applet);
				}			
			}
		}		
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Mark Belles
Software Developer (Senior)
United States United States
Senior Application Developer specializing in Windows desktop and network development.
 
Professional Experience
- B.S. of Computer Science (Graduated 2001 - PSU)
- Senior Application Developer (8+ yrs)
- Microsoft Certified Professional
 
Primary Interests
- C#, C++, HTML, Javascript
- XML, ASP.NET, Web Services, SOAP, UDDI
- Socket programming and anything network related
- Reflection, Serialization, and Plugin Frameworks
- Owner-drawn controls and GDI+ goodness

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 16 Feb 2004
Article Copyright 2004 by Mark Belles
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid