1. Tech

Your suggestion is on its way!

An email with a link to:

http://delphi.about.com/library/weekly/aa062403b.htm

was emailed to:

Thanks for sharing About.com with others!

RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Books|Link Back

Locate, Display and Execute Control Panel Applets
Page 2: Inside Control Panel Applets: Extracting a Name, Description and the Icon
Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
More of this Feature
Page 1: CPL: the theory
Page 3: Executing Applets
Page 4: Full Project CODE
Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
Related Resources
Finding files with Delphi
Using Windows API
Message Handling

Interested in Delphi code to mimic the Windows Control Panel folder behavior? In this article you can learn how to find CPL files, how to extract description, name and even the applet icon. Even more, learn how to execute applets from your applications.

Once we have found all Control Panel applications inside the Windows System folder, we can "look" inside, and extract any information we need.

But, first some info on how CPL files are "structured".

CPL: special-purpose DLL
As stated, CPL files are special-purpose dynamic link libraries. To be able to get information from a Control Panel application, we need to load the CPL file using the LoadLibrary API call.

Every Control Panel application exports the standard entry-point function, named CPlApplet. This function receives requests, in the form of messages (like "CPL_SOMETHING"), and carries out the requested work, such as initializing the application, returning a number of dialog boxes it supports, providing information about a specified dialog box, and closing the application.

Therefore, after we have dynamically loaded the CPL, in order to look inside the CPL application we need to "find" the CPlApplet function. This is done by calling the GetProcAddress API call.

Once we lay our hands on the CPlApplet function, we use it to grab the information about the Contol Panel Application we want. First, we send it the CPL_GetCount message to get the number of dialog boxes (Applets) it carries. Remember that each .CPL file is an application that can hold more than one Applet. Second, for each Applet, we send the CPL_INQUIRE message that fills the CPLInfo structure. This structure contains resource information (such as the name of the applet, description and icon) and an application-defined value for a dialog box supported by a Control Panel application. Finally, we use LoadIcon and LoadString API calls to get the information we are interested in.

When we have finished with the Control Panel application (the CPL file), we unload it using the FreeLibrary function.

Uh, the "story" above looks quite messy ;) Don't runaway, Delphi code looks much more friendly.

Control Panel Folder - Delphi Way
At the end of the article you'll have the option to download the full project source.
For now, I suggest you to start Delphi, and on that empty new form drop two TButton components, one TListView and one TImageList.
The TListView should be named 'lvCPL', you should add 3 columns (Name, Comment, FileName) to it. Name the TImageList 'cplIcons' - we'll use for SmallImages property of the ImageList. The cplIcons will be run-time filled with icons extracted from the CPLInfo structure (as explained above). The two buttons are used to locate the CPL files and execute the selected one. More about executing, later in this article...
Delphi Control Panel folder

The ProcessCPLApplet function
Here's the "main" function in the project, the one that receives one Control Panel application (parameter sCPLName: CPL file name), processes each Applet inside the application, and places the information inside the ListView component.

procedure TMainForm.ProcessCPLApplet(sCPLName : string);
var
   H: HInst;
   CPlApplet: TCPLApplet;
   NumberOfApplets: Integer;
   AppletInfo: TCPLInfo;
   I: Integer;
   Name, Comment: string;
   TheIcon : TIcon;
begin
   // Load CPL
   H := LoadLibrary(PChar(sCPLName));
   if H <> 0 then
   try
     // Locate CPlApplet Function from Module
     CPlApplet:= GetProcAddress(H, 'CPlApplet');
     if Assigned(CPlApplet) then
     begin
       // Get Number of Applets contained in the .cpl file
       NumberOfApplets:= CPlApplet(Application.Handle, CPL_GetCount, 0, 0);
       for i := 0 to -1 + NumberOfApplets do
       begin
         // Pick Name and Comment
         CPlApplet(Application.Handle, CPL_INQUIRE, i, Longint(@AppletInfo));
         Name:= LoadStringFromModule(H, AppletInfo.idName);
         Comment:= LoadStringFromModule(H, AppletInfo.idInfo);
         // Display info
         if Name <> '' then
         begin
           with lvCPL.Items.Add do
           begin
            TheIcon:=TIcon.Create;
            try
              //grab the Applet icon
              TheIcon.Handle:=LoadIcon(H, PChar(AppletInfo.idIcon));
              ImageIndex := cplIcons.AddIcon(TheIcon);
            finally
              TheIcon.Free;
            end;

            //add to ListView
            Caption := Name;
            SubItems.Add(Comment);
            SubItems.Add(sCPLName);
           end;
         end;
       end;
     end;
   finally
     FreeLibrary(H);
   end;
end;

Note: the TCPLApplet and the TCPLInfo types are defined in the Delphi CPL.pas unit, which you will have to include in the uses clause. The LoadStringFromModule is a function defined in the project used to extract the string value (stored as a resource) inside the CPLInfo structure.

All set, we have a Delphi clone of the Windows Control Panel folder, now we proceed to executing a particular applet!

Next page > Executing Control Panel Applets from Delphi > Page 1, 2, 3, 4

All graphics (if any) in this feature created by Zarko Gajic.

More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2003|2002|2001|2000|1999|1998 or by TOPIC.
Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

Got some code to share? Got a question? Need some help?

©2014 About.com. All rights reserved.