0

I have multiple process named COM SURROGATE (dllhost.exe) are running in Task Manager at my System. In which I need to kill all those processes which are running with my USERNAME(One is running with SYSTEM/"" so no need to kill that).

I need to do like below but only for the dllhost process running with myusername :

Process[] runningProcess = Process.GetProcessesByName("dllhost");
                if(runningProcess.Length > 0 )
                {
                    foreach (var surrogateProcess in runningProcess)
                    {
                        surrogateProcess.Kill();
                    }
                }
  
ksrds
  • 136
  • 2
  • 16
  • 1
    Does this answer your question? [How do I determine the owner of a process in C#?](https://stackoverflow.com/questions/777548/how-do-i-determine-the-owner-of-a-process-in-c) – JonasH Oct 05 '20 at 13:45
  • @JonasH : Thanks, I'll apply it tomorrow morning and confirm if it works..meanwhile let's see if somebody help here in another way. – ksrds Oct 05 '20 at 14:09
  • @JonasH : It won't work. There is s process with my id running already but searcher is not finding that and owner return with "NO OWNER" for that. My process is : C:\Windows\SysWOW64\dllhost.exe – ksrds Oct 06 '20 at 06:03

1 Answers1

0

I found the solution for this. Below are the key points : We can't close process running by SYSTEM/""/otheruser etc without admin access so process.kill() used to throw Access Denied error.

By using below we are trying to terminate all the process with the name of dllhost.exe (we can write any of the process name) and used Style.Hidden so user will not see the cmd prompt and not even the message. Message could be like : ERROR: The process "dllhost.exe" with PID 6332 could not be terminated. Reason: Access is denied. [This is running with either system or other user] SUCCESS: The process "dllhost.exe" with PID 15320 has been terminated. [This was running with my username which can be closed without any issue]

System.Diagnostics.Process process = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                startInfo.FileName = "cmd.exe";
                startInfo.Arguments = "/C taskkill /IM dllhost.exe /F";
                process.StartInfo = startInfo;
                process.Start();

The code will close all the processes running with my username only. Cheers.

ksrds
  • 136
  • 2
  • 16