I am facing difficulty to stop a process that I have started from a service in another user session, basically the service will start a process once a user login/ unlock/ connect, this step works perfectly, after that I save the process information(id, etc) in order to be able later to close it via Process.CloseMainWindow().
My issue here is the process that I have started never get closed using the mentioned method, also, after checking the process id existence during the time of closing it is present, but when I checked for the process name it returns Idle.
Here is my code:
protected override void OnSessionChange(SessionChangeDescription sessionChangeDescription)
{
try
{
if (sessionChangeDescription.SessionId > 0)
{
var userInfo = TermServicesManager.GetSessionInfo(Dns.GetHostEntry("").HostName, sessionChangeDescription.SessionId);
IPAddress ipAddress = new IPAddress(userInfo.ClientAddress.Address.Skip(2).Take(4).ToArray());
if (!rdpSessionList.ContainsKey(sessionChangeDescription.SessionId))
{
onRdpSession.sessionId = sessionChangeDescription.SessionId;
onRdpSession.ipAddress = ipAddress.ToString();
onRdpSession.userName = userInfo.UserName;
onRdpSession.sessionDateTime = DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH'-'mm'-'ss");
onRdpSession.recordFileName = onRdpSession.generateRecordFileName();
}
else
{
onRdpSession = rdpSessionList[sessionChangeDescription.SessionId];
}
switch (sessionChangeDescription.Reason)
{
case SessionChangeReason.SessionLogon:
case SessionChangeReason.RemoteConnect:
case SessionChangeReason.SessionUnlock:
int processId = -1;
string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\SessionFormRecorder.exe";
filepath = AppDomain.CurrentDomain.BaseDirectory;
RunInUserSession.StartProcessAsCurrentUser(onRdpSession.sessionId, "c:\\SessionFormRecorder.exe", null, null, false, processId);
onRdpSession.processId = processId;
WriteToFile("SessionChange" + DateTime.Now.ToLongTimeString() + ", SessionUnlock|RemoteConnect|SessionLogon [" + sessionChangeDescription.SessionId.ToString() + "]" + ", User: " + userInfo.UserName + ", Connect state: " + userInfo.ConnectState.ToString() + ", Client address: " + ipAddress.ToString() + ", user: " + userInfo.UserName + ", WinStationName: " + userInfo.WinStationName.ToString());
break;
case SessionChangeReason.SessionLock:
case SessionChangeReason.SessionLogoff:
case SessionChangeReason.RemoteDisconnect:
Process process = null;
process = Process.GetProcessById(onRdpSession.processId);
process.CloseMainWindow();
WriteToFile("SessionChange: " + onRdpSession.ToString() + ", Process name: " + process.ProcessName + ", " + DateTime.Now.ToLongTimeString() + " RemoteDisconnect|SessionLogoff|SessionLock [" + sessionChangeDescription.SessionId.ToString() + "]" + ", User: " + userInfo.UserName + ", Connect state: " + userInfo.ConnectState.ToString() + ", Client address: " + ipAddress.ToString() + ", user: " + userInfo.UserName + ", WinStationName: " + userInfo.WinStationName.ToString());
break;
default:
break;
}
}
}
catch(Exception ex)
{
WriteToFile("SessionChange exception: " + ex.Message + " || " + sessionChangeDescription.SessionId.ToString() + " || " + onRdpSession.sessionId.ToString());
}
}