简体   繁体   English

如何在CPP中使用“ GetExitCodeProcess”,“ CreateProcess”和“ WaitForSingleObject”函数?

[英]How to use “GetExitCodeProcess”, “CreateProcess” and “WaitForSingleObject” function with CPP?

I saw this three connected function in the "Windows.h" lib, and I wanted to know how do I exactly use them and why should I use them? 我在“ Windows.h”库中看到了这三个连接的函数,我想知道如何正确使用它们以及为什么要使用它们? I tried to search for information in google or in the MSDN documentation, but I found nothing! 我试图在Google或MSDN文档中搜索信息,但没有发现任何信息!

If someone could please explain to me how should I (and why should I...) use these three connected functions, it will be great for me, and for the other 1000 people who also stand in front of the same problem and will visit here to solve it. 如果有人可以向我解释我该如何(以及为什么要...)使用这三个连接的功能,这对我和其他也遇到相同问题并会访问的1000人将非常有用在这里解决。

So, how in a simple sample of code should I use every function? 那么,如何在一个简单的代码示例中使用每个函数呢?

Possibly the question is too broad, but since the technical details are covered by the documentation , readers may benefit from a higher level view, that includes C++-specific aspects. 可能这个问题过于笼统,但由于技术细节已包含在文档中 ,因此读者可能会受益于更高层次的观点,包括C ++特定方面。

GetExitCodeProcess . GetExitCodeProcess

GetExitCodeProcess is used to check the exit code of a process. GetExitCodeProcess用于检查进程的退出代码。 By convention in Windows, Unix and in C++, exit code 0 (eg as returned from main ) denotes success. 按照Windows,Unix和C ++中的约定,退出代码0(例如,从main返回)表示成功。 In C++ this value is available as the macro symbol EXIT_SUCCESS from <stdlib.h> . 在C ++中,此值可用作<stdlib.h>的宏符号EXIT_SUCCESS On other systems EXIT_SUCCESS needs not be 0, but in C++ exit code 0 always denotes success, possibly in addition to the value of EXIT_SUCCESS . 在其他系统上, EXIT_SUCCESS不必为0,但在C ++中,退出代码0总是表示成功,可能还包括EXIT_SUCCESS的值。

The Windows and Unix-land convention is that any other exit code denotes failure, but the only portable failure value in C++ is the one denoted by EXIT_FAILURE . Windows和Unix-land约定是任何其他退出代码都表示失败,但是C ++中唯一可移植的失败值是EXIT_FAILURE表示的EXIT_FAILURE In Windows this is conventionally the value 1. Unfortunately that conflicts with the value of a Windows error code, about “incorrect function” (as reported by errlook.exe ). 在Windows中,它通常是值1。不幸的是,它与Windows错误代码(例如errlook.exe报告)有关的错误值冲突。 For this reason one may choose to use Windows' own general failure value E_FAIL to indicate a general failure, instead of using the portable but imperfect EXIT_FAILURE . 因此,可以选择使用Windows自己的常规故障值E_FAIL来指示常规故障,而不是使用可移植但不完善的EXIT_FAILURE

Two relevant differences between Windows and Unix-land: Windows和Unix-land之间的两个相关区别:

  • In Windows a non-zero exit code is often an error code that tells you something about the cause of failure. 在Windows中,非零退出代码通常是一个错误代码,它告诉您一些有关失败原因的信息。 It can be passed to a tool such as Microsoft's errlook to get a textual description, the same as via the API function FormatMessage . 可以将其传递给诸如Microsoft errlook类的工具以获取文本描述,就像通过API函数FormatMessage In Unix-land process exit codes used as failure cause indications are much less common. 在Unix-land进程中,用作失败原因指示的退出代码很少见。

  • In Unix-land tools can generally be relied on to indicate failure or success via the exit code. 在Unix-land工具中,通常可以依靠退出代码来指示失败或成功。 This is not so in Windows. 在Windows中不是这样。 Even many of the built-in and standard commands fail to do so. 甚至许多内置命令和标准命令都无法执行此操作。

Especially the latter point is important for use of GetExitCodeProcess . 特别是后一点对于使用GetExitCodeProcess非常重要。 You need to know that the process you're checking, is one that produces a meaningful exit-code. 您需要知道所检查的过程是产生有意义的退出代码的过程。

Also note well that the code STILL_ACTIVE is defined as a low number, as I recall around 270 or thereabouts, and can be produced by a process, so that it is not a reliable indicator of whether a process is still active or not. 还要注意,代码STILL_ACTIVE定义为一个低数字,正如我记得在270左右,它可以由一个进程生成,因此它不是一个可靠的指示符,它表明某个进程是否仍然处于活动状态。 Ie, don't use GetExitCodeProcess to poll for a process to finish. 即,不要使用GetExitCodeProcess来轮询要完成的进程。 Depending on the process & circumstances, such code might hang… 根据过程和情况,此类代码可能会挂起……

WaitForSingleObject . WaitForSingleObject

Instead of waiting for a process to end by polling its exit code with GetExitCodeProcess , which might hang as explained above, use eg WaitForSingleObject , or a family member. 与其等待通过用GetExitCodeProcess轮询退出代码来等待进程结束,该退出代码可能如上所述挂起,请使用例如WaitForSingleObject或家庭成员。

CreateProcess . CreateProcess

CreateProcess used to be the fundamental way to create a new process, used internally by higher level functions such as ShellExecute . CreateProcess曾经是创建新流程的基本方式,供更高层次的函数(例如ShellExecute内部使用。

With the advent of User Access Control security, UAC, one needs to use ShellExecute (or family member) to run a process with elevated access. 随着用户访问控制安全(UAC)的出现,人们需要使用ShellExecute (或家族成员)来运行具有更高访问权限的进程。

Where the process to be started is a console subsystem process, it can often be more convenient to use the C++ standard library's system function, which uses the cmd.exe command interpreter to run a specified command, waits for that to end, and returns the process exit code. 如果要启动的过程是控制台子系统进程,则使用C ++标准库的system函数通常更方便,该函数使用cmd.exe命令解释器运行指定的命令,等待该命令结束并返回进程退出代码。

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

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