How to call CMD without opening a window

2019-02-11 22:13发布

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?

5条回答
forever°为你锁心
2楼-- · 2019-02-11 22:55

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楼-- · 2019-02-11 22:55

Try ShellExecuteEx, setting nShow=SW_HIDE.

查看更多
霸刀☆藐视天下
4楼-- · 2019-02-11 23:02

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)

查看更多
beautiful°
5楼-- · 2019-02-11 23:07

Just call it without start:

example.exe arg1 arg2 arg3
查看更多
女痞
6楼-- · 2019-02-11 23:14

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
查看更多
登录 后发表回答