gpt4 book ai didi

C++ WIN API : When creating a child process using CreateProcess, 是否需要使输入参数具有全局生命周期?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:23:21 24 4
gpt4 key购买 nike

我对 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com