Perfect programming guide objective Delphi

Source: Internet
Author: User
Valid Delphi

Clause 1: In any case, please have your Project have at least one user SysUtils. pas Unit

Many people who use Delphi complain that although Delphi is highly efficient in development, its compiled program is too large. Using Delphi5 to create a Project and compile it directly, the Size of the program has reached 286KB. If the same program is compiled under Delphi7, the Size of the Project has reached 360KB. It is precisely because of this that the application "Weight Loss" generated for Delphi compilation has become a reserved topic in almost all Delphi communities.

In the meantime, most people use Executable File compression tools (such as Aspack or Upx) to compress the executable files generated by Delphi to "lose weight, but there are also some people who use a more extreme, but more effective way to reduce the Size of executable files generated by Delphi compilation, that is, to discard the programming framework provided by VCL, you can directly use the WIN32 SDK and Object Pascal's object-oriented programming. For example, the following program uses the Delphi5 compiler to compile the program. The size is only 16 kb, the size of a basic Window program with a Window does not exceed 25 KB (the following program uses Delphi3 to compile will be smaller, for the following reasons, see the following ):

CODE:Program SmallPro;

Uses

Windows;

{$ R *. RES}

Begin

MessageBox (0, 'Hello World! ', 'Information', MB_ OK );

End.

[Copy to clipboard]

Please note that the above programs are directly compiled in the project file, so they are not referenced to other custom units. The contained windows unit is only required to call the MessageBox API function.

The compiled program is too small to be attractive to those who are familiar with Windows SDK programming and use Delphi as a development tool: -> ). I don't know if this method is also tempting to you or has already tempted you. If so, please listen to my advice first, "Please run sysutils on your project uese. PAS unit. Otherwise, your program will lose the ability to use the exception mechanism. If you do not take this advice, you will have to pay for your behavior sooner or later."

Different people have different opinions on Exception Handling. Some people think it is a wonderful way to handle errors: it separates the code that handles errors in the program code from the code that implements the logic, making the program source code more elegant and easier to write. Some people think that using the exception mechanism to handle errors in the program is not good: because once the exception is triggered and you do not control it, this exception will cause the application to terminate. This error is too straightforward and rude to handle. However, in any case, you can handle errors without using the exception mechanism in your program code, however, you cannot predict whether the various library functions or classes called in your code are used or support exception mechanisms. To ensure the robustness of your program, even if your code does not use the exception mechanism, you should add the exception handling code to the key position of your code, otherwise, the program may be terminated unexpectedly due to exceptions thrown by other codes or operating systems called by your code. The following is a simple example:

Code:Program SmallPro;

Uses

Windows,

Sysutils;

{$ R *. Res}

VaR

P: pchar;

Begin

Try

P: = nil;

P ^: = 'l ';

Except

MessageBox (0, 'exception', 'information', MB_ OK );

End;

End.

[Copy to clipboard]

The above program writes a byte of data to the address space 0x00000000. In all versions of Windows operating systems, this will be regarded as an illegal operation by the system, so the operating system will throw a SHE exception, the RTL system of Delphi enables your program to block this exception and handle it. If your program does not handle this exception, then, the RTL of Delphi will pop up a dialog box showing the exception information and terminate the entire program after you press the "OK" button in the dialog box. The above program handles this exception, and the program will continue to execute try... Blocks.

Next we will make a small change to the above example, remove the SysUtils. pas unit of uses, and then run it to see what results will appear.

The execution result of the program and the uses SysUtils. the version of the pas unit is quite different. The program only displays a dialog box, as shown in: Runtime Error, and then stops running. Our Exception Handling block try .. t does not play a role at all.

(Figure 1: runtime error)

 

After the above tests, I think you can understand that if you want your program compiled using the Delphi compiler to support the exception mechanism, then you must include SysUtils at least once in your project. pas unit. Writing this article here should be said to be successful, but I think it is necessary to give you a brief understanding of Delphi's entire exception handling mechanism so that you can access SysUtils. the position of the pas unit in the entire Delphi exception mechanism is further recognized and can be used better.

To trace the source, the Delphi compiler will insert a startup code for your program when linking to the C/C ++ compiler so that the operating system can call it, and start the entire application (this is not the main function of C/C ++, if you are interested in this aspect, I recommend that you read the "Programming Applications for Microsoft Windows Fourth Edition" by Jeffry Richter. Chapter 1 of this book describes Processes ).

For programs that exist in the form of EXE and that exist in the form of DLL, the names of startup codes inserted by Delphi are different. For EXE programs, delphi inserts a process named _ InitExe into your program. For DLL programs, the Delphi compiler inserts a process named _ InitLib into your program, you can use SysInit. find the definition and implementation of the two processes in the source code of the pas unit. By the way, System. pas and SysInit. the pas two Pascal units are the two default units included by the Delphi compiler during program compilation (you have never seen any program uses over these two units ). In Delphi, each version expands and modifies the two units, therefore, the Size of the small program compiled using Delphi7 is much larger than that compiled by Delphi5.

In the _ InitExe process, a process named _ StartExe is called internally, while in the _ StartExe process, it is called in the System. the SetExceptionHandler function defined in the pas unit initializes the Exception Handling Mechanism of the entire Delphi. The _ ExceptionHandler process set in this process is the core process of the entire Exception Handling Mechanism of Delphi.

In _ ExceptionHandler, System is used. A series of process pointer variables defined in the pas unit (such as javastproc, javastclsproc, and javastobjproc). These process pointer variables are required in the entire exception mechanism of Delphi, the initialization work of these variables is in SysUtils. in the InitExceptions unit defined in the pas unit, the InitExceptions variable is called in the initialization part of the SysUtils unit, so the entire Delphi Exception Handling Mechanism is initialized. For a DLL program, the initialization part of the exception handling process is the same as that of an EXE program, so I will not repeat it here.

After introducing the core exception handling process of Delphi, let's introduce how these exception handling processes are triggered.

When an exception is triggered, the operating system first intercepts the exception. It will be called immediately after the operating system intercepts this exception. kiUserExceptionDispatcher function (Note 1). This function is an exception handling function used by the Windows operating system. When the KiUserExceptionDispathcher function is called, it uses a callback mechanism, finally, call the _ ExceptionHandler process we mentioned above, expand the exception and handle it. If the exception matching the thrown exception is not found, it is called in SysUtils. the pointer variable of the ExceptHandler process assigned in pas throws an exception message box and terminates the program after the user presses the OK button.

Here, the nominal value is: if the value of the pointer variable in the javasthandler process is Nil, then the RTL of Delphi will call the System. the MapToRunError process defined in the pas unit maps the exception type to the runtime error code, and finally calls the RunErrorAt process to terminate the entire program in the displayed runtime error dialog box (1.

Note 1: my operating system is WIN2K, so KiUserExceptionDispatcher is in ntdll. Due to limited conditions, I have not performed similar debugging under WIN98, I don't know if similar methods are used to handle exceptions under WIN98. In addition, because the KiUserExceptionDispatcher function is not documented by Microsoft, therefore, it is not convenient for me to analyze the function operation mechanism here (because the implementation mechanism of this function may be different in different operating systems), so please forgive me.

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.