高手指教:如何杀死一个com组件里面创建的excel进程,我写程序了,杀别的都行,就是杀不掉com里创建的excel进程。

zrb007 2004-09-23 11:49:29
程序如下:

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);
// ͨ¹ýÒÔÏÂ2ÐдúÂëÒ²¿ÉÒÔɱ½ø³Ì£¬µ«ÊÇÎÞ·¨É±Ò»Ð©ÏµÍ³½ø³ÌµÈ¸ß¼¶È¨Ï޵Ľø³Ì£¬
//¸ü¶à±ê×¼ÇëÕÒMSDNÖ÷Ì⣺HOWTO: Terminate an Application "Cleanly" in Win32
//»òÕß·ÃÎÊÕâÀ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;

...全文
233 12 打赏 收藏 举报
写回复
12 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
alinsoft 2004-10-25
  • 打赏
  • 举报
回复
我记得我曾经写过一个导出到EXCEL文件的程序。发现也是EXCEL的进程一直都在,后来是发现我在程序里没有对EXCEL对象CLOSE。楼主也可以找找会不会是类似的原因。
zrb007 2004-10-25
  • 打赏
  • 举报
回复
一直没有解决,遗留下来了:(,倒是这个问题也不是很影响大局,结贴吧!
Eastunfail 2004-10-14
  • 打赏
  • 举报
回复
呵呵,不太懂com之类的。现在在学校旁边的网吧,星期六回去后试试
zdq801104 2004-10-11
  • 打赏
  • 举报
回复
直接给斑竹发留言就可以了嘛,^-^!
radandgreensky 2004-10-07
  • 打赏
  • 举报
回复
TerminateProcess 可以直接拿来杀任何进程啊,就连系统核心也可以杀掉(杀了立刻重启^_^)
怎么会杀不掉Excel呢?
zdq801104 2004-10-06
  • 打赏
  • 举报
回复
直接去找“爱的眼睛”或“清洁工门诊”大哥帮帮忙,实在是不会,帮你顶顶
zrb007 2004-09-28
  • 打赏
  • 举报
回复
请大家关注;0
zrb007 2004-09-24
  • 打赏
  • 举报
回复
大伙帮忙啊,先谢了!
ttjacky 2004-09-23
  • 打赏
  • 举报
回复
up up
zrb007 2004-09-23
  • 打赏
  • 举报
回复
其实就是调api,高手应该自己写两行代码也能试出来,但好像普通的结束进程的方法不好用啊!
lostmyway 2004-09-23
  • 打赏
  • 举报
回复
眼花
pengocn 2004-09-23
  • 打赏
  • 举报
回复
upup
相关推荐
发帖
Windows SDK/API

1177

社区成员

Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
帖子事件
创建了帖子
2004-09-23 11:49
社区公告
暂无公告