views:

33

answers:

1

I am writing a program (Visual Studio 2010 in C# Windows Forms) which keeps track of multiple instances of the Remote Desktop Client (mstsc.exe - tested with Windows 7 version). I have been attempting to launch this program and grab its PID with the following code:

Process mstsc = Process.Start(mstscLocation, mstscConString);
int mstscProcessId = mstsc.Id;
DataRow row = openConn.NewRow();
row["RDP ID"] = mstscID;
openConn.Rows.Add(row);

This starts the client and returns an ID as it should. Problem is, if I try to terminate the PID with the following code, it fails to do so:

int rdpID = Convert.ToInt32(dgvOpenConnections.Rows[selectedIndex].Cells["RDP ID"].Value.ToString());

try
{
    // kill off mstsc
    Process mstsc = Process.GetProcessById(rdpID);
    mstsc.Kill();
}

I have verified that the PID that is recorded from Process.Start is the same as is retrieved from the DataGridView (dgvOpenConnections) and placed into rpdID (try fails and hits catch as the original PID no longer exists). Furthermore, I have issued a "tasklist" at a command prompt after starting one instance of MSTSC.EXE and can verify that it changes PIDs (in this test, C# recorded 4288 but tasklist shows it running as 8172).

I cannot kill all MSTSC processes as I am trying to control more than one. Is there a way to trace down the second PID MSTSC appears to use? My guess is it either starts a second process and gets rid of the first or maybe this is a child process (although the PID that is return no longer exists after start).

How in C# can I ensure I have the right process ID to later monitor or kill a specific instance of the Remote Desktop Client?

A: 

I tried your example using Process Explorer, and I couldn't see a second or child process being created. From beginning to end the Remote Desktop Process was one and the same, and after being created I was able to kill the process using the same PID I saw in the beginning.

ErikHeemskerk
What version of windows? If that is not the issue, am I doing something wrong? I know I am converting int > string > int but all debugging looks right. I am using mstsc.exe on Windows 7 (64bit os) from c:\windows\system32 version 6.1.7600.16385 (dated 7/13/2009). About to copy over the app + the dll from xp...bet it will work then. I will double check my code just to be sure!
ThaKidd
I'm using Windows 7 x64 6.1.7600 myself, mstsc.exe is version 6.1.7600.16385, dated 7/14/2009 (UTC). I'm pretty sure something else is wrong. Your code seems to do nothing wrong.
ErikHeemskerk