問題描述
我在這里遇到了一個嚴(yán)重的問題.我需要通過 C++ 執(zhí)行 CMD 命令行而不顯示控制臺窗口.因此我不能使用 system(cmd)
,因為窗口會顯示.
I'm having a serious problem here. I need to execute a CMD command line via C++ without the console window displaying. Therefore I cannot use system(cmd)
, since the window will display.
我已經(jīng)嘗試過 winExec(cmd, SW_HIDE)
,但這也不起作用.CreateProcess
是我嘗試過的另一個.但是,這是用于運行程序或批處理文件.
I have tried winExec(cmd, SW_HIDE)
, but this does not work either. CreateProcess
is another one I tried. However, this is for running programs or batch files.
我最終嘗試了 ShellExecute
:
ShellExecute( NULL, "open",
"cmd.exe",
"ipconfig > myfile.txt",
"c:projects",
SW_SHOWNORMAL
);
有人能看出上面的代碼有什么問題嗎?我一直在使用 SW_SHOWNORMAL
直到我知道它有效.
Can anyone see anything wrong with the above code? I have used SW_SHOWNORMAL
until I know this works.
我真的需要一些幫助.什么都沒有發(fā)現(xiàn),我已經(jīng)嘗試了很長一段時間.任何人都可以提供的任何建議都會很棒:)
I really need some help with this. Nothing has come to light, and I have been trying for quite a while. Any advice anyone could give would be great :)
推薦答案
將輸出重定向到您自己的管道是一個更簡潔的解決方案,因為它避免了創(chuàng)建輸出文件,但這工作正常:
Redirecting the output to your own pipe is a tidier solution because it avoids creating the output file, but this works fine:
ShellExecute(0, "open", "cmd.exe", "/C ipconfig > out.txt", 0, SW_HIDE);
您沒有看到 cmd 窗口,并且輸出按預(yù)期重定向.
You don't see the cmd window and the output is redirected as expected.
您的代碼可能失敗了(除了 /C
之外),因為您將路徑指定為 "c:projects"
而不是 "c:\projects\b"
.
Your code is probably failing (apart from the /C
thing) because you specify the path as "c:projects"
rather than "c:\projects\b"
.
這篇關(guān)于C++ 執(zhí)行 CMD 命令的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!