AssignProcessToJobObject 错误码5 的解决办法

在windows 调试中可以正常在job中关联 子进程,并且在主进程异常退出时,子进程同时退出,子进程的创建 使用 CreateProcess方法;

!!!!!!但是 !!!!!!!

问题:
在双击执行exe(编译产生的执行程序)时,却出现报错:AssignProcessToJobObject FAIL,errCode:5

解决办法:
修改CreateProcess中的 dwCreationFlags 参数

 BOOL CreateProcessA(
  LPCSTR                lpApplicationName,
  LPSTR                 lpCommandLine,
  LPSECURITY_ATTRIBUTES lpProcessAttributes,
  LPSECURITY_ATTRIBUTES lpThreadAttributes,
  BOOL                  bInheritHandles,
  DWORD                 dwCreationFlags,
  LPVOID                lpEnvironment,
  LPCSTR                lpCurrentDirectory,
  LPSTARTUPINFOA        lpStartupInfo,
  LPPROCESS_INFORMATION lpProcessInformation
);

如下:

if (CreateProcess(_T(“子进程”), _T(""), NULL, NULL, FALSE, CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &info, &processInfo))

原因:

A process can be associated only with a single job. A process inherits limits from the job it is associated with and adds its accounting information to the job. If a process is associated with a job, all processes it creates are associated with that job by default. To create a process that is not part of the same job, call the CreateProcess function with the CREATE_BREAKAWAY_FROM_JOB flag.

MSDN :
https://docs.microsoft.com/zh-cn/windows/win32/procthread/process-creation-flags

你可能感兴趣的:(C/C++,windows)