How to call CMD without opening a window

2019-02-11 22:28发布

问题:

I've an C++ method (using Visual Studio, if it helps) that calls to cmd like this:

start \B example.exe arg1 arg2 arg3

The problem is that this call opens a (cmd) window. If i have another cmd opened, \B works, but if not, it opens a new window.

I also tried without start \B but it's the same....

I want to avoid this new window, but I don't know how. Any idea?

回答1:

I dont know how to do it in C++ but to open a new command prompt window that is also minimized (or "hidden") using a batch file i would use:

start /min cmd

Or if you start the command prompt you can type:

@echo off
cls && start /b cmd

This restarts the current command prompt window with out closing it, however all previously set environment variables are reset. Hope this helps!

(PS. This was tested and was successful on Windows 7 Ultimate OS)



回答2:

You can use the "/C" switch

cmd.exe /c dir/b 

this will run the "dir/b" command and exit-- no window will be shown, but if you want to set the ECHO off use the "/q" switch

cmd.exe /c/q dir


回答3:

How about using WSH instead of a CMD Prompt?

You can use the .Run , 0 to hide the window like:

Set objShell = WScript.CreateObject("WScript.Shell")
isHidden = 0 'change 0 to 1 to show the CMD prompt
objShell.Run "%comspec% /c myfile.bat", isHidden


回答4:

Try ShellExecuteEx, setting nShow=SW_HIDE.



回答5:

Just call it without start:

example.exe arg1 arg2 arg3