1

Most of Windows API hello world programs in books and online resources have fixed form. The program starts with WinMain() and it creates and registers window and goes into message loop until the end.

#include <windows.h>

int main(void)
{
    MessageBox(NULL, "content", "title", 0);
    
    /*
     *
     * code which use any functions in 'windows.h'
     * 
     * 
     */

    return 0;
}

However, is this form also legal?

Clifford
  • 88,407
  • 13
  • 85
  • 165
op ol
  • 705
  • 4
  • 11
  • 1
    Does this answer your question? [WINMAIN and main() in C++ (Extended)](https://stackoverflow.com/questions/13871617/winmain-and-main-in-c-extended) – Michael Jun 17 '21 at 06:03
  • @Michael I saw that post but I didn't found some related thing. I have to reread it slowly. Thank you. – op ol Jun 17 '21 at 06:08
  • 2
    Why not just try it? – Clifford Jun 17 '21 at 06:13
  • @Clifford I wrote my code based on that form. It works as my thought and is pretty cool. But I'm not sure whether this form is stable and has many hidden potential bugs. MSDN is official winapi documentation website but it is so complex(and I think it doesn't explain perfect detail) and Petzold's book is also great but I doesn't contain every detail(I'm so pedantic and try to control all corner cases and want to know low-level mechanism). So I asked the question for more detail. – op ol Jun 17 '21 at 06:22
  • 1
    of course you can use this form. why not ? another question that usually `main` used for console app and this apps not use gui. – RbMm Jun 17 '21 at 06:22
  • 2
    The two different forms is mostly something that remains from the time where Windows was just a GUI for MS DOS. MS DOS programs used `int main (void)` so Windows ones had to use something different. Particularly during the transition from 16 to 32 bit when Windows 95 was released. Nowadays, using `int main (void)` rather just means that you attach a console to a Windows program. – Lundin Jun 17 '21 at 06:41

1 Answers1

4

First things first: The symbols main and WinMain are conventions1. Using either one, or even a completely different symbol as the entry point, doesn't change anything about the program. Specifically, the entirety of the Windows API is available, irrespective of the entry point symbol chosen.

The convention becomes contractual when relying on a runtime. The overwhelming majority of applications written in C use the C Runtime (CRT). Microsoft's CRT implementation requires that programs provide an entry point called main or WinMain (or the respective Unicode variants) that it can call once initialized.

Whether Microsoft's CRT requires main or WinMain is based off of the /SUBSYSTEM linker option. When targeting the CONSOLE subsystem, the CRT requires the symbol main. Conversely, when targeting the WINDOWS subsystem, it requires WinMain.

An executable image targeting either the CONSOLE or the WINDOWS subsystem is identical, with a single exception: The OS will automatically allocate a console for a program that targets the CONSOLE subsystem, and will not allocate a console for a program that targets the WINDOWS subsystem. Everything else is all the same.

However, is this form also legal?

Yes, this is perfectly legal. Everything works just the same (modulo the arguments passed into the entry point), but the system will allocate a console for you. Particularly for learning this is a nice ad-hoc solution to get printf-style logging/tracing.


1 WinMain is just the conventional name for the Win32 process entry point

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • Isn't `WINAPI` calling convention on `WinMain()` directly related to using `windows.h` function?(I can use main() because they are irrelevant?) I'm sorry if the answer is already in your post. I think I should revisit your answer several times to fully get it. There are many convention and principles to know. But it definitely answers what I asked. Thanks. – op ol Jun 17 '21 at 08:40
  • 1
    Calling conventions are a contract between the caller and callee of a function. That contract is per function. A function with one calling convention can call a function with a different calling convention. You can freely mix any number of calling conventions inside a single module. The fact that `WinMain` and `main` have different calling conventions (with Microsoft's CRT) isn't very interesting. The compiler knows how to interpret calling conventions to generate correct code for function calls. – IInspectable Jun 17 '21 at 12:28