Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. EXE generated by VC++ : big size !

EXE generated by VC++ : big size !

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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..).

    L F T 3 Replies Last reply
    0
    • L Lost User

      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..).

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Ooops... #include of course...

      1 Reply Last reply
      0
      • L Lost User

        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..).

        F Offline
        F Offline
        Felix Cho
        wrote on last edited by
        #3

        Nice morning exercise to start the day! ;) Let me outline my steps:

        1. 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's WinMainCRTStartup 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.
        2. 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!
        3. 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.
        4. My final step is to realize that I am doing a 2-step function call to get to the MessageBox.  So I killed my WinMain and make my entry point function call MessageBox 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 2048

        So there you have it, my morning exercise. I

        1 Reply Last reply
        0
        • L Lost User

          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..).

          T Offline
          T Offline
          Tim Deveaux
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups