Why when i load my assembly show me:
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has
been thrown by the target of an invocation. ---> System.TypeInitializationExcept
ion: The type initializer for 'T???????????????????' threw an exception. ---> Sy
stem.TypeInitializationException: The type initializer for 'System.Management.Ma
nagementPath' threw an exception. ---> System.OutOfMemoryException: Exception of
type 'System.OutOfMemoryException' was thrown.
at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark
& stackMark)
at System.Threading.Thread.Start(StackCrawlMark& stackMark)
at System.Threading.Thread.Start()
at System.Management.MTAHelper.InitWorkerThread()
at System.Management.MTAHelper.CreateInMTA(Type type)
at System.Management.ManagementPath.CreateWbemPath(String path)
at System.Management.ManagementPath..cctor()
--- End of inner exception stack trace ---
at System.Management.ManagementScope._Clone(ManagementScope scope, Identifier
ChangedEventHandler handler)
at System.Management.ManagementObjectSearcher..ctor(ManagementScope scope, Ob
jectQuery query, EnumerationOptions options)
at System.Management.ManagementObjectSearcher..ctor(String queryString)
at T???????????????????..cctor()
--- End of inner exception stack trace ---
at T???????????????????.get_Is64Bit()
at ????????????????????.????????????????????()
at ????????????????????.????????????????????()
at ????????????????????.Main(String[] args)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments,
Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Objec
t[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at skip.Program.RunInternal(Byte[] encode, String pass)
at skip.Program.Main()
In my example, first i delete my header of my pe format:
[DllImport("kernel32.dll")]
private static extern IntPtr ZeroMemory(IntPtr addr, IntPtr size);
[DllImport("kernel32.dll")]
private static extern IntPtr VirtualProtect(IntPtr lpAddress, IntPtr dwSize, IntPtr flNewProtect, ref IntPtr lpflOldProtect);
[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string lpModuleName);
public static void EraseHeader()
{
IntPtr address = GetModuleHandle(null);
IntPtr dwOld = default(IntPtr);
VirtualProtect(address, (IntPtr)4096, (IntPtr)0x40, ref dwOld);
ZeroMemory(address, (IntPtr)4096);
}
static void Main(string[] args)
{
Console.WriteLine("");
EraseHeader();
while(true)
Console.WriteLine("");
//Console.ReadKey();
}
And then i load my assembly:
Assembly exeAssembly = Assembly.Load(Buffer);
object[] parameters = new object[1];
exeAssembly.EntryPoint.Invoke(null, parameters);
At result show me the previous error:
--- End of inner exception stack trace ---
I want to understand for study purpose why show me this error.
Consider this code:
namespace StackOverflow44227962StackTraces
{
using System;
public class Program
{
static void Main(string[] args)
{
var a = new ClassA();
try
{
a.DoSomething();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
namespace StackOverflow44227962StackTraces
{
using System;
public class ClassA
{
public void DoSomething()
{
try
{
CallClassB();
}
catch (Exception ex)
{
throw new Exception("This is the outermost exception", ex);
}
}
private void CallClassB()
{
var b = new ClassB();
b.DoSomething();
}
}
}
namespace StackOverflow44227962StackTraces
{
using System;
public class ClassB
{
public void DoSomething()
{
var c = new ClassC();
try
{
c.DoSomething(null);
}
catch (ArgumentNullException ex)
{
throw new InvalidOperationException("This is the exception containing the innermost exception", ex);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace StackOverflow44227962StackTraces
{
public class ClassC
{
public void DoSomething(object something)
{
if (something == null)
{
throw new ArgumentNullException(nameof(something), "This is the innermost exception");
}
}
}
}
This throws an ArgumentNullException, which is wrapped in an InvalidOperationException, which in turn is wrapped in an Exception (and I'm not suggesting that this is best practice for when to use those exception types, I'm just using them as examples), and the outermost exception's ToString() method returns the following.
System.Exception: This is the outermost exception ---> System.InvalidOperationException: This is the exception containing the innermost exception ---> System.ArgumentNullException: This is the innermost exception (Parameter 'something') at StackOverflow44227962StackTraces.ClassC.DoSomething(Object something) in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassC.cs:line 15 at StackOverflow44227962StackTraces.ClassB.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassB.cs:line 12 --- End of inner exception stack trace --- at StackOverflow44227962StackTraces.ClassB.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassB.cs:line 16 at StackOverflow44227962StackTraces.ClassA.CallClassB() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassA.cs:line 23 at StackOverflow44227962StackTraces.ClassA.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassA.cs:line 11 --- End of inner exception stack trace --- at StackOverflow44227962StackTraces.ClassA.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassA.cs:line 15 at StackOverflow44227962StackTraces.Program.Main(String[] args) in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\Program.cs:line 12
The bold text relates to the innermost exception, the italic text relates to the exception which wraps the innermost exception, and the plain text relates to the outermost exception, the one caught in the Program class. That message "--- End of inner exception stack trace ---" just means "this is the end of the information about the inner exception, what comes after this relates to the exception which is wrapped around it".
If you prefer, you could report the exception and its inner exceptions like this:
Exception cex = ex;
while (cex != null)
{
Console.WriteLine(cex.GetType().Name);
Console.WriteLine(cex.Message);
Console.WriteLine(cex.StackTrace);
Console.WriteLine();
cex = cex.InnerException;
}
This will output
Exception This is the outermost exception at StackOverflow44227962StackTraces.ClassA.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassA.cs:line 15 at StackOverflow44227962StackTraces.Program.Main(String[] args) in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\Program.cs:line 12 InvalidOperationException This is the exception containing the innermost exception at StackOverflow44227962StackTraces.ClassB.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassB.cs:line 16 at StackOverflow44227962StackTraces.ClassA.CallClassB() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassA.cs:line 23 at StackOverflow44227962StackTraces.ClassA.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassA.cs:line 11 ArgumentNullException This is the innermost exception (Parameter 'something') at StackOverflow44227962StackTraces.ClassC.DoSomething(Object something) in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassC.cs:line 15 at StackOverflow44227962StackTraces.ClassB.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassB.cs:line 12
Which you may find easier to read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With