I have an application with a manifest that requires it to run as an administrator, but part of the application is to map the drive using WNetAddConnection2, which, in my opinion, requires it to run in the normal user context due to credentials, etc. . Is there a way to execute this bit of code in a normal user context without creating a separate process.
EDIT
From the comments I got this far, but it does not work. I expected this to not be the case, as I really do not understand how I should use it. Perhaps it is better if I open a new question?
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(); } }
EDIT
If the current user has administrator privileges, then the main process is promoted with a manifest, in the promoted code I want to run the command in a non-expert user space, since it has different environment variables, etc. I believe that when a thread is started, it cannot change itself, it needs to start a new one.
Charles gargent
source share