I'm using this code to detect if a process is detected or not. I use kill() to get rid of it. I originally want to suspend it and then do some processing with it which will decide whether to allow the process to run or not but apparently I'm unable to get that done as it creates the process and kills it after that. Here's the code...
var query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 0, 0, 1),
"TargetInstance isa \"Win32_Process\"");
using (var watcher = new ManagementEventWatcher(query))
{
ManagementBaseObject mo = watcher.WaitForNextEvent();
ManagementBaseObject o = (ManagementBaseObject)mo["TargetInstance"];
String str = "";
foreach (PropertyData s in o.Properties)
{
if(s.Name.equals("ProcessId"))
{
Process p = Process.GetProcessById(Int32.Parse(s.Value));
p.Kill();
}
}
}
The problem here is that when I run it and then type cmd.exe in Run dialog it appears for a second and then dies. Its not supposed to appear even for a second. I want to catch the process before it even creates windows i.e. loads into memory. Can anyone suggest how may I achieve this?