我有一个 Windows 服务(在 WinXP SP2 下),在 LocalSystem 帐户下运行,它使用 CreateProcessWithLogonW 启动进程。为了清理子进程,我尝试使用作业对象和 TerminateJobObject。
MSDN 声明作业句柄必须具有 JOB_OBJECT_ASSIGN_PROCESS 访问权限,因为它是通过 CreateJobObject 创建的。进程句柄必须具有 PROCESS_SET_QUOTA 和 PROCESS_TERMINATE 权限。我认为它有它们,因为 TerminateProcess 和 SetProcessWorkingSetSize 都没有错误地返回。
但是,AssignProcessToJobObject 失败并显示错误号 5(访问被拒绝)。如果我将 CreateProcessWithLogonW 替换为简单的 CreateProcess,则一切正常。
我是不是遗漏了什么或者我想做的事情是不可能的?
编辑:似乎在使用 CreateProcessWithLogonW 时实际创建进程的 svchost.exe 似乎已经将进程分配给匿名作业。此函数忽略 CREATE_CREAKAWAY_FROM_JOB 标志。所以真正的问题是:有没有办法阻止 svnhost 将进程分配给作业?
最佳答案
来自 Jeff Lawson在 MSDN 上:
Interactions with Win32 Job Objects
CreateProcessWithLogonW executes the new process as a child of the Secondary Logon service, which has the outcome of making the process escape any Job Object membership/restrictions even if the Job Object did not allow breakaway.
Furthermore, the Secondary Logon service automatically creates its own new Job Object and assigns the new process into it. As such, it is not possible for the caller to explicitly assign the new process to any other Job Object (since a process may only be assigned to one Job Object, and can never be removed from a Job Object once it has been assigned to one).
是否每个新进程都需要不同的登录?否则,您可以使用新登录创建一个进程,并使用 CreateProcess 生成新进程,然后将其与作业对象相关联。
关于CreateProcessWithLogonW 和 AssignProcessToJobObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1287620/
我有以下代码:#include#include#include#pragmacomment(lib,"shlwapi.lib")int__cdeclwmain(intargc,PWSTRargv[]){HANDLEJob(CreateJobObject(NULL,NULL));if(!Job){wprintf(L"Couldnotcreatejobobject,error%d\n",GetLastError());return0;}HANDLEIOPort(CreateIoCompletionPort(INVALID_HANDLE_VALUE,NULL,0,1));if(!IOPort
我有一个Windows服务(在WinXPSP2下),在LocalSystem帐户下运行,它使用CreateProcessWithLogonW启动进程。为了清理子进程,我尝试使用作业对象和TerminateJobObject。MSDN声明作业句柄必须具有JOB_OBJECT_ASSIGN_PROCESS访问权限,因为它是通过CreateJobObject创建的。进程句柄必须具有PROCESS_SET_QUOTA和PROCESS_TERMINATE权限。我认为它有它们,因为TerminateProcess和SetProcessWorkingSetSize都没有错误地返回。但是,Assign