Click here to Skip to main content
15,881,559 members
Articles / Desktop Programming / Win32

Managed Control Panel Items

Rate me:
Please Sign up or sign in to vote.
4.75/5 (24 votes)
22 Sep 2008CPL2 min read 76.6K   1.5K   96   14
Creating Control Panel items using the .NET Framework

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 Function to Unmanaged Programs, 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 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.

C#
// 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 must also 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
    • 11.07.2007 – First version (Windows 2000, XP, 2003, and Vista compatible)
    • 05.12.2007 – Added license

License

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


Written By
Software Developer
Poland Poland
I am a graduate of Wroclaw University of Science and Technology, Poland.

My interests: gardening, reading, programming, drawing, Japan, Spain.

Comments and Discussions

 
Generaltks Pin
pophelix24-Sep-08 5:35
pophelix24-Sep-08 5:35 
GeneralThanks Pin
logiCode23-Sep-08 3:19
logiCode23-Sep-08 3:19 
GeneralRe: Thanks Pin
Lukasz Sw.23-Sep-08 3:45
Lukasz Sw.23-Sep-08 3:45 
You're welcome. Smile | :)

~~~~~~~~~~~~~~~~~~~
My website: www.lukesw.net

GeneralA life saver Pin
Richard Osafo30-Jan-08 8:18
Richard Osafo30-Jan-08 8:18 
GeneralRe: A life saver Pin
Lukasz Sw.7-Apr-08 13:05
Lukasz Sw.7-Apr-08 13:05 
GeneralVB.NET Pin
karenpayne8-Jan-08 3:28
karenpayne8-Jan-08 3:28 
GeneralRe: VB.NET Pin
Lukasz Sw.8-Jan-08 7:20
Lukasz Sw.8-Jan-08 7:20 
GeneralRe: VB.NET Pin
karenpayne8-Jan-08 7:39
karenpayne8-Jan-08 7:39 
GeneralThanks for the update but ..... Pin
fwsouthern5-Dec-07 14:28
fwsouthern5-Dec-07 14:28 
GeneralRe: Thanks for the update but ..... Pin
Lukasz Sw.6-Dec-07 23:12
Lukasz Sw.6-Dec-07 23:12 
GeneralExquisite as always Pin
Michal Brylka11-Jul-07 19:12
Michal Brylka11-Jul-07 19:12 
GeneralRe: Exquisite as always Pin
Lukasz Sw.12-Jul-07 2:16
Lukasz Sw.12-Jul-07 2:16 
GeneralGood Effort Pin
Moim Hossain11-Jul-07 16:59
Moim Hossain11-Jul-07 16:59 
GeneralRe: Good Effort Pin
Lukasz Sw.12-Jul-07 2:15
Lukasz Sw.12-Jul-07 2:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.