Reputation: 413
Is there a way to kill a process with a C# script and have the process's exit code equal 0? I tried using Process.Kill() but that returns exit code -1. I am accessing getting the Excel process with the Process.GetProcessesByName() function. It looks like there is a Win32 API function TerminateProcess that takes an Exit Code as a parameter but I can't find anything equivalent in the Process class.
Edit: Interesting discovery- when I find the Excel process in the Windows task manager then click "End task" the Excel process exits with code 0. So, it appears that the signal that the Task Manager sends to the process is different then System.Diagnostics.Kill(). Now I just need to figure out what signal the Task Manager is sending and send it.
Upvotes: 0
Views: 748
Reputation: 413
I solved my problem even though it's a bit dirty. If someone knows a way to do it without DllImport it might be useful to share. Here's what I did:
class MyClass
{
[DllImport("Kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool TerminateProcess(IntPtr proc, uint uExit);
public void MyFunction()
{
foreach (var process in Process.GetProcesssesByName("Excel")
{
TerminateProcess(my_process.Handle, 0);
}
}
}
Upvotes: 1