C ++执行DOS命令 [英] C++ Executing DOS Commands
问题描述
这里有严重的问题。需要执行正常的DOS命令(在Windows中),而CMD窗口不显示在C ++中。因此我不能使用 system(cmd);
(窗口会显示)
Serious Problem im having here. Need to execute normal DOS Commands (in windows) without the CMD window displaying in C++. Therefore i can not use system(cmd);
(As the window will display)
c $ c> winExec(cmd,SW_HIDE)这也不工作。 CreateProccess
是另一个我尝试,但是这是运行程序或批处理文件。
I have tried winExec(cmd, SW_HIDE)
this does not work either. CreateProccess
is another one I tried however this is for running programs or batch files.
code> ShellExecute()
I have resulted in trying ShellExecute()
ShellExecute( NULL, "open",
"cmd.exe",
"ipconfig > myfile.txt",
"c:\projects\b",
SW_SHOWNORMAL
);
任何人都可以看到上面的代码有什么不对吗?我已经使用 SW_SHOWNORMAL
,直到我知道这个工作。
Can anyone see anything wrong with the above code? I have used SW_SHOWNORMAL
until I know this works.
我真的需要一些帮助。没有什么是光明的,我一直在尝试了一段时间。任何人都可以给出的任何建议都是巨大的:)
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 :)
推荐答案
将输出重定向到您自己的管道是一个更整洁的解决方案,输出文件,但是工作正常:
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窗口,并且输出按预期重定向。
You don't see the cmd window and the output is redirected as expected.
你的代码可能失败了(除了 / C
),因为你指定路径为c :\projects\b
而不是c:\\projects\\b
。
Your code is probably failing (apart from the /C
thing) because you specify the path as "c:\projects\b"
rather than "c:\\projects\\b"
.
这篇关于C ++执行DOS命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!