用Delphi产生一个最小的可执行程序
曾经在网上看到有人说Delphi能够产生大小只有16k的Win32应用程序,而我自己曾经编写过的这种可执行文件大小则是在17k左右,因而我一度猜想Delphi恐怕也只能将代码优化到这种程度了。最近由于测试的目的重新把这个程序写了一遍,才发现利用一些技巧,还能够将文件的大小进一步缩减到8.5k。这个程序也能够显示Delphi作为类似于Visual C++的、非RAD工具的另一个侧面。如果你对此感兴趣的话,请看我是如何做到这一点的。
XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />
用Delphi生成一个默认的项目,然后用工具栏上的Remove file from Project按钮,将唯一的窗体(Form1)从项目中删除。然后选择View->Project Source命令,打开项目文件,并编辑代码如下所示:
DelphiCode:
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 := LoadCursor(0, IDC_ARROW); hbrBackground := GetSysColorBrush(COLOR_WINDOW); hInstance := HInstance; lpszMenuName := 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. |
其实这些代码根本就是Win32 SDK中C语言例子程序的翻版,我想没有必要再对它们作什么解释了。需要提醒你注意的是:
1.Delphi程序没有像C程序那样的WinMain入口,程序的运行就从.DPR文件的begin开始,到与之匹配的end结束。而C程序中传递给WinMain的四个参数,在Delphi中则以全局变量的形式定义在System以及SysInit单元中,它们分别是HInstance、HPrevInst、CmdLine和CmdShow(HPrevInst已经没有意义)。
2.注意我把常量szAppName定义为PChar,而不是常用的String,因为在这么一个简单的程序中没有必要使用String的高级功能,这样可以节省不少空间(大约3-4k)。
3.我把{$R *.res}一句也注释起来了,这样可以从文件中剔除冗余的资源,从而节省大约1k左右的空间。
4.程序完成后,打开Project Options对话框,翻到Compiler页,将Runtime Errors和Debugging分类中的选项全部清除,这样也可以稍微减小最终文件的大小。
尽管还不能说8.5k一定就是最小的尺寸,不过我猜想这已经非常接近Delphi的极限了。在Visual C++中,很有意思的一点是所有可执行文件的大小都会对齐到4k的边界,所以在Visual C++中最小的.EXE和最小的.DLL大小都只能达到28k,除非使用一些非常特殊的隐藏编译开关。尽管Win32中的分页默认是4k,不过我实在想不通Visual C++把可执行文件的大小也对齐到4k究竟有什么意义?
另外,我手头还有一套非常非常古老的Turbo Pascal for Windows 1.5,它所编译出的最小的Windows可执行文件的大小——你肯定无法相信——只有1.75k!当然了,它毕竟只是个Win16程序而已。
DelphiCode:
program demo; uses windows, Messages, ShellApi; {$R *.res} const id_Button=100; function PlainWinProc(hWnd:THandle; nMsg:UINT; wParam,lParam:Cardinal):Cardinal;export;stdcall; var hdc:THandle; ps:TPaintStruct; Rect:TRect; begin result:=0; case nMsg of wm_Create: CreateWindowEx(0,'Button','&Click Me',ws_Child or ws_Visible or ws_Border or bs_PushButton, 0,0,200,80,hWnd,id_Button,hInstance,nil); wm_Size: begin GetClientRect(hWnd,Rect); SetWindowPos(GetDlgItem(hWnd,id_Button),0,Rect.Right div 2-100, Rect.Bottom div 2-40,0,0,swp_NoZOrder or swp_NoSize); end; wm_Command: if LoWord(wParam)=id_Button then if HiWord(wParam)=bn_Clicked then MessageBox(hWnd,'Button Clicked','Plain API',MB_OK); wm_lButtonDown: MessageBox(hWnd,'Left Mouse Button Clicked','Plain API',MB_OK); wm_Paint: begin hdc:=BeginPaint(hWnd,ps); Ellipse(hdc,100,100,300,300); EndPaint(hWnd,ps); end; wm_DropFiles: begin MessageBox(hWnd,'Drop File','Plain API',MB_OK); DragFinish(wParam); end; wm_Destroy: PostQuitMessage(0); else result:=DefWindowProc(hWnd,nMsg,wParam,lParam); end; end; procedure WinMain; var hWnd:THandle; Msg:TMsg; WndClassEx:TWndClassEx; begin WndClassEx.cbSize:=SizeOf(TWndClassEx); WndClassEx.lpszClassName:='PlainWindow'; WndClassEx.style:=cs_VRedraw or cs_HRedraw; WndClassEx.hInstance:=Hinstance; WndClassEx.lpfnWndProc:=@PlainWinProc; WndClassEx.cbClsExtra:=0; WndClassEx.cbWndExtra:=0; WndClassEx.hIcon:=LoadIcon(Hinstance,Pchar('MAINICON')); WndClassEx.hIconSm:=LoadIcon(Hinstance,Pchar('MAINICON')); WndClassEx.hCursor:=LoadCursor(0,idc_Arrow); WndClassEx.hbrBackground:=GetStockObject(gray_Brush); WndClassEx.lpszMenuName:=nil; if RegisterClassEx(WndClassEx)=0 then begin MessageBox(0,'Invaild Class registration','Plain API',MB_OK); exit; end else begin hWnd:=CreateWindowEx(WS_EX_TOPMOST or WS_EX_ACCEPTFILES, WndClassEx.lpszClassName, 'Plain API Demo',ws_OverlappedWindow, cw_UseDefault,0,cw_UseDefault,0,0,0, Hinstance,nil); if hWnd=0 then MessageBox(0,'Window not Created','Plain API',MB_OK) else begin ShowWindow(hwnd,SW_ShowNormal); while GetMessage(Msg,0,0,0) do begin TranslateMessage(Msg); DispatchMessage(Msg); end; end; end; end; begin WinMain; end. |