如何在不打开窗口的情况下调用CMD
我有一个 C++ 方法(使用 Visual Studio,如果有帮助的话),如下所示调用 cmd:
start \B example.exe arg1 arg2 arg3
问题是此调用打开一个 (cmd) 窗口。如果我打开了另一个 cmd,\B 可以工作,但如果没有,它会打开一个新窗口。
我也尝试过不使用 start \B 但它是一样的......
我想避免这个新窗口,但我不知道如何。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我不知道如何在 C++ 中执行此操作,但要打开一个新的命令提示符窗口,该窗口也使用我将使用的批处理文件最小化(或“隐藏”):
命令提示符窗口。
或者如果启动命令提示符,您可以键入:这将重新启动当前的 命令提示符窗口无需关闭它,但所有先前设置的环境变量都会重置。希望这有帮助!
(PS.此方法在Windows 7 Ultimate操作系统上测试成功)
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:
Or if you start the command prompt you can type:
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)
您可以使用
/C /Q
开关,这将运行
dir/b
命令并退出,因为我们正在设置ECHO off
使用/q
,但如果您想在关闭之前查看输出,则不要使用/q
开关作为上面的两个示例执行起来太快,因此请尝试使用此示例
,因为
dir /b /s
将运行每个子目录,您可以看到它正在工作。如果您想在没有窗口的情况下运行命令,但仍想获取输出,则可以通过管道传递
clip
命令。这会将命令的输出复制到剪贴板中,如果剪贴板未被覆盖,您可以将其粘贴到其他地方。
有关
cmd.exe
的更多信息,只需输入cmd / ?
在提示中获取以下内容:You can use the
/C /Q
switchthis will run the
dir/b
command and exit no window will be shown since we are settng theECHO off
with the/q
, but if you want to see the output before it closes then don't use the/q
switch asThe above two examples will be too quick to execute so please try with this
Since the
dir /b /s
will run through each sub-directory you can see it working.In case you want to run the command with no window still want to get the output then pipe the
clip
command with it.This will copy the output of the command in the clipboard and that you can paste ti elsewhere, if the clipboard is not overwritten.
For more information on
cmd.exe
just typecmd /?
in the prompt to get the following:使用 WSH 代替 CMD 提示符怎么样?
您可以使用 .Run, 0 隐藏窗口,例如:
How about using WSH instead of a CMD Prompt?
You can use the .Run <command>, 0 to hide the window like:
从 win cmd:
运行另一个没有窗口的 CMD 实例来运行您的程序,并且不会阻止父窗口,因此不会冻结。
不需要
/q
开关(无论如何都不起作用)并且会给出错误:From win cmd:
Runs another instance of CMD without a window to run your program, and doesn't block parent window so it won't freeze.
The
/q
switch isn't needed (won't work anyway) and will give error:尝试 ShellExecuteEx,设置 nShow=SW_HIDE。
Try ShellExecuteEx, setting nShow=SW_HIDE.
您可以使用这个小工具“quiet.exe”,虽然很旧,但仍然适用于 Windows 10。
https://www.joeware.net/freetools/tools/quiet/index.htm
只需调用
quiet.exe“程序命令行”
,无论它是什么,它都会运行隐。You can use this tiny tool "quiet.exe", very old but still works with Windows 10.
https://www.joeware.net/freetools/tools/quiet/index.htm
Simply call
quiet.exe "program command line"
and it will run whatever it is hidden.只需调用它而不开始:
Just call it without start:
要运行 cmd 文件并退出,请使用:
cmd.exe /c call file.cmd ...parameters...
To run a cmd file and exit, use:
cmd.exe /c call file.cmd ...parameters...