Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile a C++ program with only dependency on kernel32.dll and user32.dll?

I'm working with Visual Studio 2005.

I want to compile a simple program that will work with any Windows 32bit version independent of what c++ runtime library version installed.

This program will call to GetModuleHandle and GetProcAddress functions without any other function calls, and then exit, when the exit code is the function address.

How to compile a C++ program with only dependency on kernel32.dll and user32.dll, without any c++ runtime library?

like image 598
DxCK Avatar asked Jan 24 '11 19:01

DxCK


2 Answers

You'll need to define your own entry point instead of using main or WinMain. Your entry point is a void function taking no arguments. You have to specify its name to the linker with /entry:funcName (where funcName gets replaced by whatever name you gave the function you want to use as the entry point).

When you do this you'll also have to specify the subsystem to the linker, as in /subsystem:console. It normally deduces the subsystem based on the name of function it finds (i.e., main -> console, WinMain -> Windows), but when you use your own entry point, you have to specify it explicitly. Although you probably don't want to very often, you can specify the subsystem explicitly even when you don't specify your own entry point, so (for example) you can use main as the entry point to a windows subsystem program, or WinMain as the entry point to a console program.

like image 144
Jerry Coffin Avatar answered Oct 14 '22 03:10

Jerry Coffin


Set /NODEFAULTLIB under your project options. In newer versions of Visual C++, you'll also have to turn off stack overrun checks, because those cause the compiler to automatically insert calls to library functions.

EDIT: If you really mean "run on ANY 32-bit Windows version", you're also going to have to use editbin to change the subsystem version field in the PE header. Otherwise you're restricted to (IIRC) Windows 2000 and later when building with the VC++ 2005 linker, and newer versions of VC++ are even worse (requiring XP by default). Windows 2000 is 5.0, you'd want to specify 3.5 or thereabouts to allow all versions of NT in addition to Win9x.

like image 38
Ben Voigt Avatar answered Oct 14 '22 04:10

Ben Voigt