程序如下:procedure TForm1.Button1Click(Sender: TObject);
var
  proc: procobject;
  procHandle: THandle;
  ExitCD: integer;
// This does a "hard" kill. From the MS help file :
// The TerminateProcess function is used to unconditionally cause a process to exit.
// Use it only in extreme circumstances. The state of global data maintained by dynamic-link
// Libraries (DLLs) may be compromised if TerminateProcess is used rather than ExitProcess.
// TerminateProcess causes all threads within a process to terminate, and causes a process
// to exit, but DLLs attached to the process are not notified that the process is terminating.
begin
  screen.Cursor := crhourglass;
  ExitCD := 0;
  if listbox1.itemindex <> -1 then
    begin
      proc := procObject(listbox1.items.objects[listbox1.itemindex]); // get handle
      procHandle := 0;
      try
        TerminateApp(int64(Proc.Theproc.th32ProcessID), 1000);
        // &Iacute;¨&sup1;&yacute;&Ograve;&Ocirc;&Iuml;&Acirc;2&ETH;&ETH;&acute;ú&Acirc;&euml;&Ograve;&sup2;&iquest;&Eacute;&Ograve;&Ocirc;&Eacute;±&frac12;&oslash;&sup3;&Igrave;&pound;&not;&micro;&laquo;&Ecirc;&Ccedil;&Icirc;&THORN;·¨&Eacute;±&Ograve;&raquo;&ETH;&copy;&Iuml;&micro;&Iacute;&sup3;&frac12;&oslash;&sup3;&Igrave;&micro;&Egrave;&cedil;&szlig;&frac14;&para;&Egrave;¨&Iuml;&THORN;&micro;&Auml;&frac12;&oslash;&sup3;&Igrave;&pound;&not;
        //&cedil;ü&para;à±ê×&frac14;&Ccedil;&euml;&Otilde;&Ograve;MSDN&Ouml;÷&Igrave;&acirc;&pound;&ordm;HOWTO: Terminate an Application "Cleanly" in Win32
        //&raquo;ò&Otilde;&szlig;·&Atilde;&Icirc;&Ecirc;&Otilde;&acirc;&Agrave;&iuml;&pound;&ordm;http://support.microsoft.com/default.aspx?scid=kb;EN-US;q178893
        //procHandle := OpenProcess(PROCESS_TERMINATE, false, Proc.Theproc.th32ProcessID);
        //TerminateProcess(procHandle, ExitCd);
      except
        showmessage('An error has occurred while trying to kill the selected application.' +
          #13 + 'Error code :' + inttostr(ExitCD));
      end;
      if procHandle <> 0 then
        CloseHandle(procHandle);
    end;
  if ExitCD = 0 then
    begin
      // wait for windows to kill the task before refreshing the app list
      sleep(200);
      lookforexes;                  // refresh the process list
      screen.cursor := crDefault;
    end;
end;function TerminateAppEnum(hwnd: HWND; lParam: LPARAM): boolean; stdcall;
var
  dwID: DWORD;
begin
  GetWindowThreadProcessId(hwnd, @dwID) ;
  if(dwID = lParam) then
     PostMessage(hwnd, WM_CLOSE, 0, 0) ;
  Result:= TRUE;
end;function TerminateApp(dwPID: DWORD ; dwTimeout: DWORD): DWORD;
var
  hProc: THANDLE;
  dwRet: DWORD;
begin
  // If we can't open the process with PROCESS_TERMINATE rights,
  // then we give up immediately.
  {
  hProc := OpenProcess(SYNCHRONIZE or PROCESS_TERMINATE, FALSE,
     dwPID);
  }
  hProc := OpenProcess(SYNCHRONIZE or PROCESS_TERMINATE, true,
     (dwPID));  if(hProc = 0) then
     Result:= TA_FAILED;  // TerminateAppEnum() posts WM_CLOSE to all windows whose PID
  // matches your process's.
  EnumWindows(@TerminateAppEnum, dwPID) ;  // Wait on the handle. If it signals, great. If it times out,
  // then you kill it.
  if(WaitForSingleObject(hProc, dwTimeout) <> WAIT_OBJECT_0) then
  begin
    if TerminateProcess(hProc,0) then
      dwRet:= TA_SUCCESS_KILL
    else
    dwRet:= 0;
  end else
     dwRet := TA_SUCCESS_CLEAN ;  CloseHandle(hProc) ;
  Result:= dwRet ;
end;

解决方案 »

  1.   

    其实就是调api,高手应该自己写两行代码也能试出来,但好像普通的结束进程的方法不好用啊!
      

  2.   

    TerminateProcess 可以直接拿来杀任何进程啊,就连系统核心也可以杀掉(杀了立刻重启^_^)
    怎么会杀不掉Excel呢?
      

  3.   

    呵呵,不太懂com之类的。现在在学校旁边的网吧,星期六回去后试试
      

  4.   

    我记得我曾经写过一个导出到EXCEL文件的程序。发现也是EXCEL的进程一直都在,后来是发现我在程序里没有对EXCEL对象CLOSE。楼主也可以找找会不会是类似的原因。