Introduction
Control Panel Applets are very usefull when you have hardware or software that is configured systemwide and not related to the user who is logged in.
There are many posts on how to build this applets but I found myself in troubles the first time that I tried to build my own (an applet that is used to configure a .NET Windows Service).
I decided to share my experience with the community by posting this simple (I hope) article that will lead you step by step on how to build your own control panel applet.
Background
Some background in building a VC++ application might be usefull, but it is not a must.
The steps to create the applet
- Create a Win32 project as a Dynamic Link Library (dll). You will use the MFC as a shared library.
- Add the following includes to the stdafx.h:
#include <afxwin.h>
#include <afxext.h>
- Add the .def file. It is here where you are going to export the CPlApplet method that Windows will look for.
LIBRARY "ControlPanelApplet"
EXPORTS CPlApplet
- Create a header file (.h) for your class and add:
#include "cpl.h";
This Windows header contains definitions you need for your applet.
- Also in your header file you'll need to declare:
- The class that will handle the invocations to the control panel applet (inherits from CWinApp)
- The method that we are exporting from this dll:
static LONG APIENTRY CPlApplet(HWND hWnd, UINT uMsg, LONG lParam1, LONG lParam2);
- And the following private members:
private:
static CControlPanelApplet * m_pThis;
LONG OnDblclk(HWND hWnd, UINT uAppNum, LONG lData);
LONG OnExit();
LONG OnGetCount();
LONG OnInit();
LONG OnInquire(UINT uAppNum, CPLINFO* pInfo);
LONG OnNewInquire(UINT uAppNum, NEWCPLINFO* pInfo);
LONG OnStop(UINT uAppNum, LONG lData);
The first one is a pointer to the object that you will be returning and that will implement a singleton. This way only one applet is displayed at a time. The rest are the interface that the class must expose to the OS. You'll need to create the code to handle the invocation of these methods (see below).
- In your class file (.cpp) add the following includes:
#include <windows.h>
#include <stdio.h>
#include "ControlPanelApplet.h"
- After that you can start with your class.
You'll need to take care of the variable declared on the header and also take care of the constructor.
- First you'll find the static variable used to always return the same reference.
CEditDNSControlPanelApplet* CEditDNSControlPanelApplet::m_pThis = NULL;
CEditDNSControlPanelApplet::CEditDNSControlPanelApplet() { m_pThis = this; }
- Then you'll need to take care of the exported method. This method is used by the OS to use the applet.
LONG APIENTRY CEditDNSControlPanelApplet::CPlApplet(
HWND hWnd,
UINT uMsg,
LONG lParam1,
LONG lParam2){
CEditDNSControlPanelApplet* pApplet = m_pThis;
switch (uMsg){
case CPL_DBLCLK:
return pApplet->OnDblclk(hWnd, lParam1, lParam2);
case CPL_EXIT:
return 0;
case CPL_GETCOUNT:
return 1;
case CPL_INIT:
return 1;
case CPL_INQUIRE:
return pApplet->OnInquire(lParam1, (CPLINFO*)lParam2);
case CPL_NEWINQUIRE:
return pApplet->OnNewInquire(lParam1, (NEWCPLINFO*)lParam2);
case CPL_STOP:
return 1;
case CPL_STARTWPARMS:
return pApplet->OnDblclk(hWnd, lParam1, lParam2);
default:
break;
}
return 1;
}
- As you can see, in this example we derived the processing of four of the requests to other methods. The rest of these requests are directly handled by the CPlApplet method itself. Pay attention to the resources (the icon and the strings) used in these methods.
LONG CEditDNSControlPanelApplet::OnInquire(UINT uAppNum, CPLINFO* pInfo) {
pInfo->idIcon = IDI_ICON1;
pInfo->lData = 0;
pInfo->idName = m_nNameID;
pInfo->idInfo = m_nDescID; return 0; }
LONG CEditDNSControlPanelApplet::OnNewInquire(UINT uAppNum, NEWCPLINFO* pInfo) {
pInfo->dwSize = (DWORD)sizeof(NEWCPLINFO);
pInfo->dwFlags = 0;
pInfo->dwHelpContext = 0;
pInfo->lData = 0;
pInfo->szHelpFile[ 0 ] = '\0';
LoadString( NULL, m_nNameID, pInfo->szName, 32 );
LoadString( NULL, m_nDescID, pInfo->szInfo, 64 );
return 1; }
LONG CEditDNSControlPanelApplet::OnDblclk(HWND hWnd, UINT uAppNum,LONG lData) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if(!CreateProcess(L"C:\\WINDOWS\\NOTEPAD.exe",
NULL, NULL, NULL,
FALSE,
0,
NULL, NULL,
&si,
&pi)) { return 1; }
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return 0;
}
This last method is where the magic really happens. In this case the applet is starting an instance of the Notepad. Here is where you should start your work.
Finishing the job...
From this point you can display any form you have included in the dll (CDialogue) or even start any other application you could have written in .NET, Java or whatever you want. It's up to you.
The result of building this solution will be a dll. Now you only need to rename the dll to change the extension to .cpl.
Now you have two ways to make it appear in your Control Panel folder
- put the .cpl file in your \Windows\System32\ directory.
- use the registry: go down all the path through HKLM\Software\Microsoft\Windows\CurrentVersion\Control Panel\Cpls\ and add a string value.
The name should be your applet name and the value should be the entire path to your .cpl file.
Make sure that the file has the same security entries that it would have on your Program Files folder so the system can access it properly.
History
Posted today (sept 11th, 2008). If any suggestions or bugs reported, I'll try to fix it...