3

I want to start a simple program when windows start but I don't want to show the console output window associated with that program. Just to test, the program can be as simple as:

    int main (int argc, char** argv)
    {
        while (1)
        {
             printf ("hello world...\n");
             Sleep (10000);
        }
    return 0;
    }

compiling it as: cl foo.c

I know how to start the program when windows is starting (putting in startup folder, creating registry etc), but I don't know how to hide the console output window that comes when the program is started.

While searching for this, I found that I can use start /B foo.exe. Is there any other method for doing this? some thing like "&" we use on unix.

My actual program is big and is written in C, so I can not use some other alternatives i found for c# (like WinMain) and java (like explained here).

Community
  • 1
  • 1
aajtak
  • 269
  • 1
  • 3
  • 8

7 Answers7

8

This should work.

#include <windows.h>

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdParam, int iCmdShow)
{
    for (;;) {
        //action here
    }
    return 0;
}
Juho
  • 983
  • 5
  • 9
  • note that if you omit WINAPI in the signature it won't work , I don't know why actually but it took me 3 hours to know that and i felt someone will find this useful someday – mahmoud nezar sarhan Jul 27 '17 at 16:20
  • `WINAPI` is an alias for `__stdcall` otherwise the default `__cdecl` calling convention will be used – imbr Dec 18 '19 at 19:10
5

WinMain is not a C# entry point. C# uses a method of a static class; it is called Main by default, but can be configured to any static-class method.

In Windows, non-console C programs should define a WinMain. You can then link them using the linker's /subsystem:windows command-line option, invokable either directly or from CL.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • Wouldn't the `/subsystem:windows` commandline option with plain `main` work equally well and be more portable? – rubenvb Jun 14 '11 at 11:59
  • @rubenvb: No, the signatures and calling conventions are different. – Marcelo Cantos Jun 14 '11 at 12:00
  • I use Eclipse and it links everything for me and i dont really know how to change it, could someone show me how? – gsfd Jul 15 '11 at 02:31
  • @John: Don't ask a question by commenting on an answer to another question. Only @aajtak, @rubenvb and I are likely to see your comment, and possibly none of us knows Eclipse (I don't). Raise a question yourself, and link to this question if it's relevant. – Marcelo Cantos Jul 15 '11 at 03:11
  • 1
    Using `/ENTRY` will mess up the C runtime initialization. You should probably remove that bit. – Harry Johnston Aug 16 '15 at 03:16
1

Unfortunately simply setting FreeConsole() as the first function in main allows the window to appear momentarily anyway. And FreeConsole removes the window so that if you wish to use it later( as in killing the process ) you have to make it appear on screen in a mode out of your control.

Windows allows Win32 programs to have only one of four contexts under Visual Studio: Window program with initial GUI window, Console program with initial Console window, DLL or Lib. Changing the subsystem to a non-Console choice from the project->Properties->System view only results in linking issues that block the build.

Here is what worked for me with only a little effort. Use Mike's approach above and choose Win32 Project with Window Application. Then delete everything in WinMain after the "Place code here" direction and delete all the called functions. Return true or 1, as you wish from WinMain. No window of any type will appear on launch.

And when you are ready to deal with a Console Window call AllocConsole() in your code and deal with its positioning and size as you see fit. The Console can be positioned off screen and slid into view if you wish; it only takes a few minutes to get the handle on the configuring functions. Start with Microsoft's 'Console Functions' in MSDN documents. Unfortunately, there is no book on how to use all the functions properly as there is for NCurses in Linux.

1

One method is calling FreeConsole() as first thing in main. It will hide the console window, if any.

stijn
  • 34,664
  • 13
  • 111
  • 163
0

If the program would be eg procexp.exe you can do this out of the box :

cmd /c "start C:\Users\denni\OneDrive\_bin\_tools\ProcessExplorer\procexp.exe"
0

When you're creating your project create one with WinMain instead ( Win32 Project ). If you still want the console later use AllocConsole() and FreeConsole().

Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
-1

WinMain isn't unique to C#. It's possible to write GUI applications in C too. The WinMain function is the entry point, but nothing says you have to actually create a window. You could have WinMain do nothing more than call the first function of your program to get it started. Then you'd have your program running with no GUI window and no console window.

Of course, this also means no easy way to stop it, short of killing it from Task Manager.

Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125