解决自动更新因为EXE文件正在运行而失败的问题
2008-11-13 15:20
746 查看
function IsFileInUse(FName:string):Boolean;
var
HFileRes:HFILE;
begin
Result:=False;
if not FileExists(FName) then
Exit;
HFileRes:=CreateFile(PChar(FName),GENERIC_READ or GENERIC_WRITE,0,
nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
Result:=(HFileRes=INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(HFileRes);
end;
function KillTask(ExeFileName: string): Integer;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: boolean;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
procedure CloseMainProcess(fileName: string);
begin
if (ExtractFileExt(fileName)='.exe') or
(ExtractFileExt(fileName)='.com') then
begin
if IsFileInUse(fileName) then
begin
KillTask(fileName);
end;
end;
end;
var
HFileRes:HFILE;
begin
Result:=False;
if not FileExists(FName) then
Exit;
HFileRes:=CreateFile(PChar(FName),GENERIC_READ or GENERIC_WRITE,0,
nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
Result:=(HFileRes=INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(HFileRes);
end;
function KillTask(ExeFileName: string): Integer;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: boolean;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
procedure CloseMainProcess(fileName: string);
begin
if (ExtractFileExt(fileName)='.exe') or
(ExtractFileExt(fileName)='.com') then
begin
if IsFileInUse(fileName) then
begin
KillTask(fileName);
end;
end;
end;
相关文章推荐
- 解决自动更新因为EXE文件正在运行而失败的问题
- 解决eclipse 文件更新不自动刷新的问题
- android 开发中遇到的错误及解决方式(更新R.java文件不能自动更新问题)
- NDK编译可执行文件在Android L中运行显示error: only position independent executables (PIE) are supported.失败问题解决办法。
- Android Project from Existing Code 生成 R 文件错误、失败等问题解决办法 - 持续更新
- 解决jbuilder运行后tomcat下conf目录server8080.xml文件自动消失的问题
- 关于IIS服务启动失败的问题:“IIS提示‘另一个程序正在使用此文件,进程无法访问’”,的解决方法
- 运行VS2013编译成功后出现:无法启动程序“.exe” 系统找不到指定文件的问题的解决方法
- web调用cab不能注册dll、不能运行exe文件问题解决
- Android Project from Existing Code 生成 R 文件错误、失败等问题解决办法 - 持续更新
- 在WIN10系统下运行VS2013编译成功后出现:无法启动程序“.exe” 系统找不到指定文件的问题的解决方法
- SVN利用post-commit hooks自动同步代码到生产环境,解决大数量文件更新同步问题
- 文件太新了也有错误? .exe.config 类型文件自动更新的问题.
- 【转】NDK编译可执行文件在Android L中运行显示error: only position independent executables (PIE) are supported.失败问题解决办法。
- qt 在win8 win10下不能拖拽文件到程序的问题 终于找到答案了 因为把程序的权限设置太高了.解决方法,降低运行权限
- SVN利用post-commit hooks自动同步代码到生产环境,解决大数量文件更新同步问题
- 文件太新了也有错误? .exe.config 类型文件自动更新的问题.
- 开发环境下调试没有问题,发布后运行却提示无法更新数据库 *.MDF,因为数据库是只读的解决方法
- 解决 Xcode 5 资源文件更新后不自动复制的问题