Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call WinMain from regular main?

In particular, is there a way to create a windows application without directly specifying it with the 'new project' wizard? Obviously simply including "windows.h" does not automatically create a main function, so if I wanted to create a windows application "from scratch" how would I go about doing it?

like image 648
sircodesalot Avatar asked Jan 14 '23 06:01

sircodesalot


2 Answers

When a program starts up, the start or _start function is called. The definition of this function is included in a library that is usually automatically linked in. In a standard C program, it will do some start-up things and then call your main function.

That's what happens in a standard C program, but you can use WinMain instead, which is not standard. If main is not present but WinMain is, it will call WinMain instead with the appropriate parameters.

As such, it is not necessary to have a main function; WinMain serves that purpose rather than main.

like image 120
icktoofay Avatar answered Jan 19 '23 04:01

icktoofay


icktoofay's answer is mostly right, save for one part:

That's what happens in a standard C program, but you can use WinMain instead, which is not standard. If main is not present but WinMain is, it will call WinMain instead with the appropriate parameters.

The deciding factor in what an application's secondary entry point (the function called by start or _start is) is which subsystem is specified to the linker.

If you're building manually, you can add the /SUBSYSTEM switch to the linker command line to specify that you're building a Windows application (that will expect a WinMain or wWinMain entry point) as opposed to a console application (that will expect a main or wmain entry point). If you're building from Visual Studio, you can choose the subsystem in the linker settings of the C++ project properties.

like image 40
Adam Maras Avatar answered Jan 19 '23 02:01

Adam Maras