Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ WIN API: When creating a child process using CreateProcess, do I need to make the input parameters have a global lifetime?

Tags:

c++

winapi

I am really new to both C++ and Windows API. It suddenly comes to mind today whether I need to keep a long lifetime of the input parameters of CreateProcess. According MSDN:

BOOL WINAPI CreateProcess(
  _In_opt_     LPCTSTR lpApplicationName,
  _Inout_opt_  LPTSTR lpCommandLine,
  _In_opt_     LPSECURITY_ATTRIBUTES lpProcessAttributes,
  _In_opt_     LPSECURITY_ATTRIBUTES lpThreadAttributes,
  _In_         BOOL bInheritHandles,
  _In_         DWORD dwCreationFlags,
  _In_opt_     LPVOID lpEnvironment,
  _In_opt_     LPCTSTR lpCurrentDirectory,
  _In_         LPSTARTUPINFO lpStartupInfo,
  _Out_        LPPROCESS_INFORMATION lpProcessInformation
);

for instance, the LPSTARTUPINFO lpStartupInfo is pointer pointing to a STARTUPINFO structure.

I am wondering if I need to keep the structure alive after CreateProcess returns. In addition, in other Windows API functions, there are similar input parameters input as a pointer, I don't know if I need to keep those objects pointed by the input pointers alive after the API functions return.

I don't want to wait by using WaitForSingleObject, or at least not wait immediate after CreateProcess. CreateProcess is wrapped in my another function called 'ExecuteProcess'. After CreateProcess returns, ExecuteProcess also returns and all the input parameters defined on the stack will be destroyed when ExecuteProcess returns.

An example from MSDN Creating a Child Process with Redirected Input and Output seems to use CreateProcess in a similar way to me. CreateProcess is called in a function CreateChildProcess and all the input parameters are defined on the stack of 'CreateChildProcess' and WaitForSingleObject is not used either.

like image 670
user3692418 Avatar asked Feb 13 '23 18:02

user3692418


2 Answers

No. The process arguments are copied into the new process's address space. You can get rid of them whenever you like.

like image 136
user207421 Avatar answered Feb 15 '23 09:02

user207421


As a general rule Win32 API functions will not take a reference to the variables that you pass as parameters. They might make copies, but will not take references. The reason being that were the function to take a reference to your variable and some how use it after the function returns, that imposes a very severe constraint on the caller. There may be exceptions to this general rule and if there are they will be clearly indicated in the documentation of the function.

What this means, in the context of your question, is that once CreateProcess returns, you are under no obligation to keep alive any of the variables that you passed to CreateProcess. The only obligation you have is that, if CreateProcess succeeds, you are now the owner of two handles, a process handle and a thread handle. You must, at some point, close those handles to avoid leaking them.

I've tried to answer this in a general way so that you can apply the same rule and reasoning to any other calls you make to Win32.

like image 34
David Heffernan Avatar answered Feb 15 '23 10:02

David Heffernan