aajtak
aajtak

Reputation: 269

start a program without a console window (in background)

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).

Upvotes: 3

Views: 14234

Answers (7)

Dennis Bernaerts
Dennis Bernaerts

Reputation: 81

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"

Upvotes: 0

Marcelo Cantos
Marcelo Cantos

Reputation: 186118

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.

Upvotes: 5

Robin Wernick
Robin Wernick

Reputation: 46

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.

Upvotes: 1

Juho
Juho

Reputation: 953

This should work.

#include <windows.h>

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdParam, int iCmdShow)
{
    for (;;) {
        //action here
    }
    return 0;
}

Upvotes: 8

Michael Kristofik
Michael Kristofik

Reputation: 35218

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.

Upvotes: -1

stijn
stijn

Reputation: 35921

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

Upvotes: 1

Mike Kwan
Mike Kwan

Reputation: 24477

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

Upvotes: 0

Related Questions