Launch command line programs behind all open windows from a C++ executable - c++

I am working on C++ program in windows which launches numerous external programs using command lines in quick succession after previous one finishes. Currently a new terminal pops up in front every time I make an external program call. I have tried SYSTEM an POPEN. Is there a way to launch these terminals in the back behind all open windows so that its not annoying to the end user who may be working on other stuff?
One solution is listed here but doesn't work for me as it still pops up terminal in the foreground.
c++ run system() in background, with spaces in dir path, + output to text file
system("start \"\" cmd abcd.exe");

Unless you really need to use system(), I would recommend using ShellExecuteA, if you set nShowCmd to 0, it doesn't generate any windows or pop up any consoles.

Related

Suppressing stdout for a system() call in Windows (C)

Like this I have a cross platform piece of code which needs to start a separate process, I'm using a system() call for ease of use and because it is cross-platform, but in Windows I can't find a method of stopping the stdout from the system() call going to the terminal window.
This is very annoying as the .exe the system call starts has a lot of pointless outputs which can't be silenced with options.
Here's the code:
std::system("wgrib2 C:/Inputs/HRES_ENS_2014032500+000.grib2 -lola -8.0:140:0.1 48.0:140:0.1 C:/Inputs/HRES_ENS_2014032500+000.bin bin -for 2:2:1");
In case it makes any difference, I'm using minGW as a compiler.

C++ disallow command prompt from displaying

Is there any thing I can do to make sure that the shell/command prompt does not show up when I run my C++ program, it is meant to be a background program? While it isn't a huge deal if it displays because it quickly closes, I would prefer it doesn't display at all. Program will run on windows xp/vista/7 OS
Configure your compiler or linker to mark your program as a GUI application.
Windows recognizes two main types of programs: GUI and console. If the EXE header is marked as a console program, then the OS creates a console window prior to executing it. Otherwise, it doesn't. This isn't something you can control at run time; you need to set it at link time. (You can call ShowWindow(GetConsoelWindow(), SW_HIDE) to try to control it at run time, but that doesn't prevent the window from flickering on and then off again. Plus, if your program is sharing the console with another program, like cmd.exe, then you'll have just hidden the user's command-prompt window!) Even if your program doesn't have any actual GUI, that's still the mode you need to avoid having a console window created for you.
If you're starting a new project in Visual Studio, select the "Win32 Console Application" option. If you already have a project, then in your project's configuration properties, find the "Subsystem" setting of the "Linker/System" section and set it to "Console." That makes the linker use the /subsystem:console option. If you're using Mingw, use the -Wl,--subsystem,windows option.
Sounds to me like you want to create a Windows service, not a command line utility. This is why services exist. Long running background tasks that require no user interaction. A simple example here.

AIR NativeProcess API

I use nativeprocess api in AIR to launch a c++ console app. The console app runs correctly but does not appear, but I want it to be visible and user be able to interact with it. How can I achieve that?
Instead of launching your executable directly, you'll need to launch your platform's terminal application (on Windows, that's CMD.exe, on OS-X it's Terminal.app, and on unix/linux it's xterm).
By default, the terminal application will run an interactive shell prompt, but you can use command-line arguments to tell it to execute any other program instead. In this case, you'll want to tell it to execute your C++ console application.
On Windows, this might look something like this:
CMD.exe /K C:\path\to\your\app.exe
on OS-X, it's a little more complicated. Here's a related S.O. post ( Running a command in a new Mac OS X Terminal window)

Eclipse C++ - output to window DOS

I am using Eclipse with C++. When I run the program, I get the following message in my Console window:
**** Build of configuration Debug for project Disks Repulsion ****
**** Internal Builder is used for build ****
Nothing to build for Disks Repulsion
I makes changes to the program and run it again, and this time I get following message:
**** Build of configuration Debug for project Disks Repulsion ****
**** Internal Builder is used for build ****
g++ -oDisksRepulsion.exe DisksRepulsion.o -lopengl32 -lglu32 -lglut32
C:\MinGW\bin..\lib\gcc\mingw32\3.4.5........\mingw32\bin\ld.exe: cannot open output file DisksRepulsion.exe: Permission denied
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 103 ms.
When I first ran the program, it started my program (I can see it running in my Window Task Manager), but there is no output being sent to the console.
After making changes to my program, the program is still running in the background, so I get the "Permission denied" error, when I try to run the program. I can make the error go away by ending the instances via task manager. However, when I run the program, I still don't see any output being sent to the console.
Yesterday, I was able to see the output in my console, but don't know why I cannot see it today. Also, when I saw my output, the cursor's focus did not change from the code to the console. I dislike having to do it manually.
I also don't like that when I make changes to my program, and run it again, that the program is not automatically terminated on its own.
I have used VC++ and I prefer the window Dos for output. So, I wanted to know if there is anyway in Eclipse to send the output to Windows Command Prompt, since I know that the instance of the .exe is really gone when I close the program. It automatically gets cursor's focus. I will also be able to get some output.
I installed MinGW with MaSYS or something, to compile the program.
It sounds like your application isn't terminating on its own and you didn't build in any sort of interface that would allow you to kill the program. This sounds like a bug in your code and not an Eclipse issue. If issuing a Ctrl+C in the console won't kill your program, then look into coding something that will let you kill your app with keystroke or input sequence.
If you want to run your app in a command console, then open a normal command console, browse to the folder containing your project, and run the compiled executable from the console instead of doing it through the Eclipse interface.
A simple solution, which I've been using for ages now, is opening a command prompt yourself and running the executable manually. An advantage of this method is that you can set your "DOS" window's size to anything you want. (Right now I'm using a 120x50 window with 8192 lines of scrollback buffer.) Another one is that you will never lose your console output; in fact, you'll be able to see outputs from past runs. (8192 lines is A LOT unless you're printf-debugging a tight loop.)
An alternative to terminating your program from the taskbar is using the red icons on the top right corner of the Eclipse "Console" window.
Is your program's entrypoint main() or WinMain()? There may also be a setting/link option for the "subsystem," console or Windows I think they're called in Visual Studio.
Anyway, if your program is starting up via WinMain, the expectation is that you'll have a Windows form of some sort through which the user can control the program. If you use main(), then your program should automatically trigger the opening of a console window.
Look up AllocConsole() on MSDN (or google it) if you are using WinMain() intentionally and want a console window to also open up. There's some trickery also available via google search that can hook stdout to this console, but at this time I don't remember what it is. (You basically redirect the stdout handle to your new console.)
Good luck.

Holding cmd.exe open on Vista

I'm writing C++ console programs. After compilation, when I run the program from my file browser, cmd.exe automatically closes such that I can't see my programs output.
The only way to work around this I've found is to run the program from inside cmd.exe
Is there anyway to keep cmd.exe open after a program finishes running?
Is there a setting I can change somewhere? I don't want to run a batch script with cmd.exe /K
Thanks!
[Edit] Don't know if this matters, but I'm on Vista x64
You can setup a shortcut with the /K switch when launching cmd.exe to have it not terminate after running a given command:
cmd.exe /K YourProgram.exe
Have your application ask for a keypress before exiting - that's the easiest fix!
I've always been a fan of just creating a batch file that calls you're program and then calls pause
Prog.exe
Pause
This will give a nice "Press any key to continue..." prompt, it's simple and doesn't require modification of program.
As the last line of your main() function, you can add this line:
system("PAUSE");
Also, make sure to #include <stdlib.h> to declare the system() function. This will make the console pause. However, if your program is run from inside cmd.exe, this will still pause, which may be undesirable.
I know you asked for how to do it via a file browsers, but in case other people are interested in the same problem but through visual studio:
It's best to set a breakpoint right before your program ends.
Then you can deploy your exe and you can be sure that you won't forget to remove the asking for input. It's also better then asking for input because it takes a lot of time to comment out and back in the asking for input.
I think it is best not to ask for input and to instead start your program from a launched command prompt.