function TFrmImportExcel.FindProcess(AFileName: string): boolean;<br>var<br>hSnapshot: THandle;//用于获得进程列表<br>lppe: TProcessEntry32;//用于查找进程<br>Found: Boolean;//用于判断进程遍历是否完成<br>KillHandle: THandle;//用于杀死进程<br>begin<br>Result :=False;<br>hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);//获得系统进程列表<br>lppe.dwSize := SizeOf(TProcessEntry32);//在调用Process32First API之前,需要初始化lppe记录的大小<br>Found := Process32First(hSnapshot, lppe);//将进程列表的第一个进程信息读入ppe记录中<br>while Found do<br>begin<br> if ((UpperCase(ExtractFileName(lppe.szExeFile))=UpperCase(AFileName)) or (UpperCase(lppe.szExeFile )=UpperCase(AFileName))) then<br> begin<br> {if MsShow('发现打开Excel,是否将其关闭?',2)=6 then<br> begin<br> //由于我的操作系统是xp,所以在调用TerminateProcess API之前<br> //我必须先获得关闭进程的权限,如果操作系统是NT以下可以直接中止进程<br> KillHandle := OpenProcess(PROCESS_TERMINATE, False, lppe.th32ProcessID);<br> TerminateProcess(KillHandle, 0);//强制关闭进程<br> CloseHandle(KillHandle);<br> end;}<br> Result :=True;<br> end;<br> Found := Process32Next(hSnapshot, lppe);//将进程列表的下一个进程信息读入lppe记录中<br>end;<br>end;<br><br>procedure TFrmImportExcel.EndProcess(AFileName: string);<br>const<br>PROCESS_TERMINATE = $0001;<br>var<br>ContinueLoop: BOOL;<br>FSnapShotHandle: THandle;<br>FProcessEntry32: TProcessEntry32;<br>begin<br>FSnapShotHandle := CreateToolhelp32SnapShot(TH32CS_SNAPPROCESS, 0);<br>FProcessEntry32.dwSize := SizeOf(FProcessEntry32);<br>ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);<br>while integer(ContinueLoop) <> 0 do<br>begin<br> if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile))=UpperCase(AFileName)) or (UpperCase(FProcessEntry32.szExeFile )=UpperCase(AFileName))) then<br> TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),FProcessEntry32.th32ProcessID), 0);<br> ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);<br>end;<br>end;<br><br><br>//调用<br>if FindProcess('EXCEL.EXE') then<br>begin<br> if MsShow('检测到打开了Excel,是否让其关闭?',2)<>6 then Exit;<br> EndProcess('EXCEL.EXE');<br>end;