title: How can I assign the current process to a newly created job object?
body: <p>I can't seem to use the <a href="http://msdn.microsoft.com/en-us/library/ms681949(VS.85).aspx" rel="nofollow">AssignProcessToJobObject</a> function to assign the current process to a job object handle given by <a href="http://msdn.microsoft.com/en-us/library/ms682409(VS.85).aspx" rel="nofollow">CreateJobObject</a>. 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.</p>
  
  <p>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):</p>
  
  <p>My sample application (<code>main.cpp</code>):</p>
  
  <pre><code>#include &lt;windows.h&gt;
  
  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) &amp;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;
  }
  </code></pre>
  
  <p>The UAC manifest (<code>main.exe.manifest</code>):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"&gt;
     &lt;ms_asmv3:trustInfo xmlns:ms_asmv3="urn:schemas-microsoft-com:asm.v3"&gt;
        &lt;ms_asmv3:security&gt;
           &lt;ms_asmv3:requestedPrivileges&gt;
              &lt;ms_asmv3:requestedExecutionLevel level="requireAdministrator"/&gt;
           &lt;/ms_asmv3:requestedPrivileges&gt;
        &lt;/ms_asmv3:security&gt;
     &lt;/ms_asmv3:trustInfo&gt;
  &lt;/assembly&gt;
  </code></pre>
  
  <p>I build this sample by running</p>
  
  <pre><code>cl main.cpp
  mt -manifest main.exe.manifest -outputresource:main.exe;1
  </code></pre>
  
  <p>Unfortunately, running my <code>main.exe</code> sample after these steps still yields an 'Access denied' error in debug output when attempting the <code>AssignProcessToJobObject</code> call. Does anybody know why that is?</p>