Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I correctly set the entry point for an exe in Visual Studio?

Tags:

c++

I have set the entry point to WinMain but when I run the app it starts and doesn't display, I then have to shut it with task manager. Here's the code upto WinMain() :

#include <Windows.h>

// forward declarations
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); 

// The entry point into a windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int windowStyle )
{
....

I'm not experienced in C++, and I don't know what this is doing, except making my exe smaller, which is what I'm trying to achieve.

edit : What I'm trying to do is create a very small window exe to understand how demo coder's work. So I'm thinking of creating a small c++ window app that provides a window handle to which I can attach SlimDX (if i can statically link the final c++ dll to a C# app, but I'm not there yet) I have my BasicWindow.exe down to 6,656 bytes. So I'm experimenting with anything I can find to get that size down to <3k.

[2012.Jan.10] Well I've had some success by rebuilding minicrt (available from http://www.benshoof.org/blog/small-programs/) under VS2010 and adding it as an additional dependency. I couldn't Ignore All Default Libraries as suggested, but I now have a Windowed application with an exe size of 4,096 bytes. I'd say that's some significant success. I'm within striking distance now. Every reduction from here on in, is more room for SlimDX. I'm pretty happy considering the only c++ apps I've ever written are console apps and a basic window :) I've been lucky I know !

like image 484
Gavin Williams Avatar asked Jan 09 '12 15:01

Gavin Williams


People also ask

Which is the entry point of Windows application?

WinMain() and the Windows Procedure As you may have already noticed, the return type for WinMain is, and always will be, int. All 32-bit Windows operating system applications use the calling convention WINAPI. This calling convention MUST be used to distinguish the function as the entry point.

Should I use WinMain or wWinMain?

The only difference between WinMain and wWinMain is the command line string and you should use wWinMain in Unicode applications (and all applications created these days should use Unicode). You can of course manually call GetCommandLineW() in WinMain and parse it yourself if you really want to.

How do you define an entry point in C++?

The entry point of a program is where it starts executing at the machine code level. That's seldom if ever main ; instead, the entry point function does a few initialization tasks and then, for a C or C++ program, calls main .


1 Answers

A typical application should not mess up with Entry point setting of linker. Entry point should be set on a function included in the standard runtime library (which is wWinMainCRTStartup for unicode application for windows subsystem). This function does stuff like the proper initialization of CRT and creation of global objects. By rerouting entry point to your WinMain you will get undefined behavior unless you know precisely what you are doing and somehow implementing CRT initialization in your own WinMain. In my opinion the resulting size decrease will be negliable and the whole affair is hardly worth the risk.

like image 87
Pavel Zhuravlev Avatar answered Sep 23 '22 07:09

Pavel Zhuravlev