Small EXE/DLL << Back



There is an excellent article written about how to reduce EXE/DLL size, take a look at here. The article was written a long time ago, but most of the points in that article are still applicable. Here are the steps to create small EXE/DLL in Visual Studio 2019.

  1. Open Visual Studio 2019
  2. File -> New -> Project
  3. Type in "Windows Desktop Application" in the search box (or "Dynamic-Link Library (DLL)" for creating DLL)
  4. View -> Solution Explorer
  5. Delete all the files under "Header Files" and "Resource Files"
  6. Project -> Properties
  7. Configuration: Release
  8. C/C++ -> Code Generation -> Security Check -> Disable Security Check (/GS-)
  9. C/C++ -> General -> SDL Checks -> No (/sdl-) DLL only!!
  10. C/C++ -> Precompiled Header -> Precompiled Header -> (Not Using Precompiled Headers) DLL only!!
  11. Linker -> Input -> Ignore All Default Libraries (Yes /NODEFAULTLIB)
  12. Linker -> Manifest File -> Generate Manifest -> No (/MANIFEST:NO)
  13. Linker -> Advanced -> Randomized Base Address -> No (/DYNAMICBASE:NO) EXE only!! You are giving up the ASLR feature.

Under Solution Explorer, you should have a file called WindowsProject1.cpp (for EXE) or dllmain.cpp (for DLL). If you see a pch.cpp, remove it.

// For EXE, replace the content of WindowsProject1.cpp with the following

#include <windows.h>

#ifdef _DEBUG
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nCmdShow)
#else
int WINAPI WinMainCRTStartup(void)
#endif
{
	OutputDebugString(TEXT("Hello"));
	ExitProcess(0);
	return 0;
}

// For DLL, replace the content of dllmain.cpp with the following

#include <windows.h>

#pragma comment(linker,"/export:Hello=_Hello@0")
EXTERN_C void WINAPI Hello()
{
    OutputDebugString(TEXT("Hello"));
}

#ifdef _DEBUG
EXTERN_C BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
#else
EXTERN_C BOOL WINAPI _DllMainCRTStartup(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
#endif
{
    return TRUE;
}




Free Web Hosting