EXE generated by VC++ : big size !
-
Hi, Just a little question (not very important... just to know ;o))) : I compiled the following code in VC++ (no MFC) : // Test.cpp #include int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox(NULL, "Hello World", "Hello", MB_OK); return 0; } And the EXE size is 36 ko ! I often saw EXE's of 5-10 ko, is there a way to reduce the size ?? (I mean, without compressing the EXE with UPX or other executable packers..).
-
Hi, Just a little question (not very important... just to know ;o))) : I compiled the following code in VC++ (no MFC) : // Test.cpp #include int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox(NULL, "Hello World", "Hello", MB_OK); return 0; } And the EXE size is 36 ko ! I often saw EXE's of 5-10 ko, is there a way to reduce the size ?? (I mean, without compressing the EXE with UPX or other executable packers..).
-
Hi, Just a little question (not very important... just to know ;o))) : I compiled the following code in VC++ (no MFC) : // Test.cpp #include int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox(NULL, "Hello World", "Hello", MB_OK); return 0; } And the EXE size is 36 ko ! I often saw EXE's of 5-10 ko, is there a way to reduce the size ?? (I mean, without compressing the EXE with UPX or other executable packers..).
Nice morning exercise to start the day! ;) Let me outline my steps:
- Take out the C runtime library (idea from ATL):
The compiler by default will link in CRT startup code before actually calling your entry point function. So the first step (and the biggest) step is to remove those CRT stuff since you are just using Win32 API. Borrowing idea from ATL'sWinMainCRTStartup
I put something like this:
void WinMainCRTStartup(void) { WinMain(GetModuleHandle(NULL), NULL, NULL, 0); return; }
This takes 20K :omg: out of the exe and it is down to 16K. - I tried a few things and I think the compiler just won't reduce the size further. It always give you 3 segments and stuff. So I thought of UPX, the exe compression tool (I know I cheated a bit here, but I don't know assembly! :-D). I used it to compress my exe and boom... it is down to 2560 bytes!
- Without resorting to assembly (I don't know the PE format too much :p ), I opened up the original exe in binary mode and I noticed a lot of blanks (to fill up the segments I suppose). So I thought, "if I could make the segments more blank then I may be able to get more compression out of this". I notice that I am calling 2 functions from Win32 API, killing 1 of them should make the import table more compressible. Since I am not interested in the module handle, I make that NULL:
WinMain(NULL, NULL, NULL, 0);
Unfortunately that didn't work, UPX still compressed it to 2560 bytes. - My final step is to realize that I am doing a 2-step function call to get to the
MessageBox
. So I killed myWinMain
and make my entry point function callMessageBox
directly:
void WinMainCRTStartup(void) { MessageBox(NULL, "Hello World","Hello", MB_OK); }
Now, that didn't do much in terms of the compiler, it still gave me 16K, but UPX made it down to 2048 bytes! I guess my code segment is more compressible now.
To sum up, here is a little table for you:
Before UPX After UPX
Plain old WinMain() 36384 12288
Minus CRT 16384 2560
Minus GetModuleHandle() 16384 2560
Direct call 16384 2048So there you have it, my morning exercise. I
- Take out the C runtime library (idea from ATL):
-
Hi, Just a little question (not very important... just to know ;o))) : I compiled the following code in VC++ (no MFC) : // Test.cpp #include int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox(NULL, "Hello World", "Hello", MB_OK); return 0; } And the EXE size is 36 ko ! I often saw EXE's of 5-10 ko, is there a way to reduce the size ?? (I mean, without compressing the EXE with UPX or other executable packers..).
There's an MSJ Under the Hood article by by Matt Pietrek that talks about some of this stuff too - search the MSDN for 'TINYCRT'. Also, you might want to look at this (also by Matt Pietrek) for a more general overview of how compiler and linker settings affect app size.