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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章