title: How to copy privileges from one primary user token to another?
body: <p>My goal is to copy <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/bb530716(v=vs.85).aspx" rel="nofollow">privileges</a> from one <em>primary user token</em> to another before I start a user mode process with the destination token. I created a sample pseudo code to illustrate what I need to accomplish.</p>
  
  <p>The following will be run from a local system service:</p>
  
  <pre><code>//dwSessionId = user session ID to run process in
  
  HANDLE hToken1 = NULL;      //Source user token
  WTSQueryUserToken(dwSessionId, &amp;hToken1);
  
  HANDLE hSelfToken = NULL;   //User token for system service
  HANDLE hToken2 = NULL;      //Adjusted self-token
  
  OpenProcessToken(::GetCurrentProcess(), TOKEN_ALL_ACCESS, &amp;hSelfToken);
  DuplicateTokenEx(hSelfToken, TOKEN_ASSIGN_PRIMARY | TOKEN_ALL_ACCESS, 
      NULL, SecurityIdentification, TokenPrimary, &amp;hToken2);
  
  //Specify user session to run in
  SetTokenInformation(hToken2, TokenSessionId, &amp;dwSessionId, sizeof(dwSessionId));
  
  //Now I need to set privileges in 'hToken2' as they are in 'hToken1'
  ...
  
  //Then use 'hToken2' in CreateProcessAsUser() to start a process
  </code></pre>
  
  <p>Any idea how to copy privileges from <code>hToken1</code> to <code>hToken2</code>?</p>