解决方案 »
- Delphi批量插入数据库的问题
- 博士妙想帖上首页头条,散分祝贺
- 初学delphi问题,请帮忙!
- 应用程序图标如何改变?
- 请问这是什么错误: [Error] RLINK32: Error opening file "E:\Borland\Delphi5\Projects\Unit1.DFM"
- 急急在线等待!!!
- 熟悉事务的高手请进
- 如何在程序中创建一个FOXBASE格式的数据表(DBF格式),并往里写数据?
- 如何将金额合计的自动小写变成大写????????????
- 我是一个新手,如何做一个组合查询程序,最好有源码。谢谢大家!
- 如何修改TBitmap这个类?我晕了!!
- ClientDataSet修改数据库并导入后再次修改会出错,怎么办?
可以参考http://www.codes51.com/article/detail_23118.html
implementation{$R *.dfm}
{AFile: 要结束的进程
AEffectFirst: 是否只结束第一个找到的进程可以只输入EXE名称, 或者全路径+文件名, 或者只是某个文件路径
如果输入的只是某个路径, 则关闭属于这个路径下的所有进程}
function KillProcess(AFile: string; AEffectFirst: Boolean = True): WORD; overload;
const
PROCESS_TERMINATE = $0001;
var
nContinueLoop: BOOL;
nSnapShotHandle: THandle;
nProcessEntry32: TProcessEntry32;
nSelfID, nPrHandle: Cardinal;
nMBF: array [0..MAX_PATH] of Char;
nCMode: Byte; {0:检测全路径文件, 1:仅检测文件 2:仅检测路径}
nCPath, nCFile, nStr, nFile, nPath: string;
begin
Result := 0;
AFile := UpperCase(AFile);
nCPath := ExtractFilePath(AFile);
nCFile := ExtractFileName(AFile); if (nPath <> '') and (nCFile <> '') then
nCMode := 0
else if nCFile <> '' then
nCMode := 1
else if nCPath <> '' then
nCMode := 2
else
Exit; nSelfID := GetCurrentProcessID;
nSnapShotHandle := CreateToolhelp32SnapShot(TH32CS_SNAPPROCESS, 0);
try
nProcessEntry32.dwSize := SizeOf(nProcessEntry32);
nContinueLoop := Process32First(nSnapShotHandle, nProcessEntry32);
while nContinueLoop do
begin
if nProcessEntry32.th32ProcessID <> nSelfID then
begin
nStr := UpperCase(nProcessEntry32.szExeFile);
nFile := ExtractFileName(nStr);
nPrHandle := OpenProcess(PROCESS_ALL_ACCESS, False, nProcessEntry32.th32ProcessID);
try
if nCMode <> 1 then
begin
nPath := ExtractFilePath(nStr);
if nPath = '' then
begin
if GetModuleFileNameEx(nPrHandle, 0, @nMBF[0], SizeOf(nMBF)) > 0 then
nPath := UpperCase(ExtractFilePath(nMBF));
end;
end; if ((nCMode = 0) and (nPath = nCPath) and (nFile = nCFile)) {全路径文件名相同}
or ((nCMode = 1) and (nFile = nCFile)) {文件名相同}
or ((nCMode = 2) and (Copy(nPath, 1, Length(nCPath)) = nCPath)) {路径包含} then
begin
TerminateProcess(nPrHandle, 0); {强制关闭进程}
if AEffectFirst then
Break;
end;
finally
CloseHandle(nPrHandle);
end;
end;
nContinueLoop := Process32Next(nSnapShotHandle, nProcessEntry32);
end;
finally
CloseHandle(nSnapShotHandle);
end;
end;procedure TFormMain.btnkill2Click(Sender: TObject);
begin
KillProcess(PChar(edtname2.Text) ,False);
end;用函数去找句柄,然后去关闭他。无论word还是别的exe
这个要求比较蛋疼啊,比较好的办法应该是从文件驱动入手
比较蛋疼的办法,就是FileOpen一下,然后写入一个字节内容,保存,如果可以保存成功,说明没打开(然后再删除最后的一个写入的字节内容),否则说明打开了