⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 convunit.pas

📁 通常情况下, 用Delphi开发的程序, dfm是作为资源嵌入可执行文件的, 这就或多或少地带来了一些安全方面的问题. 比如, 通过分析资源, 就能大致了解Form上用了哪些控件 甚至, 通过修改
💻 PAS
字号:
unit ConvUnit;

interface

function DfmFileToDefFile(const DfmPath, DefPath: string): Boolean;

implementation

uses Windows, SysUtils, Classes, CodeUnit;

function FileToString(const FilePath: string): string;
var
  hFile: THandle;
  nSize: DWord;
begin
  Result := '';

  hFile := CreateFile(PChar(FilePath), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if (hFile = INVALID_HANDLE_VALUE) then Exit;

  nSize := GetFileSize(hFile, nil);

  SetLength(Result, nSize);
  ReadFile(hFile, Result[1], nSize, nSize, nil);

  CloseHandle(hFile);
end;

function StringToFile(const FilePath, FileText: string): Boolean;
var
  hFile: THandle;
  nSize: DWord;
begin
  Result := False;

  hFile := CreateFile(PChar(FilePath), GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  if (hFile = INVALID_HANDLE_VALUE) then Exit;

  WriteFile(hFile, FileText[1], Length(FileText), nSize, nil);

  SetEndOfFile(hFile);
  CloseHandle(hFile);

  Result := True;
end;

function StringToDefine(const Str: string): string;
var
  J, L: Integer;
  P: PDWord;
begin
  L := Length(Str);

  if (L = 0) then
    Result := #$27#$27
  else begin
    SetLength(Result, L * 4 + (L - 1) div 20 * 4);
    P := @Result[1];

    for J := 1 to L do
      if (J mod 20 = 1) and (J <> 1) then
      begin
        P^ := PDWord(PChar(' +'#13#10))^;
        Inc(P);
        P^ := PDWord(PChar('#$' + IntToHex(Ord(Str[J]), 2)))^;
        Inc(P);
      end else
      begin
        P^ := PDWord(PChar('#$' + IntToHex(Ord(Str[J]), 2)))^;
        Inc(P);
      end;
  end;
end;

function DfmFileToDefFile(const DfmPath, DefPath: string): Boolean;
var
  DfmStream, BinStream: TStringStream;
  DfmString, BinString, DefString: string;
begin
  DfmStream := nil;
  BinStream := nil;
  Result := False;

  try  
    // 读出Dfm
    DfmString := FileToString(DfmPath);
    if (DfmString = '') then Exit;
    DfmStream := TStringStream.Create(DfmString);

    // 转成Bin
    BinStream := TStringStream.Create(BinString);
    ObjectTextToBinary(DfmStream, BinStream);

    // 加密Bin
    BinString := BinStream.DataString;
    FastCode(BinString);

    // 转成Def
    DefString := StringToDefine(BinString);

    // 保存Def
    Result := StringToFile(DefPath, DefString);
  finally
    DfmStream.Free;
    BinStream.Free;
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -