If you look what youtaskkill really find , it will send a message WM_CLOSEto the process message loop. Therefore, you need to find a way to process this message and exit the application.
The following small test application shows a way to do just that. If you start it from Visual Studio using the CTRL+ F5shortcut (so that the process runs outside the debugger), you can close it with taskkill /IM [processname].
using System;
using System.Security.Permissions;
using System.Windows.Forms;
namespace TaskKillTestApp
{
static class Program
{
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
private class TestMessageFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x10)
{
Application.Exit();
return true;
}
return false;
}
}
[STAThread]
static void Main()
{
Application.AddMessageFilter(new TestMessageFilter());
Application.Run();
}
}
}
mghie source
share