取一些通用函数

取程序路径
function GetAppPath: String;
begin
  Result := ExtractFilePath(Application.ExeName);
end;
取计算机名 
1 Function GetPcName:String;
2 Var
3   buffer:array[0..254]of char;
4   Size:Dword;
5 begin
6   Size:=SizeOf(Buffer);
7   GetComputerName(buffer,Size);
8   result:=String(Buffer);
9 end;
输入对话框
function InputQuery(const ACaption, APrompt: string; var Value: string):
    Boolean;

 var
  Form: TForm;
  Prompt: TLabel;
  Edit: TEdit;
  DialogUnits: TPoint;
  ButtonTop, ButtonWidth, ButtonHeight: Integer;


begin
  Result := False;
  Form := TForm.Create(Application);
  with Form do
    try
      Font.Name:='宋体';     //指定字体
      Font.Size:=9;
      Canvas.Font := Font;
      DialogUnits := GetAveCharSize(Canvas);
      BorderStyle := bsDialog;
      Caption := ACaption;
      ClientWidth := MulDiv(180, DialogUnits.X, 4);
      Position := poScreenCenter;
      Prompt := TLabel.Create(Form);
      with Prompt do
      begin
        Parent := Form;
        Caption := APrompt;
        Left := MulDiv(8, DialogUnits.X, 4);
        Top := MulDiv(8, DialogUnits.Y, 8);
        Constraints.MaxWidth := MulDiv(164, DialogUnits.X, 4);
        WordWrap := True;
      end;
      Edit := TEdit.Create(Form);
      with Edit do
      begin
        Parent := Form;
        Left := Prompt.Left;
        Top := Prompt.Top + Prompt.Height + 5;
        Width := MulDiv(164, DialogUnits.X, 4);
        MaxLength := 0;
        Text := Value;
        SelectAll;
      end;
      ButtonTop := Edit.Top + Edit.Height + 15;
      ButtonWidth := MulDiv(50, DialogUnits.X, 4);
      ButtonHeight := MulDiv(14, DialogUnits.Y, 8);
      with TButton.Create(Form) do
      begin
        Parent := Form;
        Caption := '确定(&O)'; 
        ModalResult := mrOk;
        Default := True;
        SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth,
          ButtonHeight);
      end;
      with TButton.Create(Form) do
      begin
        Parent := Form;
        Caption := '取消(&C)';
        ModalResult := mrCancel;
        Cancel := True;
        SetBounds(MulDiv(92, DialogUnits.X, 4), ButtonTop, ButtonWidth, ButtonHeight);
        Form.ClientHeight := Top + Height + 13;          
      end;
      if ShowModal = mrOk then
      begin
        Value := Edit.Text;
        Result := True;
      end;
    finally
      Form.Free;
    end;
end;
检测调试器是否存在!可以防止cracker用工具修改代码并运行之
 1 function AntiLoader():Boolean;
 2 var
 3   isDebuggerPresent: function:Boolean;
 4   Addr: THandle;
 5 begin
 6   Addr := LoadLibrary('kernel32.dll');
 7   isDebuggerPresent := GetProcAddress(Addr, 'IsDebuggerPresent');
 8   if isDebuggerPresent then
 9     Result:=True
10   else
11     Result:=False;
12 end;
生成GUID
1 function CoCreateGUID: String;
2 var
3   guid: TGUID;
4   s: string;
5 begin
6   activex.CoCreateGUID(guid);
7   s := GUIDToString(guid);
8   Result := Copy(s, 2, Length(s) - 2);
9 end;
删除文件夹
 1 function DoRemoveDir(sDirName:String): Boolean;
 2 var
 3   hFindFile:Cardinal;
 4   tfile:String;
 5   sCurDir:String;
 6   bEmptyDir:Boolean;
 7   FindFileData:WIN32_FIND_DATA;
 8 begin
 9   bEmptyDir:=True;
10   sCurDir:=GetCurrentDir;
11   SetLength(sCurDir,Length(sCurDir));
12   ChDir(sDirName);
13   hFindFile:=FindFirstFile('*.*',FindFileData);
14   if hFindFile<>INVALID_HANDLE_VALUE then
15   begin
16     repeat
17       tfile:=FindFileData.cFileName;
18       if (tfile='.') or (tfile='..') then
19       begin
20         bEmptyDir:=bEmptyDir and True;
21         Continue;
22       end;
23       bEmptyDir:=False;
24       if FindFileData.dwFileAttributes= FILE_ATTRIBUTE_DIRECTORY then
25       begin
26           if sDirName[Length(sDirName)]<>'\' then
27             DoRemoveDir(sDirName+'\'+tfile)
28           else
29             DoRemoveDir(sDirName+tfile);
30           result:=  RemoveDirectory(PChar(tfile));
31       end
32       else
33       begin
34         result := DeleteFile(PChar(tfile))
35       end;
36     until FindNextFile(hFindFile, FindFileData)=false;
37     windows.FindClose(hFindFile);
38   end else
39   begin
40     ChDir(sCurDir);
41     result:=false;
42     exit;
43   end;
44   if bEmptyDir then
45   begin
46     ChDir('..');
47     RemoveDirectory(PChar(sDirName));
48   end;
49   ChDir(sCurDir); 
50   result:=true;
51 end;
  • ExpandFileName() 返回文件的全路径(含驱动器、路径) 
  • ExtractFileExt() 从文件名中抽取扩展名 
  • ExtractFileName() 从文件名中抽取不含路径的文件名 
  • ExtractFilePath() 从文件名中抽取路径名 
  • ExtractFileDir() 从文件名中抽取目录名 
  • ExtractFileDrive() 从文件名中抽取驱动器名 
  • ChangeFileExt() 改变文件的扩展名 
  • ExpandUNCFileName() 返回含有网络驱动器的文件全路径 
  • ExtractRelativePath() 从文件名中抽取相对路径信息 
  • ExtractShortPathName() 把文件名转化为DOS的8·3格式 
  • MatchesMask() 检查文件是否与指定的文件名格式匹配 
  • ExtractFilePath(FileName:String) 
  • 该函数返回路径名,其结尾字符总是“\” 
原文地址:https://www.cnblogs.com/jieke/p/2837114.html