-1

From a C# program, I am executing a byte[] containing another .NET console-application directly in memory using the 'Invoke' method, with this code:

static void Main(string[] args)
{
 byte[] Bytes = File.ReadAllBytes("C:\\test.exe");

 Assembly a = Assembly.Load(FileBytes);
 MethodInfo m = a.EntryPoint;
 var parameters = m.GetParameters().Length == 0 ? null : new[] { new string[0] };
 m.Invoke(null, parameters);
}

However, as the console-application that I am launching in memory ("C:\test.exe") never ends (it uses a while forever loop), the black cmd window never disappears.

How can it be executed hidden? As now I cannot use for example 'System.Diagnostics.ProcessWindowStyle.Hidden' as the process is not in disk.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
samsam
  • 39
  • 1
  • 8
  • 3
    you're launching, not lunching :) and see here http://stackoverflow.com/questions/836427/how-to-run-a-c-sharp-console-application-with-the-console-hidden you could also see about just creating a Windows Service or starting this code from one, you wouldn't see the console window – eddie_cat Nov 03 '14 at 20:25
  • 2
    @eddie_cat No lunch for me :( – Servy Nov 03 '14 at 20:26
  • lol sorry for my bad english =P – samsam Nov 03 '14 at 20:28
  • 1
    Is there a reason you're not just using `Process`? – Servy Nov 03 '14 at 20:28
  • Yes because using Process the console-application would have to be on disk, but I need it to be run directly on memory. – samsam Nov 03 '14 at 20:30
  • Can you change the console application's source code? – brz Nov 03 '14 at 20:40
  • Yes, and it uses a forever while loop in main, so the cmd black window never disappears and could be annoying. – samsam Nov 03 '14 at 20:43
  • Yes it is I also developed it, it is a compiled C# console-application. – samsam Nov 03 '14 at 20:47

2 Answers2

0

In the OP's case they've clarified that they can modify the source of the assembly being loaded. In that case you should be able to just set the application to be a GUI application, which will prevent the console window from being opened.

See How can I determine the subsystem used by a given .NET assembly? for a discussion of how to detect the application being console.

Community
  • 1
  • 1
OSborn
  • 895
  • 4
  • 10
0

I can think of two solutions:

1.Change your console application output type from Console Application to Windows Application. You can change it from Project Properties. This will hide the console window.

2.Write your assembly bytes to a file and use Process.Start to hide the console window. Something like this:

var f = Path.GetTempFileName() + ".exe";
File.WriteAllBytes(f, assemblyBytes);
var p = new Process();
p.StartInfo.FileName = f;
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
p.Start();
brz
  • 5,926
  • 1
  • 18
  • 18