Joakim Carlsson
Joakim Carlsson

Reputation: 1580

Running console-based executable inside another process through reflection results in "The handle is invalid" IOException

I've uploaded my .exe file to my SQL server with Entity Framework, but when I try to execute my .exe file, I get the following exception:

Additional information: Exception has been thrown by the target of an invocation.

And this is how I am trying to do it

private void LoadBin_OnClick(object sender, RoutedEventArgs e)
{
    byte[] bin = Database.GetByte(BinList.SelectedItem.ToString());
    Assembly assembly = Assembly.Load(bin);
    MethodInfo method = assembly.EntryPoint;
    method.Invoke(null, null);
}

The exception is being thrown when I am trying to invoke it, my Main method does not have string[] args.

Activated Event Time Duration Thread System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.IOException: The handle is invalid.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.__Error.WinIOError() at System.Console.set_Title(String value) at SimpleExternal.Program.Main() --- 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, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at Loader.MainWindow.LoadCheat_OnClick(Object sender, RoutedEventArgs e) in C:\Users\Broder\Source\Repos\Loader\Loader\Loader\MainWindow.xaml.cs:line 40 9.00s

When loading the file via runPE and hosting the process in another it works great, but when trying to run it with assembly is when I get the error.

Upvotes: 0

Views: 755

Answers (1)

quetzalcoatl
quetzalcoatl

Reputation: 33536

That Console.set_Title may come from some runtime wrappers around your Main, or maybe from your code within that DLL/EXE. I really don't know, but that's not important. Important is that it runs when it is ran as standalone app. This means that some something was not prepared correctly when you ran it the other way.

I guess, just ensure that your current application that loads&runs this dll/exe also has a Console Window allocated. If you don't want it, then allocate it and hide it. Otherwise, any attempt to use Console class's features that touch Console Window will fail, because, well, there will be no console. The "handle" you see in that message refers to HWND, the handle-to-window.

Of course, you may also try to modify the code that dll/exe is built from to not use console window, or to check-if-there-is-a-console beforewards, or at the very least have a try/catch around all blocks of code that try to touch the console window.

For example, if Console.WriteLine is used for logging or status reporting, you may change that to System.Diagnostics.Trace.WriteLine instead

Upvotes: 1

Related Questions