delphi 功能函数大全-备份用
function CheckTask(ExeFileName: string): Boolean;
const
PROCESS_TERMINATE=$0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
result := False;
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 := True;
ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
end;
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 PosEx(const Source, Sub: string; Index: integer): integer;
var
Buf : string;
i,Len,C:integer;
begin
C := 0;
Result := 0;
Buf := Source;
i := Pos(Sub, Source);
Len := Length(Sub);
while i <> 0 do
begin
inc(C);
Inc(Result, i);
Delete(Buf, 1, i + Len - 1);
i := Pos(Sub, Buf);
if C >= Index then Break;
if i > 0 then Inc(Result, Len - 1);
end;
if C < Index then Result := 0;
end;
//-------------------截取字符串函数 开始
function split(src,dec : string):TStringList;
var
i : integer;
str : string;
begin
result := TStringList.Create;
repeat
i := pos(dec,src);
str := copy(src,1,i-1);
if (str='') and (i>0) then
begin
delete(src,1,length(dec));
continue;
end;
if i>0 then
begin
result.Add(str);
delete(src,1,i+length(dec)-1);
end;
until i<=0;
if src<>'' then
result.Add(src);
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 {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Result := (HFileRes = INVALID_HANDLE_VALUE); //如果CreateFile返回失败那么Result为真(即文件正在被使用)
if not Result then //如果CreateFile函数返回是成功
CloseHandle(HFileRes); //那么关闭句柄
end;
//--------------------------------------------------------------------