📄 uadscommon.pas
字号:
unit uADsCommon;
interface
uses Classes, SysUtils, windows, TLHelp32, shellapi, StrUtils;
function GetName(strLine: string): string;
function FindTarget(strExeName: string): longint;
function MyGetFileTime(const Tf: string): TDateTime;
function MySetFileDateTime(FileName: string; TargetTime: TDateTime): string;
function GetValue(strLine: string): string;
function ProcessRandomNumber(s: string): string;
function GetRandomByRange(const sScript: string): string;
function GetSysPath: string;
function GetFileSize(const FileName: string): LongInt;
function MyGetTempPath: string;
implementation
uses uGlobalVar, uLog;
function MyGetTempPath: string;
var
sTmp: string;
nLen:integer;
begin
result := '';
SetLength(sTmp, 512);
nLen:= windows.GetTempPath(512, PChar(sTmp));
SetLength(sTmp, nLen);
if sTmp[nLen] <> '\' then
Result := sTmp + '\'
else
result := sTmp;
end;
function GetFileSize(const FileName: string): LongInt;
var
SearchRec: TSearchRec;
begin
if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then
Result := SearchRec.Size
else
Result := -1;
end;
function GetSysPath: string;
var
sTmp: string;
begin
result := '';
//调用WindowsApi得到Windows的系统路径
SetLength(sTmp, 256);
GetSystemDirectory(PChar(sTmp), 256); //这是一个API函数
SetLength(sTmp, StrLen(PChar(sTmp)));
Result := sTmp + '\';
end;
function GetRandomByRange(const sScript: string): string;
//=[rr,1,10] ---> 范围[1,10)
var
nPos, nPos1, nPos2: integer;
nRange1, nRange2, n: integer;
sRange1, sRange2, s, strRangeKeyword: string;
begin
Randomize;
result := sScript;
s := sScript;
nPos1 := pos('[rr,', s);
if nPos1 > 0 then
begin
s := copy(s, nPos1, length(s) - nPos1 + 1);
nPos2 := pos(']', s);
if nPos2 > 0 then
begin
strRangeKeyword := copy(s, 1, nPos2);
s := copy(strRangeKeyword, 5, length(strRangeKeyword) - 5);
nPos := pos(',', s);
sRange1 := copy(s, 1, nPos - 1);
sRange2 := copy(s, nPos + 1, length(s) - nPos);
sRange1 := trim(sRange1);
sRange2 := trim(sRange2);
nRange1 := strtoint(sRange1);
nRange2 := strtoint(sRange2);
if nRange1 > nRange2 then
begin
n := nRange2;
nRange2 := nRange1;
nRange1 := n;
end;
n := nRange1 + random(nRange2 - nRange1);
result := ansireplacestr(sScript, strRangeKeyword, inttostr(n));
end;
end;
end;
function ProcessRandomNumber(s: string): string;
var
nRandom: integer;
begin
nRandom := random(32765);
result := AnsiReplaceStr(s, '[r0]', inttostr(nRandom));
nRandom := random(32765);
result := AnsiReplaceStr(result, '[r1]', inttostr(nRandom));
nRandom := random(32765);
result := AnsiReplaceStr(result, '[r]', inttostr(nRandom));
result := GetRandomByRange(result);
end;
function GetName(strLine: string): string;
var
i: integer;
begin
result := '';
for i := 1 to length(strLine) - 1 do
begin
if strLine[i] = '=' then
begin
result := copy(strLine, 1, i - 1);
exit;
end;
end;
end;
function GetValue(strLine: string): string;
var
i: integer;
begin
result := '';
for i := 1 to length(strLine) - 1 do
begin
if strLine[i] = '=' then
begin
result := copy(strLine, i + 1, length(strLine) - i);
exit;
end;
end;
end;
function CovFileDate(Fd: _FileTime): TDateTime;
var
Tct: _SystemTime;
Temp: _FileTime;
begin
FileTimeToLocalFileTime(Fd, Temp);
FileTimeToSystemTime(Temp, Tct);
CovFileDate := SystemTimeToDateTime(Tct);
end;
function MyGetFileTime(const Tf: string): TDateTime;
const
Model = 'yyyy/mm/dd,hh:mm:ss';
var
Tp: TSearchRec; { 申明Tp为一个查找记录 }
// T1, T2, T3: string;
begin
FindFirst(Tf, faAnyFile, Tp);
// T1 := FormatDateTime(Model, CovFileDate(Tp.FindData.ftCreationTime));
// T2 := FormatDateTime('hh:mm', CovFileDate(Tp.FindData.ftLastWriteTime));
// T3 := FormatDateTime(Model, Now);
result := CovFileDate(Tp.FindData.ftLastWriteTime);
SysUtils.FindClose(Tp);
// result := t2;
end;
function MySetFileDateTime(FileName: string; TargetTime: TDateTime): string;
var
FileHandle: HFile;
SystemTime: TSystemTime;
FileTime: TFileTime;
begin
DateTimeToSystemTime(TargetTime, SystemTime);
SystemTimeToFileTime(SystemTime, FileTime);
LocalFileTimeToFileTime(FileTime, FileTime); //将本地时间转化为系统的时间,再写入文件中
FileHandle := FileOpen(FileName, fmOpenWrite or fmShareDenyNone); //fmOpenWrite就带有GENERIC_WRITE的意思
if FileHandle <= 0
then
Result := 'Open File Error!!!'
else
if not SetFileTime(FileHandle, @FileTime, @FileTime, @FileTime)
then
Result := 'Set File Time Error!!!'
else
Result := 'Set File Time Successfully!!!';
FileClose(FileHandle);
end;
function FindTarget(strExeName: string): longint;
var
clp: bool;
hHandle: THandle;
PE32: TProcessentry32;
strExeNameTmp: string;
begin
RESULT := 0;
hHandle := CreateToolhelp32Snapshot(th32cs_snapprocess, 0);
PE32.dwsize := sizeof(PE32);
clp := Process32First(hHandle, PE32);
while integer(clp) <> 0 do
begin
strExeNameTmp := PE32.szExeFile;
if AnsiSameText(strExeNameTmp, strExeName) then
begin
RESULT := PE32.th32ProcessID;
BREAK;
end;
clp := Process32Next(hHandle, PE32);
end;
closehandle(hHandle);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -