How can I assign the current process to a newly created job object?
I can't seem to use the AssignProcessToJobObject function to assign the current process to a job object handle given by CreateJobObject. This has been asked a few times already on StackOverflow, but so far none of the solutions (which usually boil down to embedding an UAC manifest) seem to work for me.
I'm using MSVC9 on Windows 7 for this. Here's the source code for my sample application and a small manifest I'm embedding (which supposedly fixes the problem - but not for me):
My sample application (main.cpp
):
#include <windows.h>
static void dumpLastError()
{
LPVOID lpMsgBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
开发者_运维知识库 (LPTSTR) &lpMsgBuf,
0, NULL );
OutputDebugStringA( (LPTSTR)lpMsgBuf );
LocalFree(lpMsgBuf);
}
int main()
{
HANDLE job = CreateJobObjectA( NULL, "demo job 123" );
if ( !job ) {
OutputDebugStringA( "CreateJobObject failed" );
dumpLastError();
return 1;
}
if ( !AssignProcessToJobObject( job, GetCurrentProcess() ) ) {
OutputDebugStringA( "AssignProcessToJobObject failed" );
dumpLastError();
return 1;
}
return 0;
}
The UAC manifest (main.exe.manifest
):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<ms_asmv3:trustInfo xmlns:ms_asmv3="urn:schemas-microsoft-com:asm.v3">
<ms_asmv3:security>
<ms_asmv3:requestedPrivileges>
<ms_asmv3:requestedExecutionLevel level="requireAdministrator"/>
</ms_asmv3:requestedPrivileges>
</ms_asmv3:security>
</ms_asmv3:trustInfo>
</assembly>
I build this sample by running
cl main.cpp
mt -manifest main.exe.manifest -outputresource:main.exe;1
Unfortunately, running my main.exe
sample after these steps still yields an 'Access denied' error in debug output when attempting the AssignProcessToJobObject
call. Does anybody know why that is?
I know this is an old question, but I was recently having the exact same problem. As suggested I was using the command-line workaround until a couple of minutes ago I found this post.
Since I was creating the process, I just followed the instructions on the article adding CREATE_BREAKAWAY_FROM_JOB to the process creation flags:
CreateProcess(szPath, NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi)
with
CreateProcess(szPath, NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS | CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &si, &pi)
I tested and it works as expected, the process gets assigned to the job, no UAC manifest, no command-line.
Hope it helps you or anyone else having this problem.
I followed a number of discussions at one point relating to job objects and UAC manifests. The only piece of information that helped me around the same problem that you're having was that this security feature (introduced in Vista) is apparently not enforced when run from a cmd.
I assume you're launching PyCmd from the start menu. Try launching it from cmd, and I'll bet the problem will go away there too.
What I ended up doing (to run mintty with cygwin) was to make a mintty.bat which said
start mintty.exe
and then a shortcut to mintty.bat which had a property set to run 'minimized' (I forget the exact wording). This allows the shell I wanted to be launched from the start menu and still work as if it had been launched from cmd.exe.
As a side-note, I'd love it if someone would come along and actually explain the problem at a build-level, and how to fix it.
精彩评论