Fix for "Launching ProcessMonitor.exe indirectly causes targets to crash"?

Mar 10, 2012 at 5:16 AM
Edited Mar 10, 2012 at 8:54 AM

I think if you let the application "self elevate" to administrator, you would not have this problem.

Try this...

 

// Add this class

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;

namespace ProcessMonitor
{
    public class VistaTools
    {
        public static bool IsAdmin
        {
            get
            {
                WindowsIdentity id = WindowsIdentity.GetCurrent();
                WindowsPrincipal p = new WindowsPrincipal(id);
                return p.IsInRole(WindowsBuiltInRole.Administrator);
            }
        }

        public static bool IsVista
        {
            get
            {
                return HasUserAccountControl;
            }
        }

        public static bool HasUserAccountControl
        {
            get
            {
                System.OperatingSystem osInfo = System.Environment.OSVersion;
                return (osInfo.Version.Major >= 6);
            }
        }

        public static bool RestartElevated(string arguments = "", bool wait = false)
        {
            return StartProgram(null, arguments, true, wait);
        }

        public static bool StartProgram(string fileName = null, string arguments = "", bool asAdmin = false, bool wait = false)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();

            if (fileName == null)
                fileName = Process.GetCurrentProcess().MainModule.FileName;

            startInfo.UseShellExecute = true;            
            startInfo.WorkingDirectory = Environment.CurrentDirectory;
            startInfo.FileName = fileName;
            startInfo.Arguments = arguments;
            if (asAdmin && HasUserAccountControl)
                startInfo.Verb = "runas";
            try
            {
                Process p = Process.Start(startInfo);
                if (wait)
                    p.WaitForExit();
                return true;
            }
            catch (System.ComponentModel.Win32Exception)
            {
                return false;
            }
        }
    }
}

// Add this to the beginning of your Main in ProcessMonitor/Program.cs
if (VistaTools.IsVista && !VistaTools.IsAdmin)
{
    VistaTools.RestartElevated();
    Process.GetCurrentProcess().Kill();
    return;
}

 

 

If it fixes the problem, I would like to contribute it to the project.