5,317,180 members and growing! (18,201 online)
Email Password   helpLost your password?
General Reading » Hardware & System » System     Intermediate License: The Common Public License Version 1.0 (CPL)

Managed Control Panel Items

By Lukasz Swiatkowski

Creating Control Panel items using the .NET Framework.
C# (C# 2.0, C# 3.0, C#), Windows (Windows, Win2K, WinXP, Win2003, Vista, TabletPC, Embedded), .NET, .NET 3.5, .NET 3.0, .NET 2.0, Win32, Dev

Posted: 11 Jul 2007
Updated: 6 Dec 2007
Views: 12,226
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
16 votes for this Article.
Popularity: 5.19 Rating: 4.31 out of 5
1 vote, 6.3%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
5 votes, 31.3%
4
10 votes, 62.5%
5

Screenshots

The Control Panel containing three managed items:

Control Panel which contains three managed items

An application which displays installed managed items:

Application which displays installed managed items

A sample application executed with a custom command string:

Sample application executed with a command string

Introduction

I have always wanted to create DLL-based Control Panel items using only C#. Of course, I couldn't accomplish that because the Visual C# compiler cannot export methods in the same way unmanaged compilers (Delphi, Visual C++) can. Some time ago, I found an article: "How to automate exporting .NET functions to unmanaged", which made it possible.

One can say that a Control Panel item can be created as an ordinary EXE-file. That is true. But each each EXE-based item has to be separately registered in the Windows registry. When an item is created (as a DLL file) using the classes I have developed, it only has to be copied to the particular folder (where a CplNet.dll file was registered).

Using the code

The classes I have created simplify creation of Control Panel items. To create a managed Control Panel item, one must create a class which implements an IControlPanelItem interface.

// Interface which must be implemented by each Control Panel item.
public interface IControlPanelItem : IDisposable
{
    // Icon of a Control Panes item.
    Icon Icon { get; }

    // Name of a Control Panes item (max. 32 characters).
    string Name { get; }

    // Description/tooltip/status bar string
    // of a Control Panes item (max. 64 characters).
    string Info { get; }

    // Shows the Control Panel item's window.
    //     controlPanelWindow – The control panel window handle.
    //     command            – The optional command string.
    void Start(IWin32Window controlPanelWindow, string command);
}

The class also must derive from the MarshalByRefObject class (this is required for cross-domain invocations) and have a ControlPanelItemAttribute attribute applied to it.

When all DLLs are compiled, they must be copied to some directory (%windir%\Microsoft.NET\ControlPanelItems is recommended).

If all files were copied to the recommended directory, then a Register.reg file can simply be executed to register the CplNet.dll file. Otherwise, the CplNet.dll file has to be registered manually, using the following command:

rundll32 %windir%\Microsoft.NET\ControlPanelItems\CplNet.dll,Register_RunDLL

(Of course, the appropriate path should be provided instead of that which is given above). If the command does not work (this sometimes happens, but I do not know why), a CplNetItem.dll file has to be copied to the GAC (the %windir%\assembly folder).

When an item is no longer needed, it can be simply deleted. When there are no items left, the CplNet.dll file can be unregistered (using an Unregister.reg file, or the command given below) and the whole folder can be deleted (and optionally, the CplNetItem.dll file can be removed from the GAC).

rundll32 %windir%\Microsoft.NET\ControlPanelItems\CplNet.dll,Unregister_RunDLL

How to execute an item with a custom command string

If one needs to run an item with a custom command string, then the following syntax must be used:

control.exe %windir%\Microsoft.NET\ControlPanelItems\CplNet.dll,@<i>,<p>

where <i> is the number of the item to run, and <p> is the command string.

Example:

control.exe %windir%\Microsoft.NET\ControlPanelItems\CplNet.dll,@0,ABC 123

If the number of the item is not known, then the "Managed Control Panel Items" tool can be used to obtain it. The first item listed by this tool has number 0 assigned, the second has 1, etc...

History

  • 1.0
    • 05.12.2007 – Added license.
    • 11.07.2007 – First version (Windows 2000, XP, 2003, and Vista compatible).

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)

About the Author

Lukasz Swiatkowski


Mvp
I'm studying computer science at Wroclaw University of Technology, Poland.

My interests: .NET, reading, programming, drawing, OBE and LD, Japan, yoga, tai-chi.
My favourite movie: "Star Trek: First Contact".

My website: www.lukesw.net
My email: lukasz.swiatkowski/*at-sign*/gmail.com.
Location: Poland Poland

Other popular Hardware & System articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralA life savermemberRichard Osafo9:18 30 Jan '08  
GeneralRe: A life savermvpLukasz Swiatkowski14:05 7 Apr '08  
GeneralVB.NETmemberGlimmerMan4:28 8 Jan '08  
GeneralRe: VB.NETmvpLukasz Swiatkowski8:20 8 Jan '08  
GeneralRe: VB.NETmemberGlimmerMan8:39 8 Jan '08  
GeneralThanks for the update but .....memberfwsouthern15:28 5 Dec '07  
GeneralRe: Thanks for the update but .....memberLukasz Swiatkowski0:12 7 Dec '07  
GeneralExquisite as alwaysmemberMichal Brylka20:12 11 Jul '07  
GeneralRe: Exquisite as alwaysmemberLukasz Swiatkowski3:16 12 Jul '07  
GeneralGood EffortmemberMoim Hossain17:59 11 Jul '07  
GeneralRe: Good EffortmemberLukasz Swiatkowski3:15 12 Jul '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Dec 2007
Editor: Smitha Vijayan
Copyright 2007 by Lukasz Swiatkowski
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project