views:

3454

answers:

6

I'm launching a Java process ("java.exe") from .Net. using Process.Start(). In addition to the Java process, another process called conhost.exe is launched somehow. I am redirecting the output from the Java process to the .Net process.

  1. Why is conhost.exe even launched?
  2. How do I track it from .Net? I want to track this specific instance, and since I'm not creating it directly (but rather the Java.exe process), I don't have it's PID.
+1  A: 

To be blatant, I don't know anything about Java, so I can't help you with #1. I can help with #2, though.

To track it with .NET, you can use System.Diagnostics.

First, you have to get each of the processes by the name "conhost.exe", launch Java, then get all the processes again, and compare.

To get the specific instances, use the process ID:

foreach (Process singleProcess in Process.GetProcessesByName("conhost"))
{
    //Store the following in some kind of array
    somePidArray[yourindex] = singleProcess.Id;
}

Then when you want to kill the processes, run the exact same loop, and if the process ID was not stored in the initial loop, then call singleProcess.Kill(); on it. Then you will have kept all the initial conhost.exe processes alive, and only kill the ones created between the time you launch Java in your program and the time your Java process exits.

Breakthrough
I don't know which conhost.exe is mine, because I'm not launching it directly, so I have no way to understand which PID is my own.
ripper234
Are you using Windows 7?
Breakthrough
Yes. I also hate that SO requires at least 15 characters in comments.
ripper234
+2  A: 

Update: I guess that you can find the reasoning on the oldnewthing. It was probably added to restore some functionality( like drug and drop) that was removed from Vista due to security reasons.

Before Update: conhost seems to lunch on any cmd.exe opening. Its probably some new, undocumented thing on windows 7.

Igal Serban
+1  A: 

It is a process that hosts the console window. It was introduced in Windows 7 (iirc), in older versions the functionality was executed in the context of the csrss.exe process.

Filip Navara
Sorry. I have massed up your post by mistake...
Igal Serban
+5  A: 

In earlier versions of Windows, console windows were hosted in CSRSS, which is a highly privileged, trusted, system critical process. On Win7, it appears that console windows are now hosted in conhost.exe, which has less rights. This was probably done for security & reliability reasons - a security issue in the console system won't compromise the entire box, and a crash in the console code won't blue screen the system.

Michael
A: 

This raises a related question: do you want a console window for the Java application spawned by the .NET app? If not, you can execute the javaw command instead of java. I haven't experimented with it on Vista, but it may eliminate the conhost.exe process.

erickson
I want a console window because I'm reading its output from the .Net side.
ripper234
+1  A: 
The How-To Geek