3

I am using old bcc32 (borland 5.5) (I know this is very old compiler but do not tell me I should do not use it )

I can compile with console or with windows subsystem.

I do not want to compile with windows subsystem, I would like the console with just main() ad no "windows.h" include (from there I call my own library which opens the window and handles the message pump internally on its own)

It works but problem is just that when I compile with console subsystem there ugly blue console window appears and I do not need that

So I think I just need no windows subsystem compile, no console subsystem compile but just make my program compile in some raw mode (both with no console creatuion and witn no WinMain entry point - how to do it ?

Johan
  • 74,508
  • 24
  • 191
  • 319
grunge fightr
  • 1,360
  • 2
  • 19
  • 38

1 Answers1

5

Generally, when you don't want a console, you should choose the "GUI" or "Windows" subsystem (depending on what your development environment elects to call it). That controls which bits the linker sets in your EXE header. You can't control the subsystem after your program has started running because the OS readers your EXE file and determines the subsystem to use before executing any code in your program.

Using the Windows subsystem does not require that you have any windows; it just tells the OS when it's loading your program that you don't need a console. It has nothing to do with whether you use any Windows APIs. Both subsystems are full-fledged Windows applications and offer full use of Windows API functions.

You can do all the same things from WinMain as you can from main. There are functions for fetching the command line and splitting it into separate arguments.

However, if you don't want your main function to be called WinMain then you cannot use the Windows subsystem. The choice of subsystem also controls which set of preparatory code the linker puts in your program. The preparatory code is where the real entry point of your program lives, and it's what initializes the C++ standard library and calls the constructors of any objects with namespace scope. The code included in console mode calls main (or wmain); the code linked for the Windows subsystem calls WinMain (or wWinMain). If you use the Windows subsystem and don't define a function named WinMain, then you will get errors when you link; the linker will complain about an undefined function. (You do not have to include windows.h to define WinMain, if inclusion of that header is what you're afraid of.)

Since the subsystem to use is just a set of bits in the EXE header, you change change it after linking has occurred with the editbin program. When you link, select the console subsystem so you get the console preparatory code and main, and then modify the binary to use the Windows subsystem instead:

link foo.obj /out foo.exe /subsystem:CONSOLE
editbin /subsystem:WINDOWS foo.exe

Then your program will run without a console. Beware that if there are any problems while starting up, the preparatory code might crash your program because it was written expecting to have a console available to write to, but it won't be there.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • But I strongly do not want to include "windows.h" and do not want a WinMain entry point (I just want to include "my_own_framework.h" and had a simple main() function) - With concole subsystem it works perfectly well but just this console appears :c Can I use windows subsystem and do not include windows h and do not use WinMain - main() wilg bring a compile error as far as I remember – grunge fightr Apr 21 '13 at 16:13
  • 2
    @grungefightr: There is no way to create a console app with a `main()` entry point and not have a console window created for it by the OS. You have to create a GUI app with a `WinMain()` entry point in order to remove the console window, whether or not you actually create a GUI in your code. That is simply how Windows works. – Remy Lebeau Apr 21 '13 at 16:27
  • I suspect that console-mode exe startup stub just calls something like CreateConsole api call and it would be good for me just to not call it - Do not need damn console and damn WinMain here also :c – grunge fightr Apr 21 '13 at 16:31
  • 1
    Grunge fightr: afaik those are flags in the binary header, not stub code. You could try to hide the console after startup, but it would probably quickly flash. Note afaik not being a console has other difficulties too, DrWatson (or its successors) might kick in if you do not poll for messages occasionally. Same for console with threading initialized iirc – Marco van de Voort Apr 21 '13 at 17:01
  • You asked the question and have received the answer. If you want a console, target the `CONSOLE` subsystem. If you do not want a console target the `WINDOWS` subsystem. – David Heffernan Apr 21 '13 at 19:02
  • 1
    @RemyLebeau You can override the `SUBSYSTEM` when linking (or change it afterward with `editbin`). http://stackoverflow.com/questions/2422430/hide-console-of-c-program-in-window-os/2424210#2424210 – jamesdlin Apr 21 '13 at 19:13
  • @jamesdlin Of course. But how does that related to the matter at hand? – David Heffernan Apr 21 '13 at 19:34
  • @DavidHeffernan grunge fightr wants to use `main()`, and Remy said that you have to create a GUI app with `WinMain()`, which isn't exactly true. – jamesdlin Apr 21 '13 at 19:36
  • @jamesdlin Well, that's not how I read the question. I read the question that grunge wants to use neither CONSOLE nor WINDOWS subsystem. – David Heffernan Apr 21 '13 at 19:39
  • @DavidHeffernan I read it as grunge being confused. He doesn't want a console window and thinks that using `SUBSYSTEM:WINDOWS` entails doing a lot of other things he doesn't want (which isn't true). – jamesdlin Apr 21 '13 at 19:44
  • I do not want to have to include windows.h /using WinMain entry point Console would be good but besides its console window (I am using my own libs to output things - do not want ugly console window do not want ugly winapi calls_ – grunge fightr Apr 23 '13 at 12:18
  • +1: Fixing the Subsystem saved me from a big trouble. However, EditBin didn't work for me. I've had to create a small app to do the job. Now I run it whenever recompiling the target/main app. Your answer is THE answer! :) – Paulo França Lacerda Nov 30 '16 at 07:16