简体   繁体   English

C ++ - 使用system()进行Windows命令

[英]C++ - Using system() for Windows commands

I'm new to C++, and I've read and heard that using system calls is bad practice. 我是C ++的新手,我读过并听说使用系统调用是不好的做法。 So what if you need to use system() to run a Windows command like ipconfig or netstat . 那么如果你需要使用system()来运行像ipconfignetstat这样的Windows命令呢? Is it still evil or is that considered an acceptable use of system()? 它仍然是邪恶的还是被认为是system()的可接受用途? I'm writing a program to collect a variety of information about the system and I use system() many times and pipe the output into text files for review. 我正在编写一个程序来收集有关系统的各种信息,我多次使用system()并将输出传输到文本文件中以供查看。

Most of the calls you'd make have Windows API calls you can use directly. 您进行的大多数调用都有可以直接使用的Windows API调用。 Ideally, you'd just call the appropriate Windows API call instead of launching a process, writing to a file, then parsing the file. 理想情况下,您只需调用适当的Windows API调用,而不是启动进程,写入文件,然后解析文件。

That being said, on Windows, CreateProcess will launch a process and provide you a lot more control than a call to system . 话虽如此,在Windows上, CreateProcess将启动一个进程并为您提供比调用system更多的控制权。 This includes using STARTUPINFO to provide your own HANDLE for the process standard output stream, which means you can directly read from the process without writing to a file. 这包括使用STARTUPINFO为流程标准输出流提供您自己的HANDLE ,这意味着您可以直接从流程读取而无需写入文件。

The problem is not the use of system , but the abuse of it in wrong frequent situations. 问题不在于system的使用,而是在错误的频繁情况下滥用它。

  • Calling system means invoke the command interpreter (CMD) giving it a string to parse. 调用系统意味着调用命令解释器(CMD)为其提供要解析的字符串。 If that string is a command will be executed, if it is a program will be called because the command interpreter behave that way. 如果该字符串是一个命令将被执行,如果它是一个程序将被调用,因为命令解释器的行为方式。

  • Calling CreateProcess means just letting the kernel to boot a process that will execute a given program. 调用CreateProcess意味着只让内核启动一个执行给定程序的进程。 There is no command interpreter invocation. 没有命令解释器调用。

Now the point becomes "does the command interpreter have a role in your execution , or will it be just a passive slave? 现在重点是“命令解释器在你的执行中有作用,还是只是一个被动的奴隶?

In the first case (more rare) system is most likely the real way to go. 在第一种情况下(更罕见) system很可能是真正的方法。

In the second case (more frequent), placing a command interpreter in between makes the real execution dependent on the eventual tweaks the system has placed around it (for example, launching a batch instead of your command) you may not be aware of when running on your user's machines. 在第二种情况下(更频繁),在它们之间放置一个命令解释器使得实际执行依赖于系统围绕它进行的最终调整(例如,启动批处理而不是命令),在运行时可能不会注意到在用户的计算机上。 And this can even translate in potential security breaches.) 这甚至可以转化为潜在的安全漏洞。)

The most common use of system, is -however- the system("pause") frequently seen to keep the console open after a program termination. 最常见的系统使用方式是 - 在程序终止后经常看到system("pause")使控制台保持打开状态。 It can be a quick and dirty solution, but that's not in general the way to go, for two reasons: 它可以是一个快速而肮脏的解决方案,但这通常不是一种可行的方法,原因有两个:

  • If a program is executed inside another process there can be no-one waiting to press a key to return: just launch the program in an already open shell, and the shell will remain open. 如果程序在另一个进程内执行,则没有人等待按键返回:只需在已打开的shell中启动程序,shell将保持打开状态。

  • If a program is designed to be interactive (so there is a human providing input), then usign normal i/o functionality will be more appropriate. 如果程序设计为交互式(因此有人提供输入),那么使用正常的i / o功能将更合适。

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

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