|
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.
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