Generate a minimum executable with Delphi

zhaozj2021-02-11  213

Generate a minimum executable with Delphi

Once I saw someone said that Delphi can produce only 16K WIN32 applications, and this executable size of myself has written is around 17K, so I will prize Delphi, I can only optimize the code. This extent. Recently, since the purpose of the test was re-written, it was found to use some skills, and the size of the file was further reduced to 8.5k. This program can also display Delphi as another side of the non-RAD tool similar to Visual C . If you are interested in this, please see how I did this.

Generate a default item with Delphi, then use the Remove File from Project button on the toolbar to remove the unique form (Form1) from the project. Then select the View-> Project Source command, open the project file, and edit the code as follows:

PROGRAM miniapp;

Uses

Windows, Messages;

// {$ r * .res}

Const

Szappname: pchar = 'miniapp';

Function WndProc (awnd: hwnd; message: uint; wp: wparam; lp: lparam): LRESULT; stdcall;

Begin

Result: = 0;

Case Message of

WM_DESTROY:

PostquitMessage (0);

Else

Result: = DEFWINDOWPROC (AWND, Message, WP, LP);

END;

END;

VAR

WC: WNDCLASS;

Hmainwnd: hwnd;

Amsg: msg;

Begin

With WC do Begin

STYLE: = CS_VREDRAW OR CS_HREDRAW;

LPFNWNDPROC: = @wndproc;

CBCLSEXTRA: = 0;

CbWndextra: = 0;

Hicon: = Loadicon (0, IDi_Application);

Hcursor: = loadingcursor (0, IDC_ARROW);

HbRBackground: = getSyscolorbrush (color_window);

Hinstance: = hinstance;

Lpsz Gene: = NIL;

Lpszclassname: = szappname;

END;

RegisterClass (WC);

HmainWnd: = CREATEWINDOW (Szappname,

Szappname,

WS_OVERLAPPEDWINDOW,

Integer (CW_USEDEFAULT), INTEGER (CW_USEDEFAULT),

Integer (CW_USEDEFAULT), INTEGER (CW_USEDEFAULT),

HWND_DESKTOP, 0,

Hinstance, NIL);

ShowWindow (HmainWnd, cmdshow);

UpdateWindow (HmainWnd);

While GetMessage (Amsg, 0, 0, 0) DO Begin

TranslateMessage (AMSG);

DispatchMessage (AMSG);

END;

End.

In fact, these codes are all turned on the C language example program in Win32 SDK. I don't want to explain them again. Need to remind you to pay attention to:

1. The Delphi program does not have a Winmain entry like a C program, and the program starts from the.dpr file Begin starts, and the end of the END matched. The four parameters of WinMain are passed in the C program. In the Delphi, they are defined in the System and Sysinit units in the form of global variables, which are Hinstance, Hprevinst, cmdline, and cmdshow (HPREVINST have no meaning). 2. Note that I define constant szappname as PCHAR instead of a common String, because there is no need to use String's advanced features in such a simple program, which saves a lot of space (approximately 3-4K).

3. I also commented in {$ r * .res}, which can remove redundant resources from the file to save about 1K.

4. After the program is complete, open the Project Options dialog box, turn it out to the Compiler page, clear all the options in the Runtime ErrorS and Debugging categories, which can also reduce the size of the final file slightly.

Although I can't say that 8.5k must be the smallest size, but I guess this is very close to the limit of Delphi. In Visual C , it is very interesting that all executable files will be aligned to 4K boundaries, so the smallest in Visual C can only reach 28K, unless you use some very special Hide compilation switch. Although the pagination in Win32 is 4K, but I really want to have the size of the executable of Visual C to 4K.

In addition, I still have a very very old Turbo Pascal for Windows 1.5, the smallest Windows executable of the smallest Windows executable - you can't believe - only 1.75K! Of course, it is just a Win16 program after all.

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

New Post(0)