ZZ Windows Control Panel Programming

Source: Internet
Author: User

ZZ from: http://blog.csdn.net/tomcui/archive/2008/01/22/2058502.aspx

 

Windows Control Panel Programming

Note: This article is written for my reference to relevant articles when I am working on a project. If there is any improper situation, please point it out.

Mail: tomcui60000520@163.com

Keywords:

Control Panel, control panel application,

 

1. What is a control panel?

Open the windows control panel and you will see a similar image.

 

Figure 1

Double-click an icon to display a dialog box that allows you to complete the corresponding software and hardware settings. This is the control panel we see. So how to develop control panel programs? Search for the keyword "Control Panel" in msdn and Google with questions, and you will find related technical articles. This is my method of work: use existing resources for reference. But is it true? We can continue step by step with msdn.

After mining, it is found that it is not an EXE file (the control panel application that supports EXE in Windows Vista, and Microsoft recommends making it an EXE file), but a file with a CPL suffix, you can find such a file in Windows> system32. If you use tools such as dependency Walker for Win32 (x86) or dumpbin, you can see that some functions are exported to the file.

 

 

Figure 2

Observe several such files and find that the exported functions are different, but all of them have cplapplet functions exported. These features are consistent with the DLL features. Go to msdn and check the cplapplet function instructions to prove that our guess is correct. It can be said that the control panel should be a DLL file with the CPL extension and must be exported to the cplapplet function.

For specific descriptions, refer:

The http://msdn2.microsoft.com/en-us/library/bb776838 (vs.85). aspx

Ii. clarify several concepts

L control panel management program: The program used to manage the Control Panel. In windows on the desktop, It is a control program. EXE, Which is ctlpnl in Windows CE. EXE, which is used to manage control panel entries in the control panel. Simply put, when we open the control panel, these management programs are running. But what we see is the shell appearance (Note: This is my guess and there is no basis yet ).

L control panel item: each icon displayed in the control panel corresponds to a control panel item.

L control panel application: the CPL file that is finally seen. A control panel application can implement several control panel entries.

 

3. Control Panel Application Programming

Compiling a control panel application is to compile a DLL file to implement the functions required for control. This involves a function that has to be mentioned. Without it, the control panel program cannot be implemented. This function is a cplapplet. The following describes the parameters of the function.

Function: Long cplapplet (hwnd hwndcpl, uint MSG, lparam lparam1, lparam lparam2)

Function cplapplet is the control panel application (Control Panel applicationinterface entry point, which is automatically called by the Control Panel Management Program (control.exe or ctlpnl.exe). It is a callback function (callback). Note: The CPL file must export the function cplapplet, in this way, the control panel can find the entry point of the program.

When the control panel is started, it searches for files in the corresponding entry directory of windows, system32, or registry, and Loads files with CPL extension, it calls the export function cplapplet () of the CPL file and sends a message to the function. Therefore, the control panel application processes the messages sent from the control panel, that is, the messages are processed in the function cplapplet. This function has no default behavior. If multiple Control Panel programs are implemented in a CPL file, there will be only one cplapplet function, which is responsible for all control panel applications.

Parameter description:

Hwndcpl: the control panel manager is the window handle of control.exe. If the control panel application or other windows need to pass the parent window handle, you can use this parameter.

MSG: the message sent to the control panel application, which is sent by the Control Panel management program.

Lparam1: Message Parameters

Lparam2: Message Parameters

The return value of a function varies depending on the message.

To use this function, the application must contain the header file Cpl. h.

 

 

Message name
Description
 
Cpl_init
The first message received by the Control Panel application, usually processing global initialization and memory allocation. Otherwise, 0 is returned. The control panel administrator terminates communication with the application and releases the corresponding CPL file.
 
Cpl_getcount
The message is sent immediately after the cpl_init message. It returns the number of control panel components contained in the CPL file that the control panel administrator can view, that is, the number of icons that the CPL file can appear in the control panel.
 
Cpl_inquire
Sent after cpl_getcount to provide information for the specified control panel entry.
 
Cpl_newinquire
The message is sent after cpl_getcount, which is similar to the function completed by the message cpl_inquire, except that the tnewcplinfo structure pointer is required, and the resources contained do not provide cache. Therefore, the control panel starts slowly, it is generally not recommended to process the message unless necessary, for example, to dynamically change the icon and string of the control panel based on certain conditions.
 
Cpl_dblclk
Indicates that you have selected a control panel entry, and the program should display the corresponding dialog box so that you can complete the corresponding task. 0 is returned. Otherwise, non-0 is returned.
 
Cpl_stop
The control panel management program is sent when it is closed, and the Control Panel application processes memory release and other actions at this time. Processed successfully. 0 is returned.
 
Cpl_select
Not used currently. Only Windows 95 and Microsoft Windows NT 4.0 are supported.
 
Cpl_startwparms
This message is similar to cpl_dblclk, but lparam2 points to the lpctstr message. This message is valid in shell32.dll version 5.0 (Windows 2000, Windows Millennium Edition (Windows ME) and later versions.
 
Cpl_exit
The message is sent after the cpl_stop message, which is the last chance for the Control Panel application to release resources. 0 is returned for successful processing.
 

 

Cpl_inquire: lparam1 is an integer starting from 0. It is the index of the control panel in the CPL file. The lparam2 parameter requires a pointer to the cplinfo structure, fill in the required icons, strings, and other information. If the message is successfully processed, 0 is returned.

Cpl_newinquire: both the message and cpl_inquire are messages sent after cpl_getcount, but there is no clear sequence. Therefore, the program should not rely on their order to process different transactions.

To write a control panel application, follow these steps:

1. select an appropriate development tool (for example, Visual Studio 2008) to create a DLL project;

2. Export the function cplapplet;

3. Complete the required work in the message processing process of the function cplapplet;

A simple example

Development tools: Microsoft Visual Studio 2008

Operating System: Windows XP SP2

Steps:

1. Create a Win32 project named cpltest;

 

2. Select DLL for the application type (the CPL file is essentially DLL );

 

 

3. Add or import an icon file and two string Resources in the project to display icons and prompts in the control panel management program;

Right-click resource files and choose add> resource. Then, select icon or string table.

Some content of resource. H is as follows:

# Define idi_icon1 101 // icon ID

# Define ids_string102 102 // string Tom

# Define ids_string103 103 // string Cui

4. Add the function export cplapplet to the dllmain. cpp file;

Extern "C" _ declspec (dllexport) Long apientry cplapplet (hwnd hwndcpl, uint umsg, lparam lparam1, lparam lparam2 );

In principle, you can export it in the above way. However, note that the cplapplet is called by apientry. The function exported in this way will be renamed and cannot be exported through multiple experiments. You may remove the apientry, but the compiled CPL file cannot run. You have read the relevant documentation and specified the function call method in the document of the Windows Mobile version 5.0 SDK, windows CE 5.0 and Windows Shell and controls do not specify this call method. Therefore, only apientry is added.

Now the question is how to export this function? It seems that the def file is going to be passed. If your project does not produce the def file, you can use project-> properties-> linker-> module definition file to specify or use NotePad to create such a file. Enter the following content.

; Cpltest. Def: Declares the module parameters for the DLL.

 

Library "cpltest"

 

Exports

; Explicit exports can go here

Cplapplet

5. Add the message processing function of the cplapplet function to the dllmain. cpp file to complete the specified function;

The preceding two header files are included in dllmain. cpp.

# Include "resource. H" // resource ID

# Include <Cpl. h> // header file required by the cplapplet Function

In my example, the function of displaying a MessageBox is completed.

Complete dllmain. CPP Code:

// Dllmain. cpp: defines the entry point for the DLL application.

# Include "stdafx. H"

# Include "resource. H"

# Include <Cpl. h>

 

Long apientry cplapplet (hwnd hwndcpl, uint umsg, lparam lparam1, lparam lparam2 );

 

Bool apientry dllmain (hmodule,

DWORD ul_reason_for_call,

Lpvoid lpreserved

)

{

Switch (ul_reason_for_call)

{

Case dll_process_attach:

Case dll_thread_attach:

Case dll_thread_detach:

Case dll_process_detach:

Break;

}

Return true;

}

 

Long apientry cplapplet (hwnd hwndcpl, uint umsg, lparam lparam1, lparam lparam2)

{

Int I;

Lpcplinfo;

 

I = (INT) lparam1;

 

Switch (umsg ){

Case cpl_init: // first message, sent once

Return true;

 

Case cpl_getcount: // second message, sent once

Return 1;

Break;

 

Case cpl_inquire: // third message, sent once per application

Lpcplinfo = (lpcplinfo) lparam2;

Lpcplinfo-> ldata = 0;

Lpcplinfo-> idicon = idi_icon1;

Lpcplinfo-> idname = ids_string102;

Lpcplinfo-> idinfo = ids_string103;

Break;

 

Case cpl_dblclk: // application icon double-clicked

MessageBox (null, text ("tom66"), text ("cuei666"), mb_ OK );

Break;

 

Case cpl_stop: // sent once per application before cpl_exit

Break;

 

Case cpl_exit: // sent once before freelibrary is called

Break;

 

Default:

Break;

 

}

Return 0;

}

 

6. Compile

Project-> properties-> linker-> output file: Modify the suffix of the output file to CPL, or change the DLL to Cpl.

 

Iv. Installation and running of Control Panel applications

L copy the CPL file to Windows (Windows CE) or Windows/system32 (Windows for desktop). You can double-click here to run it, you can also open the Control Panel to view the control panel entries contained in the CPL file. The icons and files are specified in the cplapplet. You can double-click them to run them.

2. Run rundll32 shell32.dll and control_rundll cpltest. Cpl (CPL file name) @ 1 on the command line. (The number specifies the number of control panel entries to run. a cpl file can contain several control panel entries ). In Windows, enter ctlpnl.exe/Windows/cplmain. CPL, 5 in the command line, which is different from the desktop version.

3. Create a new string in the Windows registry [HKEY_LOCAL_MACHINE/software/Microsoft/Windows/CurrentVersion/control panel/cpls] and specify the complete path of CPL, then you can see the newly added control panel entries in the control panel. The method of writing the registry is commonly used by some application software. During installation, you can add it to the Registry through installation tools such as InstallShield. When uninstalling the registry, delete related items in the registry.

 

4. You can directly Delete the corresponding CPL file by copying it. I have not found any better way.

 

V. References:

1 http://msdn2.microsoft.com/en-us/library/aa926276.aspx

2 http://msdn2.microsoft.com/en-us/library/bb776392.aspx

Add by myself
To install the control panel application by modifying the registry, you must respond to the cpl_inquire message in the cplapplet and load the icon in it. (Lpcplinfo-> idicon). Otherwise, the icons of the installed application cannot be loaded or displayed when the control panel is opened.
By copying the file to the System32 directory, you can respond to cpl_inquire or cpl_newinquire and load the icon in any of the response functions.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/jtujtujtu/archive/2008/12/12/3503878.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.