2

I am loading a console application assembly from a resource into a memory stream (later to byte array) and executing the assembly in a new thread. Note that the container assembly is standard Winforms.

The console application is running in the new thread, but it is not visible. If the console application is replaced with winforms assembly, the main form is visible and works correctly. If I run the console application from disk as a test, it loads fine and is visible.

Any ideas?

ThreadStart _thdInvoke;
Thread _thdMain;
MethodInfo _methodInfo;

snip

/* memoryStream is the console application loaded from embedded resource */
var assembly = Assembly.Load(memoryStream.ToArray());
_methodInfo = assembly.EntryPoint;
_thdInvoke = InitializeEp;
_thdMain = new Thread(_thdInvoke);
_thdMain.Start();

snip

private void InitializeEp()
    {
        try
        {
            _methodInfo.Invoke(null, null);
        }
        catch (Exception)
        {

        }
    }

Note that the console application Main method has been changed so that string[] args has been removed.

Thanks for the help!

navitiello
  • 67
  • 6
  • 1
    What needs to be shown? The console window? Probably the console window exits too fast. Before `Main(...)` ends (thus just before }), add `Console.ReadLine()` to wait for Enter key. Is the window still hidden? – Sjips Nov 03 '14 at 23:07
  • The console application is running a loop which takes about 15 seconds to complete. After the loop, there is a Console.ReadKey(). – navitiello Nov 03 '14 at 23:15
  • You cannot use Process.Start, or what is the use of starting a console application? Is it an application inside the DLL? – Icepickle Nov 03 '14 at 23:16
  • Process.Start would be for running the application from disk. The console application is being started from memory. Let's just say the usage is something similar to: the main application (winform app) is loading a console application (as a resource) into memory and running it. The console application can not be saved to disk in any way. – navitiello Nov 03 '14 at 23:21
  • What type of program is the outer program? Is it also a console program? Or a GUI program (WinForms, WPF)? – Peter Duniho Nov 03 '14 at 23:29
  • The outer program is a standard WinForms assembly. – navitiello Nov 03 '14 at 23:32
  • if you have full control, why have this necessity in your program? – Icepickle Nov 03 '14 at 23:49
  • That isn't really important or relevant to the issue, but the console application starts out as an AES256 encrypted cipher before it is decrypted and loaded into memory. – navitiello Nov 04 '14 at 00:00

1 Answers1

1

It is "invisible" because the console window doesn't actually exist. Nothing told the OS to create the console and redirect all reads/writes to it.

It appears you need to do some setup in order to create the console window for your application.

Your Main() is running, but all calls to Console are completely ignored and reads will not block.

I tested the code you had and verified your program should be running (minus the console window). You can test this by taking your void Main() and changing it to int Main() then returning some number. _methodInfo.Invoke(...) will return the value int Main() returned.

Community
  • 1
  • 1
TyCobb
  • 8,909
  • 1
  • 33
  • 53