2

I am trying to run an executable in background written in C++ in Windows 10. The program does not have any GUI. I tried this

window = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(window,0);

But although it does work on Windows 7 and opens and instantly closes the console, and the executable runs in background, on Windows 10 it opens a console and the console stays open. If I run the .exe with run /B main.exe I have to keep the console open in order for the program to keep running, and if I close the cmd console execution stops.

Is there a way to programatically run the file in background? I am aware that I can run it in background using vbscript, but I would like to avoid that.

6
  • 1
    You could explicitly create a process for the application. Unless it contains code to explicitly display the console window, it shouldn't be shown IIRC. Commented Sep 23, 2019 at 13:18
  • But that would mean having to write 2 programs, right? As far as I have read in the docs, the input parameters for createprocessa requires me to give the path to the executable. I would like to have it all in one single program, both the option to run in background as well as the rest of the main program. Commented Sep 23, 2019 at 13:35
  • So you have one program, which executes itself? Doesn't matter, you can still use CreateProcess to execute any program you have the path to, including itself. Or (not really knowing the problem you're attempting to solve) perhaps what you really want is to use threads to run some parts of your program in the "background"? Commented Sep 23, 2019 at 13:37
  • CreateProcess() has flags to hide the process it spawns. If you spawn a console app, you can tell it to hide the console window that is created. You don't need to hunt for the window afterwards and hide it manually Commented Sep 23, 2019 at 16:26
  • 3
    @Hamperfait Did you try the FreeConsole()? The console disappears if I start it without the debugger -- remains visible when started from Visual Studio. So it should do the job. I have only a win10 -- so I don't know the behavior on others.. Commented Sep 25, 2019 at 5:12

1 Answer 1

3

With FreeConsole() you can detach a process from the console window -- that worked for me:

int main() 
{
   FreeConsole();
   while (1) ::Sleep(1000);
   return 0;
}

For detailed description see also the related Microsoft documentation

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain better the answer and source code, thanks
I added a link to the microsoft doc -- check the function's doc for further reading.
@LXG: The documentation cited links to AttachConsole, which explains "...A console is closed when the last process attached to it terminates or calls FreeConsole"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.