杀进程和取文件最近使用时间
unit uFTPclient;
interface
uses
? SysUtils,Windows,Tlhelp32;
const
?? FILE_CREATE_TIME=0;??? //文件建立時間
?? FILE_MODIFY_TIME=1;??? //修改時間
?? FILE_ACCESS_TIME=2;??? //最后訪問時間
type
? TFileTimes?? =?? (ftLastAccess,?? ftLastWrite,?? ftCreation);
//文件是否正被使用
function IsFileInUse(FName:string):Boolean;
//提升權限
//殺服務程序進程,它會提示"拒絕訪問".其實只要程序擁有Debug權限即可
//使用的時候先EnableDebugPrivilege提升權限,然后KillTask(ExeFileName: string)
function EnableDebugPrivilege: Boolean;
//查找進程??
function FindProcessId(ExeFileName: string):THandle;
//殺進程
function KillTask(ExeFileName: string): Integer;
//取文件最后使用時間
function GetFileLastAccessTime(sFileName:string;uFlag:byte):TDateTime;
//設置文件最后修改時間
Function SetFileLastWriteTime(FileName:string; DateTime: TDateTime): Integer;
implementation
Function SetFileLastWriteTime(FileName:string; DateTime: TDateTime): Integer;
var
? hFile???????????????? : THandle;
? WriteTime, LocalTime? : TFILETIME;
? SystemTime??????????? : TSystemTime;
begin
? result:= 0;
? try
??? hFile := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);
??? if hFile <= 0 then
??? begin
????? result:= 1;
??? end else
??? begin
????? DateTimeToSystemTime(DateTime, SystemTime);
????? SystemTimeToFileTime(SystemTime, LocalTime);
????? LocalFileTimeToFileTime(Localtime, WriteTime);
????? If not SetFileTime(hFile, nil, nil, @WriteTime) then
????? begin
??????? result:= 2;
????? end;
??? end;
? finally
??? FileClose(hFile);
? end;
end;
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 EnableDebugPrivilege: Boolean;
? function EnablePrivilege(hToken: Cardinal; PrivName: string; bEnable: Boolean): Boolean;
? var
? TP: TOKEN_PRIVILEGES;
? Dummy: Cardinal;
? begin
? TP.PrivilegeCount := 1;
? LookupPrivilegeValue(nil, pchar(PrivName), TP.Privileges[0].Luid);
? if bEnable then
? TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
? else TP.Privileges[0].Attributes := 0;
? AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil, Dummy);
? Result := GetLastError = ERROR_SUCCESS;
? end;
var
? hToken: Cardinal;
begin
? OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken);
? result:=EnablePrivilege(hToken, 'SeDebugPrivilege', True);
? CloseHandle(hToken);
end;
function FindProcessId(ExeFileName: string):THandle;
var
? ContinueLoop:BOOL;
? 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(FProcessEntry32.szExeFile)=UpperCase(ExeFileName) then
??? begin
????? result:=FProcessEntry32.th32ProcessID;
????? break;
??? end;
??? ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
? end;
? CloseHandle (FSnapshotHandle);
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;
function GetFileLastAccessTime(sFileName:string;uFlag:byte):TDateTime;
var
? ffd:TWin32FindData;
? dft:DWord;
? lft:TFileTime;
? h:THandle;
begin
? h:=FindFirstFile(PChar(sFileName),ffd);
? if h<>INVALID_HANDLE_VALUE then
? begin
? case uFlag of
? FILE_CREATE_TIME:FileTimeToLocalFileTime(ffd.ftCreationTime,lft);
? FILE_MODIFY_TIME:FileTimeToLocalFileTime(ffd.ftLastWriteTime,lft);
? FILE_ACCESS_TIME:FileTimeToLocalFileTime(ffd.ftLastAccessTime,lft);
? else
??? FileTimeToLocalFileTime(ffd.ftLastAccessTime,lft);
? end;
? FileTimeToDosDateTime(lft,LongRec(dft).Hi,LongRec(dft).Lo);
? Result:=FileDateToDateTime(dft);
? windows.FindClose(h);
? end
? else
? result:=0;
end;
end.
轉載于:https://www.cnblogs.com/hnxxcxg/archive/2008/11/12/2940978.html
總結
以上是生活随笔為你收集整理的杀进程和取文件最近使用时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “愁君独向沙头宿”下一句是什么
- 下一篇: 【推荐】极限编程的十二大原则——小版本