Exemplo n.º 1
0
 public static void ProtectCurrentProcessFromTerminate()
 {
     // denies the current user terminate permissions on the current process
     using (var process = Win32.OpenProcess(Win32.READ_CONTROL | Win32.WRITE_DAC, false, Win32.GetCurrentProcessId()))
     {
         if (process.IsInvalid)
         {
             throw new Win32Exception();
         }
         var security = new ProcessSecurity(process);
         using (var identity = WindowsIdentity.GetCurrent())
             security.AddAccessRule(new ProcessAccessRule(identity.User, Win32.PROCESS_TERMINATE, false, InheritanceFlags.None, PropagationFlags.None, AccessControlType.Deny));
         security.WriteLock();
         try { security.Persist(process, AccessControlSections.Access); }
         finally { security.WriteUnlock(); }
     }
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            try
            {
                // enable styles, faster text rendering and protect the current process
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                ProcessSecurity.ProtectCurrentProcessFromTerminate();

                // parse the args
                var showSecurityDialog = false;
                var overrideConfig     = (string)null;
                foreach (var arg in args)
                {
                    if (arg.Equals(ParamShowSecurityDialog, StringComparison.OrdinalIgnoreCase))
                    {
                        if (!showSecurityDialog)
                        {
                            showSecurityDialog = true;
                            continue;
                        }
                    }
                    else if (arg.StartsWith(ParamOverrideConfig, StringComparison.OrdinalIgnoreCase))
                    {
                        if (overrideConfig == null)
                        {
                            overrideConfig = arg.Substring(ParamOverrideConfig.Length);
                            continue;
                        }
                    }
                    MessageBox.Show(string.Format("{0} [{1}] [{2}<path>]\n\n\n\n{1}\t\tDisplays the security dialog.\n\n{2}\tUse an alternative app config file.", Environment.GetCommandLineArgs()[0], ParamShowSecurityDialog, ParamOverrideConfig), "Usage", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                // set the config file and show the security dialog if requested
                if (overrideConfig != null)
                {
                    ((AppDomainSetup)typeof(AppDomain).GetProperty("FusionStore", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(AppDomain.CurrentDomain, null)).ConfigurationFile = overrideConfig;
                }
                configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                if (showSecurityDialog)
                {
                    Security.ShowEditDialog();
                    return;
                }

                // create the rdp session
                var session = new RDPSessionClass();
                session.OnError += OnSessionError;
                session.Open();
                try
                {
                    // create the web service host and run the app
                    var host = new WebServiceHost(new Service(session));
                    host.Open();
                    try { Application.Run(); }
                    finally { host.Close(); }
                }
                finally { session.Close(); }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, e.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.ExitCode = Marshal.GetHRForException(e);
            }
        }