Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Killing a process running on a remote pc?

Status
Not open for further replies.

mattp14

Programmer
Apr 8, 2005
35
US
I work in tech support and I need to be able to build the functionality of killing a process that's running on a remote pc into my c# apps.

Any thoughts? The process class doesnt seem to allow this...
 
Solution 1:
Write an agent program that will be deployed on each remote machine.
Write your C# application using to do:
- grab the list of all connected machines
- select a remote machine name and sent a query to extract the list of all running processes
- select the process or processes to be killed or stopped (services)
- send the list (name) of the selected processes to the agent
- upon receiving the list of processes the agent will do:
- find the process by name. If it is running then kill it using Process.Kill() if the process has no graphical interface else use CloseMainWindow() and if that fails call Kill(). If the process is a running service you can stop it only. In order to do that, the agent should use Process and ServiceController classes.
Solution 2:
Create C# application using :
-Process class to read the running processes on the local/remote machine and kill them.
-ServiceController class to read the running services and device drivers on the local/remote machine and stop/start them.
Code:
Process[] arrProcess=Process.GetProcesses(strMachineName);
Process tmp= Process.GetProcessById(Int32.Parse (strKey),strMachineName);
Process tmp=Process.GetProcessByName(strProcName);
pSelectedProcess.Kill();
//Intercept the exit using an event handler for each process to kill
Code:
tmp.Exited += new EventHandler(OnProcessExited);
private void ProcessExited(object sender, EventArgs e)
{	
Process tmp2 = (Process)sender;
MessageBox.Show(tmp2.ProcessName + "  ID: " + tmp2.Id.ToString() + " is exiting");
}
//ServiceController
//Load services
ServiceController[]arrSrv;
arrSrv= ServiceController.GetServices(strMachineName);
//..
foreach(ServiceController srv in arrSrv)
{
// check the srv.Status
if(srv.CanStop)
{
try
{
srv.Stop(); System.Threading.Thread.Sleep(500);
while(srv.Status == ServiceControllerStatus.StopPending)
{
Application.DoEvents();
}
}
}
}
/..
[/code]
obislavu
 
Thanks for the reply. I may have to consider your first example, but would like to avoid it at all costs as that would be a security breach.

I am not able to get your second example to work however. I tried something similar to this before posting. The framework's process class doesnt seem to allow the killing of a process that's not local. I get an exception when trying, saying that this feature is not supported on a remote pc.

Any other ideas? I refuse to believe that there's no way around this...
 
Well, this might not help you out at all, but maybe take a look at using some existing tools that are already out there - you might even be able to write some custom code that can use these existing tools to get the job done. Good luck.

This is freeware, and it also allows you to kill processes remotely, amongst many other things! I thought it was cool.


PSTools from Sysinternals
 
The above code allows you to access processes/services/drivers on a remote machine machine but it is evident that you need to have the right privileges to access/kill/stop.
obislavu
 
Thanks rdgerken... i might have to look into that if I can't find a better programmatic solution.

obislavu: I've got the right privelages. Did you try that code and it worked for you?

Thanks for the help guys... it's very much appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top