我对 C++ 和 Windows API 都很陌生。今天突然想到我是否需要保持 CreateProcess 的输入参数的生命周期很长。根据 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
);

例如,LPSTARTUPINFO lpStartupInfo 是指向 STARTUPINFO 结构的指针。

我想知道在 CreateProcess 返回后是否需要保持结构处于事件状态。另外,在其他Windows API函数中,也有类似的输入参数作为指针输入,不知道是否需要在API函数返回后保持输入指针指向的那些对象存活。

我不想使用 WaitForSingleObject 等待,或者至少不要在 CreateProcess 之后立即等待。 CreateProcess 包含在我的另一个名为“ExecuteProcess”的函数中。 CreateProcess 返回后,ExecuteProcess 也返回,当 ExecuteProcess 返回时,栈上定义的所有输入参数都会被销毁。

MSDN Creating a Child Process with Redirected Input and Output 中的一个示例似乎以与我类似的方式使用 CreateProcessCreateProcess 在函数 CreateChildProcess 中调用,所有输入参数都定义在“CreateChildProcess”的堆栈中,并且 WaitForSingleObject 也不使用。

最佳答案

否。进程参数被复制到新进程的地址空间中。您可以随时摆脱它们。

关于C++ WIN API : When creating a child process using CreateProcess, 我需要使输入参数具有全局生命周期吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24159619/

10-12 05:28