title: Execute code in another users context
body: <p>I have an app with a manifest that requires running as administrator, but part of the app is to map a drive using WNetAddConnection2 which I believe requires it to be run in the normal user context due to credentials etc. Is there a way to execute this bit of code in the normal user context without creating a separate process.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>From the comments I have got this far but it doesnt work. I expected it not to as I dont really understand quite how I should use this. Perhaps it best if I open a new question?</p>
  
  <pre><code>class Program
  {
      [DllImport("advapi32.DLL")]
      public static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
      [DllImport("advapi32.DLL")]
      public static extern bool RevertToSelf();
  
      static void Main(string[] args)
      {
          IntPtr phToken = IntPtr.Zero;
          ImpersonateLoggedOnUser(phToken);
          MapDrives();
          RevertToSelf();
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>If the current user has admin privileges then the main process is elevated with the manifest, in the code which is elevated I want to run a command in the users non-elevated space as this appears to have different environment variables etc. I believe once a thread is started it cant change itself, it needs to run a new one.</p>