[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
/// <summary>
/// 根据进程id杀死进程
/// </summary>
/// <param name="excel">excl程序</param>
public static void KillProcessById(Excel.Application excel)
{
IntPtr t = new IntPtr(excel.Hwnd);//获得excel程序句柄
int k = 0;
GetWindowThreadProcessId(t, out k);//k得到进程id
if (k != 0)
{
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
}
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
/// <summary>
/// 根据进程id杀死进程
/// </summary>
/// <param name="excel">excl程序</param>
public static void KillProcessById(Excel.Application excel)
{
IntPtr t = new IntPtr(excel.Hwnd);//获得excel程序句柄
int k = 0;
GetWindowThreadProcessId(t, out k);//k得到进程id
if (k != 0)
{
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
}