public static int RunWithElevatedContext(DoWithContext whatToCall, bool hideWindow) { progress("rwec: started"); System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingCallback), "RemotingCallback", System.Runtime.Remoting.WellKnownObjectMode.Singleton); RemotingCallback.Callback = whatToCall; // Prepare channel string channelName = "XSharper" + Guid.NewGuid(); registerChannel(channelName, channelName); // Prepare runas command line System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.UseShellExecute = true; startInfo.WorkingDirectory = Environment.CurrentDirectory; startInfo.FileName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; string arg = string.Empty; if (AppDomain.CurrentDomain.SetupInformation.ConfigurationFile != null) { arg += ("//config "); arg += ("\"" + AppDomain.CurrentDomain.SetupInformation.ConfigurationFile + "\" "); } arg += XsCreateElevatedScriptContext + " " + channelName + " \"" + BaseDirectory.TrimEnd('\\', '/') + "\" " + System.Diagnostics.Process.GetCurrentProcess().Id + " *** EXECUTED FROM *** " + Environment.CommandLine; progress("rwec: arguments: [" + arg + "]"); startInfo.Arguments = arg; startInfo.Verb = "runas"; startInfo.WindowStyle = hideWindow ? System.Diagnostics.ProcessWindowStyle.Hidden : System.Diagnostics.ProcessWindowStyle.Normal; // run try { progress("rwec: Starting " + startInfo.FileName + " " + startInfo.Arguments); System.Diagnostics.Process p = System.Diagnostics.Process.Start(startInfo); if (p != null) { progress("rwec: started"); p.WaitForExit(); progress("rwec: terminated with exit code " + p.ExitCode); return(p.ExitCode); } progress("rwec: failed to start"); return(-1); } catch (Exception e) { progress("rwec: " + e); return(-2); } }
public static int RunWithElevatedContext(DoWithContext whatToCall, bool hideWindow) { progress("rwec: started"); System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingCallback), "RemotingCallback", System.Runtime.Remoting.WellKnownObjectMode.Singleton); RemotingCallback.Callback = whatToCall; // Prepare channel string channelName = "XSharper" + Guid.NewGuid(); registerChannel(channelName, channelName); // Prepare runas command line System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.UseShellExecute = true; startInfo.WorkingDirectory = Environment.CurrentDirectory; startInfo.FileName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; string arg = string.Empty; if (AppDomain.CurrentDomain.SetupInformation.ConfigurationFile != null) { arg += ("//config "); arg += ("\"" + AppDomain.CurrentDomain.SetupInformation.ConfigurationFile + "\" "); } arg += XsCreateElevatedScriptContext + " " + channelName + " \"" + BaseDirectory.TrimEnd('\\', '/') + "\" " + System.Diagnostics.Process.GetCurrentProcess().Id + " *** EXECUTED FROM *** " + Environment.CommandLine; progress("rwec: arguments: [" + arg + "]"); startInfo.Arguments = arg; startInfo.Verb = "runas"; startInfo.WindowStyle = hideWindow ? System.Diagnostics.ProcessWindowStyle.Hidden : System.Diagnostics.ProcessWindowStyle.Normal; // run try { progress("rwec: Starting " + startInfo.FileName + " " + startInfo.Arguments); System.Diagnostics.Process p = System.Diagnostics.Process.Start(startInfo); if (p != null) { progress("rwec: started"); p.WaitForExit(); progress("rwec: terminated with exit code " + p.ExitCode); return p.ExitCode; } progress("rwec: failed to start"); return -1; } catch (Exception e) { progress("rwec: " + e); return -2; } }