In-depth programming: How much is the control panel know?

xiaoxiao2021-03-06  103

I am divided into 3 parts:

1. What is the control panel, where is it?

2, true identity of the CPL file

3, how VCL provides support for "Control Panel"

What is the control panel, where is it?

What is the control panel? Where is it? In the past, I was also very confused, just occasionally heard the "Control Panel" description from some books about "Windows System Management": "Every item of the control panel generally corresponds to one .cpl file, these files exist Under the system directory, you can specify the items you want to display in the control panel, you can hide, etc. "About how to implement, may also teach you some to achieve the purpose by modifying the registry, yes, this can achieve the purpose, but I Think of this is just from a manager's point of view, if you are from the programmer's perspective, can their description solve your problem? How do you use a program to reach the purpose of controlling your "Control Panel", you want to know, please listen to the decomposition.

True identity of the CPL file

The above mentioned ".CPL" extension file ", since it is related to the control panel, I will analyze what the CPL file is, just find some CPL files, such as: main.cpl, access.cpl, etc. , I use the Dumpbin test results as follows:

C: / windows / system32> dumpbin main.cpl

Microsoft (R) Coff Binary File Dumper Version 6.00.8168

Copyright (c) Microsoft Corp 1992-1998. All Rights Reserved.

Dump of file main.cpl

FILE TYPE: DLL (you can know it is a DLL file)

C: / windows / system32> dumpbin / exports appwiz .cpl

Microsoft (R) Coff Binary File Dumper Version 6.00.8168

Copyright (c) Microsoft Corp 1992-1998. All Rights Reserved.

DUMP OF File AppWiz.cpl

FILE TYPE: DLL

Section Contains The Following Exports for AppWiz.dll

Ordinal Hint Rva Name

1 0 00017926 CPLAPPLET

2 1 00017F05 ConfigStartMenu

. . . . . .

C: / Windows / System32> Dumpbin / Exports Access.cpl

Microsoft (R) Coff Binary File Dumper Version 6.00.8168

Copyright (c) Microsoft Corp 1992-1998. All Rights Reserved.

DUMP of File Access.cpl

FILE TYPE: DLL

Section Contains the Following Exports for Access.dll

Ordinal Hint Rva Name

1 0 00004B41 CPLAPPLET

2 1 00004B33 Debugmain

3 2 00004B30 DLLREGISTERSERVER

3 3 00004A27 DLLUNREGISTERSERVER

From the above test results, what do you see, I think there are at least two:

1, the CPL file is a DLL file

2, the CPL file exports a CPLAPPLET function

These two points uncover the mystery of the control panel program, you will not be confused about the control panel program, in fact, the control panel program is a DLL file that must export the CPLApplet function, just hangs the suffix name of the CPL Yes. Since all the CPLApplet functions can be exported, you can imagine the importance of the function of the CPLApplet, in fact, not only the control panel, as long as all all applications that want to load the CPL file must obtain the address of the CPLApplet function and then call the function to complete the corresponding Function, the following is the statement I have from MSDN, please refer to MSDN.long AP for details.

IENTRY CPLAPPLET

HWND HWNDCPL,

UINT UMSG,

Long Lparam1,

Long lparam2

);

Meaning of parameters

HWNDCPL activates the window handle of the Control Panel Component Application

UMSG external incoming control message, the cpLapplet function is to complete the corresponding task through this message.

LPARAM1 Message Parameters 1

LPARAM2 Message Parameters 2

CPLAPPLET function acceptable control message list:

Message description

The first message received by the CPL_INIT CPL program, which usually completes the initialization of the control panel component data and variables here.

The CPL_GETCOUNT CPLAPPLET function received the second message after the CPL_INIT message, which causes the CPLAPPLET function to return the number of control panel components included in the CPL file.

CPL_INQUIRE CPL_INQUIRE and the next CPL_Newinquire message to be described is the most important message in all control panel messages, which can say that the control panel is to use these two messages to get the name, description and description of each component.

Icon and other information. CPL_INQUIRE fills the component information into the structure of TCPLINFO, the declaration of the TCPLINFO structure is as follows:

Typedef struct tagcplinfo {

Int idicon;

Int idName;

Int iDinfo;

Long LDATA;

} CPLinfo;

Typedef tagcplinfo tcplinfo;

Then your program can use LoadString, loadicon (this API function has been replaced by the loadImage function, but you can still use it) and other API functions to achieve the corresponding information.

CPL_Newinquire

CPL_NEWINQUIRE is similar to the function completed by the CPL_INQUIRE message, but it will fill the component information in the TNewCPLINFO structure instead of the TCPLINFO structure. When the name knows the name of CPL_Newinquire, it will be prioritized by conventional thinking, Using CPL_Newinquire, but this is an exception. Although the TNewCPLinfo structure is more complete than TCPLINFO, it is unable to cache, so use TNewCPLinfo will slow down the speed of the turning control panel, which is also a Microsoft file to indicate "unless If necessary, otherwise try to pass the CPL_INQUIRE message to pass the component information.

CPL_DBLCLK When the user double-click the icon of the component in the control panel, the CPL_DBLCLK message triggers the user's operation, which is usually turned on to open a dialog to provide the user to adjust settings, such as "Internet Options" will display a related IE setting Dialog, you can set some properties of IE in this dialog

CPL_STOP This message is mainly providing opportunities to give you a good job, such as released and component-related memory.

CPL_EXIT This message is the last chance of your work, that is, you can make some good work before the application calls the freelyibray function, such as: release memory, etc.

Tip: The message sequence listed above is also the order of the messages received by the CPL program, that is, the CPL program is based on CPL_INIT, CPL_GETCOUNT to CPL_EXIT to complete the corresponding task. If you still feel abstract on the above description, Below I have an actual example to explain how the application is established by the Control Panel.

The steps are as follows:

1: Establish a resource file to use in the program, the following figure is the case where Ctrl.rc is made with Borland Resound Workshop.

Figure 1-1 Editing a resource file

Tip: 1, 2 indicates the string and

The code of the icon resource, you can also go to other names, you can use Brcc32.exe to compile it into the RES resource file command as follows:

Brc32 ctrl.rc

This will be compiled by Ctrl.res, of course, you can also use Borland Resound Workshop to establish a file in RES format, save this link.

2: Open your BCB, build a DLL project through DLL Wizard (because the CPL program is a DLL file)

3: Export CPLApplet functions, this is the most important, other operations and other programs have no difference

4: Select the "Project / Options" option from the menu, change the "Target file extension" of the Application page to "CPL" in the "Project Options" option dialog box

prompt:

Since the program is not an executable file, you cannot press F9, you must run through the control panel or Rundll32.exe, first put it under the CPL file copy to the system directory, then use the control panel to run or run, command. As follows: rundll32 shell32.dll control_rundll * .cpl

The source program is as follows:

#include

#include

#pragma HDRSTOP

File: // Import the header file related to the control panel

#include

#pragma resource "ctrl.res"

FILE: / / Export CPLApplet function

Extern "C" __declspec (dllexport) long _stdcall CPLApplet

HWND HWNDCPL,

UINT UMSG,

Long Lparam1,

Long lparam2

);

File: / / Implement the CPLAPPLET function

Long _stdcall CPLApplet (HWND HWNDCPL, UINT UMSG, Long LParam1, Long LPARAM2)

{

LPCPLINFO PTCPLINFO;

Switch (UMSG)

{

Case CPL_INIT:

ShowMessage ("Initialization Data or Variables!");

Return 1;

Case CPL_Getcount:

ShowMessage ("There is only one component!");

Return 1;

Case CPL_INQUIRE:

ShowMessage ("Sets Control Panel Component Resource Information!");

PTCPLINFO = (LPCPLINFO) LPARAM2;

File: // Fill information in the structure of TCPLINFO

PTCPLINFO-> IDNAME = 1;

PTCPLINFO-> iDINFO = 1;

PTCPLINFO-> iDicon = 2;

PTCPLINFO-> LDATA = 0;

Break;

Case CPL_DBLCLK:

SHOWMESSAGE ("I am very happy to see you!");

Return 0;

Case CPL_EXIT: SHOWMESSAGE ("Exit Control Panel!");

Return 0;

}

Return 0;

}

#pragma argsused

Int WinAPI DLLENTRYPOINT (Hinstance Hinst, Unsigned Long Reason, Void * LPRESERVED) {

Return 1;

}

The DLLEntryPoint function is the entry function of all DLL files, which is equivalent to the Winmain function of the executable file. ...) Export CPLApplet functions

Tell how to write the original control panel program, below I will introduce how the VCL supports writing control panel programs. It mainly introduces a CTLPANEL unit and the TappletApplication and TappletModule class. TappletApplication represents a CPL file, which represents a control panel component, with the help of these two classes, you can easily complete the control panel program encoding. Strange, how do you know, in fact, just click "Project / V

IEW Soure Opens the project source code, you can find that the statement "#include" indicates that it introduces the CTLPanel unit, continues to track the CTLPAnel unit (ie open the CTLPanel.hpp file), and you will find the definition of the TappletApplication and TappletModule, and their definitions are as follows:

Class PascalImplementation TappletApplication: PUBLIC CLASSES :: Tcomponent

{

Typedef classes :: tComponent inherited

Private:

UNSIGNED FCONTROLPANELHANDLE;

Classes :: tlist * fmodules;

. . . . . .

}

Class PascalImplementation TappletModule: PUBLIC CLASSES :: TDATAMODULE

{

Typedef classes :: tdataModule inherited

Private:

TactivateEvent fonactivate;

TSTopevent Fonstop;

. . . . . .

}

This step is not lost as a good way to learn.

Program example of a control panel under VCL support: Show your machine name

The steps are as follows:

1. Select the "file / new" option of C Builder to enable the "New Items" dialog (as shown in the figure below) Select "Control Panel Application" to establish a control panel application, the CPL file.

2, the property appleticon sets

Setting titles in Caption, such as: "Show your machine name"

1. Fill in the following code to get the machine name at the ACTIVATE event.

Void __fastcall tappletModule1 :: AppleTModuleActivate (TOBJECT * SENDER,

INT DATA

{

LPTSTR LPBUFFER = New Char [256]; LPDWORD NSIZE = New Unsigned Long (256);

GetComputername (lpbuffer, nsize);

ShowMessage ("Your Computer Name:" Ansistring (LPBuffer));

DELETE LPBUFFER;

DELETE NSIZE;

}

Figure 1-2 "New Items" dialog

Tip: Select the right button in "Applet Module", you will have a shortcut menu, you can simply complete the "installation", "delete", without having to complete the compilation of the control panel program through the "Batch File" as the above example. , Test and other functions

Tip: If you want you to know the function of this program, you don't have to achieve your goals through the keyboard input.

转载请注明原文地址:https://www.9cbs.com/read-97457.html

New Post(0)