Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Make small .exe with little memory usage

Posted on 1999-06-30
16
Medium Priority
?
559 Views
Last Modified: 2008-03-17
If i compile a very little program like:

uses Windows;
begin
  Messagebeep(0);
end.

and then I study the exe file with ResourcenWorkshop or ShowDeep (which shows a list of the used WinAPI function of the .exe) then I see: Delphi has linked a lot of unused stuff to the .exe.
I know every Delphi program loads the Ole32.dll into the memory. That means: the little messagebeep() program will needs more than 1MB memory!

How to make small .exe (without unused resource like MAINICON, string table, Bitmaps..) which needs only little memory (without loading unused DLL's)? Should I start to learn VisualC :-( ?
0
Comment
Question by:bengore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +3
16 Comments
 
LVL 3

Expert Comment

by:philipleighs
ID: 1385526
Have you tried only declaring the functions you need?

Instead of
uses Windows;

Try
function MessageBeep(uType: Integer): Boolean; stdcall; external 'user32.dll';

Cheers,
Phil.

0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1385527
Sorry Ben, there is nothing you can do about it. When you use Delphi's components the VCL (Visual Component Library) will be linked to your app. This is what you see in ResourceWorkshop.

Epsylon.
0
 
LVL 3

Expert Comment

by:philipleighs
ID: 1385528
Epsilon,

When you compile

program SmallExe;
uses Windows;
begin
  Messagebeep(0);
end.

It only includes 72 bytes of resources! The exe is 16kb.

You only get the VCL if you add Forms or Graphics or something to the uses clause.

Cheers,
Phil.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:Madshi
ID: 1385529
Yep, Phil is right. I've written some Delphi programs with only 16KB, too...   :-)
0
 
LVL 7

Accepted Solution

by:
ahalya earned 300 total points
ID: 1385530
Nope.  you don't have to learn VC. You can (almost) do anything with Delphi.
Here is a sample code that uses Dialogs and a window to receive messages (but not the Forms unit) and yet compiles to a small EXE.

I use this program to disable a Windows shutdown.


program WinMon;

uses Windows, Messages;

var Terminated : boolean;
    WHandle    : HWND;
    M          : TMsg;
    Mode       : ShortInt;

function NoTOClose:Boolean;

var  id : byte;

begin;
if Mode = -1 then id := GetProfileInt('Desktop', 'Busy', 0)
             else id := Mode;


case id of
    0 : Result := true; //say No to closing, without any dialogs.
    1 : begin;
        Result := true;
        MessageBox(0, 'Windows cannot be closed at this time',
                      'Shut down Windows',MB_OK + MB_IconError);
        end;
    2 : Result := MessageBox(0, 'Windows is busy and cannot be closed properly at this time'#13+
                                'Do you want to force a shutdown ?',
                                'Shut down Windows',
                                MB_YesNo + MB_IconQuestion) = IDNo;
    else Result := false;
    end;
end;


function WProc(Window: HWND; Msg:word; WParam, LParam: Longint): LResult;stdcall;

begin;
case Msg of
  WM_QueryEndSession:
      begin;
      if NoToClose then Result := 0 else Result := 1;
      end;
  WM_EndSession : Terminated := (wParam=1);
  WM_Close      : Terminated := true;
  WM_Quit       : Terminated := true;
else
  Result := DefWindowProc(Window, Msg, wParam,lParam);
end;
if Terminated then Halt;
end;



function InitOk:boolean;

Const WinHPos   = 00;
      WinVPos   = 00;
      WinWidth  = 00;
      WinHeight = 00;
      ExStyle   = WS_EX_TOOLWINDOW;
      Style     = WS_POPUP;
      WinClass  = 'TWinMonitorClass';
      WinTitle  = 'WinMonitor';

var   wc : TWNDCLASS;
      a : Atom;

     i  : integer;
     s  : Shortstring;

begin;
Mode := -1;

//Look for and close previous instance, if any.
SendMessage(FindWindow(WinClass, WinTitle), WM_Close, 0, 0);

//setup and register window class
wc.style := cs_HRedraw or CS_VRedraw;
wc.lpfnWndProc := @WProc;
wc.cbClsExtra  := 0;
wc.cbWndExtra  := 0;
wc.hInstance   := hInstance;
wc.hIcon       := 0;
wc.hCursor     := LoadCursor(0, IDC_Arrow);
wc.hbrBackground := GetStockObject(LTGRAY_Brush);//Black_Brush);
wc.lpszMenuName  := nil;
wc.lpszClassName := WinClass;
a := RegisterClass(WC);
if a <> 0 then
   begin;
   //Now Create a window to receive messages.
   WHandle := CreateWindowEx(ExStyle, wc.lpszClassName, WinTitle, style,
                             WinHPos, WinVPos, WinWidth, WinHeight,
                             0, 0, hInstance, nil);
   if WHandle <> 0 then
      begin;
      ShowWindow(WHandle, sw_Show);
      UpdateWindow(WHandle);
      Result := true;
      end
   else Result := false;
   end
else Result := false;
Terminated := false;
end;


begin;
if not InitOK then Halt;

repeat
{if PeekMessage(M, WHandle, 0, 0, PM_REMOVE) then
  begin
  TranslateMessage(M);
  DispatchMessage(M);
  end;
}
sleep(INFINITE);
until Terminated;
end.
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1385531
Ben,

I was trying to find an article I have from Delphi Informant which actually explains in great detail why Delphi compiles such large EXEs compared to C++ (smaller than VB in total though!).

The reason Delphis EXEs are so much bigger than, say C, is because you do not have to create any low-level code such as message handlers.  You dont have to create your Windows in code - this is all done for you.  Basically, you are totally spoon fed your code - you dont have to really know anything to make an App in Delphi.

You can, if you are game enough, create your own event and message handlers, create all your own components from APIs or Windows supplied DLLs, design all your forms.  This will greatly reduce the size of your EXEs.  However, you will loose your RAD (Rapid Application Development).  You will basically be going back and writing Pascal like we used to in the days of TPW or OWL (and didnt we all totally enjoy that!!!).

The only way you can reduce your exe size, and still maintain the development environment we have with Delphi is to only use units you need, optimize your code, reuse code and components, stick as much as possible (especially graphics) into resource DLLs/DLLs, optimize your compiler settings for size and finally, if you are game, compress your exes.

I think the size of Delphi executables are fine considering all the hard work and time it saves me in the development stage.  Whats a few hundred Ks these days when most of us have 10+ gig hard drives and ISDN internet connections (or even 56K modems).

I'll put it this way: ITS A SMALL PRICE TO PAY FOR YOUR SANITY! :)


0
 
LVL 3

Expert Comment

by:philipleighs
ID: 1385532
Hey Madshi,

Remember the other day where you spotted that I quoted your code?

Well ahalya has just quoted mine (albeit from C -> pascal)!

PS: I think the issue here is the amount of memory the app uses, not the size of the exe.

Cheers,
Phil.

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1385533
Hehe, good code spreads itself...   :-)
0
 
LVL 3

Expert Comment

by:philipleighs
ID: 1385534
Right on!  :-)
0
 

Author Comment

by:bengore
ID: 1385535
Thanks for all your answers and comments!
Please run my example and study it with the task manager of WinNT:

program test;
uses Windows;
begin
  FreeLibrary(GetModuleHandle('OleAut32')); {is this wrong? an error occured when closing the program (runtime error 216)}
  MessageBox(0,'Now switch to the WinNT Taskmanager!', 'Test',mb_Ok);
end.

If you compile it with Delphi (2/3/4) then it needs 1.5 MB memory!! That is to much! How can I reduce the memory usage?
I have to write a program which run in the taskbar area. My competitor/revial have write a similar program in VisualC and his .exe needs only 75 KB memory!
Thank you for your help!
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1385536
Have you tried Phil's suggestion from

Wednesday, June 30 1999 - 05:00PM

?
0
 
LVL 7

Expert Comment

by:ahalya
ID: 1385537
Phil:

>  "Well ahalya has just quoted mine (albeit from C -> pascal)! "

When I started my WinAPI programming I asked questions on the net about the usage of the "CreateWindowsEx" function & its arguments.  In my code I might have used the examples from the net to create my Window.

May be you gave the answer for my CreateWindowsEx question (possibly in comp.lang.pascal.delphi.misc) but I'm sorry i don't recall who did.  

I didn't think that I have to state that I leaned to use this WinAPI functions from so and so, everytime i use WinAPI functions.

Further when i tell about a WinAPI function to someone, I don't expect then to credit me for its use in their answers.  (I hope you'll agree with me here: Hey, we all learned our WinAPIs someplace, didn't we ?)

I think I gave the reference of "RegisterServiceProcess" function to Madshi -in Q10088342- and when he gives that same function as the answer to someone elses question later -Q10109240- he was perfectly ok in not telling, from where he found the usage of the function.


0
 
LVL 3

Expert Comment

by:philipleighs
ID: 1385538
Hey take it easy ahalya!

My comment was to Madshi.
I didn't mean to imply that you owe me credits or anything.

After all, it is called E-E for a reason!

BTW, the Q is:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=10089658 

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1385539
Hi ahalya,

you're right. The situation was this: In a different question (one day ago) Phil posted some code (a bit more complex code) from me. And he wrote that it was not his code, but that he didn't remember who had written it. So I only wrote something like: "It was me... :-)". That's why Phil in this question said that you used HIS code. He said it more to me (because of the other question I mentioned) not to you. So none of us has any problems with using code from other experts...   :-)
Hmm. However, if I copy quite complex code (more than 20 lines or so) from another expert I usually write his name to the code, only to be polite. Of course that's no must. I did that not with the RegisterServiceProcess you gave me, because it was "only" the API description - though *VERY* helpful for me...

Please take it easy, guyz!   :-)

Regards, Madshi.
0
 
LVL 7

Expert Comment

by:ahalya
ID: 1385540
Hi Madshi &Phil:

Thanks for your cool responses.  I'm sport :-)

I agree with Madshi that RSP is a wonderful function (I found it in  comp.lang.pascal.delphi.misc)

Cheers,



0
 
LVL 3

Expert Comment

by:philipleighs
ID: 1385541
Good vibes guys.  :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question