catch another process unhandled exception

自闭症网瘾萝莉.ら 提交于 2019-11-27 08:04:17

If you are calling to a .Net executable assembly you can load it and (at your own risk :D ) call to the Main method of the Program class into a try_catch statement:

Assembly assembly = Assembly.LoadFrom("ErroneusApp.exe");
Type[] types= assembly.GetTypes();
foreach (Type t in types)
{
 MethodInfo method = t.GetMethod("Main",
     BindingFlags.Static | BindingFlags.NonPublic);
 if (method != null)
 {
    try
    {
        method.Invoke(null, null);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    break;
 }
}

But be aware of the security risks you are introducing doing that.

jmservera

You can try something like that to avoid the debugger question to appear, you won't get the exception but only the exit code:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            ProcessStartInfo info = 
                 new ProcessStartInfo("ErroneusApp.exe");
            info.ErrorDialog = false;
            info.RedirectStandardError = true;
            info.RedirectStandardOutput = true;
            info.CreateNoWindow = true;
            info.UseShellExecute = false;

            System.Diagnostics.Process p = 
                System.Diagnostics.Process.Start(info);
            p.EnableRaisingEvents = true;
            p.Exited += p_Exited;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.ReadLine();
    }


    static void p_Exited(object sender, EventArgs e)
    {
        Process p = sender as Process;
        if (p != null)
        {
            Console.WriteLine("Exited with code:{0} ", p.ExitCode);
        }
        else
            Console.WriteLine("exited");
    }

}

In this question they provided another workaround for that, but changing some registry values.

No. If the controlled app utilizes standardError and return codes you may be notified of the occurance of an error or exception but you cannot trap it in any way.

Instead of doing the whole sneaking around the assembly to try to find the Main method like in jmservera's answer, you can simply execute the assembly in a new domain. See this msdn article

In resolving to the previous target invocation issue, finally i end up with the below method.

Some of your application would have dependencies in the current directory, which leads to exception whenever it is executed in the another directory since the dependencies doesnt match.

Assembly assembly = Assembly.LoadFrom(file);
                    Directory.SetCurrentDirectory(Path.GetDirectoryName(file));
                    Type[] types = assembly.GetTypes();
                    foreach (Type t in types)
                    {
                        MethodInfo method = t.GetMethod("Main", BindingFlags.Static | BindingFlags.NonPublic);
                        if (method != null)
                        {
                            try
                            {
                              method.Invoke(null, null);
                            }

Directory.SetCurrentDirectory(Path.GetDirectoryName(file)); //by refering to the current directory target invocation exception would be resolved.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!