Jump to content
Tuts 4 You

MSVC2008 - Removing MSVC Framework code..


Recommended Posts

Hello, folks. I managed (ONCE UPON A TIME) to make it so that everytime I compile an application in MSVC2008, the output file would be free of that __security_init_cookie + __tmainCRTStartup code the compiler/linker adds in. Now I can't seem to freakin' get the results properly with project's settings. Any ideas?

Here's how code looks like when compiled:



And I want it only to start from this:


As in, program's OEP to be this:


Tried options:

Code Generation -> Buffer Security Check -> No

Code Generation -> Enable Function Level Linking -> No

Code Generation -> Enable Floating Point Exceptions -> No

Code Generation -> Enable C++ Exceptions -> No

Language -> Enable Runtime Type Info -> No

P.S.: On top of that, I used these options:

Code Generation -> Runtime Library -> Multi-threaded (/MT) - so that I get rid of MSVCR90D.dll inits

Linker -> Optimization -> Keep Unreferenced Data - I have a function I wanna use in TLS (not called from anywhere in the code)

Here's also the code:

#include <windows.h>int MyFunction();
int tlsdone = 0;INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
return 0;
}int MyFunction ()
if(tlsdone == 0)
MessageBoxA( NULL,
tlsdone = 1;
return 0;

It's from a public tutorial I read a while ago. Can't seem to shake the damn CLR off T_T. Olly tells of more info:

- while at EP, I see this:



- while at 4021BC, I get this:


Where in god's name are these references coming from? o_O

Edited by SunBeam
Link to comment

Got it. Forced in Linker -> Command Line -> Additional options: /ENTRY:WinMain ;-)

EDIT: After a few tweaks, this is how the file looks like. Beat that, ASM!!! :-)


File is 2 KB :-)

Used options below.

Configuration: Active (Release)

General -

Edited by SunBeam
Link to comment

Hi Sunbeam,

In this case you should not declare WinMain like this :

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);

Because these args are initiated by the startup stub :) , you should declare it like this :




Edited by GamingMasteR
Link to comment

Hello. True observation, thing is these args never make it in the final build :-) It's as if you were writing stripped-down ASM code.

UPDATE: File size got smaller to 1 KB by tweaking these :)

Linker -> Manifest File -
Edited by SunBeam
Link to comment

Hello, back again. I've started doing something, and ended up doing something else (as usual, lol). The quest I was after was this:


And the result is shown as below:






@GamingMasteR: Tried /ALIGN earlier, makes a small difference in this case. I changed from default (1000 for SectionAlignment) to 512. Lol, funny thing is that if we are to wipe out the remaining 00s, we get a file sized 729 bytes :-)

#pragma comment(linker, "/ALIGN:512")

@HVC: I'll look it up :-)

I am a stupid f*g :-) I could've easily used up this:

#pragma comment(linker, "/INCLUDE:__tls_used")

And gotten my self a direct TLS to edit T_T. Instead I preferred went the other way around and added it manually LOL...

Edited by SunBeam
Link to comment

You can add TLS data within the code itself rather then doing it by hand:


As for your optimizations, you shouldn't remove the manifest, as it is there to tell the system what runtime is needed for your application as well as handling security rights on Vista for the UAC if its enabled.

If you want to get a smaller size, don't statically link to the runtime, meaning change the runtime library to Multi-threaded DLL (/MD). However the price you pay with this is that users will be forced to have the runtime installed (free downloads from Microsoft) but the size is noticeable.

Redirecting the entry point can also land up causing issues later down the road with larger scale projects and using certain API and macros so keep in mind that if you plan to use it, that you are debugging thoroughly before releasing to ensure your project fully works. (More then most you wont be able to compile with the issues that arise while using this trick.)

And you also remove the security checks, keep in mind, doing that can cause unexpected errors to happen on things that used to be automatically handled. If you are removing them, you should look into doing more in-depth security checks yourself then, using try/catch blocks, extra variable checks, pointer checks, etc.

All in all, the price you pay for getting smaller size isn't really worth the losses. After removing all the things you don't want, you land up recoding them by hand to ensure that the stripped things don't cause issues, which lands up just pushing your projects size back up to what it was before. It's 2009, a few extra KB here and there isn't a huge deal, grab a packer, use 7zip, or create an installer for what you are making. Pushing for extremely small sizes isn't really worth it.

Link to comment

That wasn't my goal. But as I said, I ended up doing a totally different thing. Mainly, I was trying to get rid of that annoying sysinit wrapper, which always makes me waste time in Olly T_T.. Having EP set directly works wonders..

Link to comment
  • 11 months later...
  • 2 weeks later...

I do it everytime when I forget what I did to remove all those useless craps :-) Cheerios ;-)

got the same problems with static lib and VS2008. Using them with

MASM it want to have a entry "MAIN". i managed it with some tools to remove,

but there are no linker options to remove it for a static lib.

anybody an idea?? :dunno:

thanx in advance.


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...