简体   繁体   English

从C ++获取CMD行程序的输出(特别是netstat)

[英]Get output of CMD line program from C++ (specifically netstat)

I want to be able to run "netstat -n" and grab the output somehow so I can then write it out to another file. 我希望能够运行“ netstat -n”并以某种方式获取输出,以便随后将其写到另一个文件中。

How can I do this in C++ on Windows CE 如何在Windows CE上的C ++中执行此操作

Thankyou 谢谢

Chris 克里斯

You must call CreateProcess and override the process's output handle: 您必须调用CreateProcess并覆盖进程的输出句柄:

STARTUPINFO aInfo;
...
aINfo.hStdOutput = myHandle;
CreateProcess(..., &aInfo, ...);

http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx

But not pretty sure it'll work under CE 但不太确定它是否可以在CE下运行

I solved this by essentially calling netstat from the cmd prompt, piping the output to a file, and then using it from there. 我通过从cmd提示符本质上调用netstat,将输出传递到文件中,然后从那里使用它来解决此问题。 I believe Kerido's answer to be right but this is how I got it working. 我相信Kerido的答案是正确的,但这就是我如何使它起作用的方式。

This code then launches cmd.exe and telling it to run netstat -n. 然后,此代码启动cmd.exe,并告诉其运行netstat -n。 Note that the /c is required else cmd.exe will not launch the code 请注意,/ c是必需的,否则cmd.exe将不会启动代码

int retVal = CreateProcessW(L"cmd.exe", L"/c netstat -n > \"/netstatoutput.txt\"", NULL, NULL, NULL, CREATE_NEW_CONSOLE, NULL, NULL, NULL, NULL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM