解决自动更新因为EXE文件正在运行而失败的问题
2013-04-28 14:05
597 查看
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文件正在运行而失败的问题
- 解决 Xcode 5 资源文件更新后不自动复制的问题
- android 开发中遇到的错误及解决方式(更新R.java文件不能自动更新问题)
- 文件太新了也有错误? .exe.config 类型文件自动更新的问题.
- 解决点击Docker出现windows 正在查找bash.exe。如果想亲自查找文件,请点击“浏览”的问题
- NDK编译可执行文件在Android L中运行显示error: only position independent executables (PIE) are supported.失败问题解决办法。
- cxfreeze打包工程文件生成.exe,运行exe出现闪退问题,相关解决办法总结
- Android 版本更新下载自动安装APK,并解决Android6.0,7.0安装失败的问题
- 【转】NDK编译可执行文件在Android L中运行显示error: only position independent executables (PIE) are supported.失败问题解决办法。------不错
- 解决eclipse 文件更新不自动刷新的问题
- Android 版本更新下载自动安装APK,并解决Android6.0安装失败的问题
- 解决打开Android SDK Manager时出现“.....\sdk\tools\lib\\find_java.exe不是内部或外部命令,也不是可运行的程序或批处理文件“问题
- 解决jbuilder运行后tomcat下conf目录server8080.xml文件自动消失的问题
- SVN利用post-commit hooks自动同步代码到生产环境,解决大数量文件更新同步问题
- 解决任务计划程序未启动任务,因为相同任务的实例正在运行的问题
- 记事本打开 .exe 运行文件,发现所有程序文件都默认记事本打开了怎么解决问题
- Android 版本更新下载自动安装APK,并解决Android6.0安装失败的问题
- 文件太新了也有错误? .exe.config 类型文件自动更新的问题.
- 解决 Xcode 5 资源文件更新后不自动复制的问题
- NDK编译可执行文件在Android L中运行显示error: only position independent executables (PIE) are supported.失败问题解决办法。