开发者

Delphi Form (DFM) To Text

How can I开发者_运维问答 convert Delphi's DFM forms from the binary format into text format using C#?


Best way is probably to call CONVERT.EXE, a command-line app included with Delphi. Here's an example in Delphi. You can do the same in C#.


I use these four methods to test the DFM file format and to convert as follows:

function IsDFMStreamBinary( AStream: TMemoryStream ): Boolean;
{ Returns true if dfm file is in a binary format }
var
  F: TMemoryStream;
  B: byte;
begin
  B := 0;
  F := TMemoryStream.Create;
  F.LoadFromStream( AStream );
  try
    F.read( B, 1 );
    Result := B = $FF;
  finally
    F.Free;
  end;
end;

function DfmFile2Stream( const ASrc: string; ADest: TStream ): Boolean;
{ Save dfm to stream }
var
  SrcS: TFileStream;
begin
  SrcS := TFileStream.Create( ASrc, fmOpenRead or fmShareDenyWrite );
  try
    ObjectResourceToText( SrcS, ADest );
    Result := True;
  finally
    SrcS.Free;
  end;
end;

procedure Txt2DFM( ASrc, ADest: string );
{ Convert Text to DFM }
var
  SrcS, DestS: TFileStream;
begin
  SrcS := TFileStream.Create( ASrc, fmOpenRead );
  DestS := TFileStream.Create( ADest, fmCreate );
  try
    ObjectTextToResource( SrcS, DestS );
  finally
    SrcS.Free;
    DestS.Free;
  end;
end;

function Dfm2Txt( const ASrc, ADest: string ): boolean;
{ Convert a binary DFM to text }
var
  ASrcS, ADestS: TFileStream;
begin
  ASrcS := TFileStream.Create( ASrc, fmOpenRead );
  ADestS := TFileStream.Create( ADest, fmCreate );
  try
    ObjectResourceToText( ASrcS, ADestS );
    Result := True;
  finally
    ASrcS.Free;
    ADestS.Free;
  end;
end;


A Delphi's binary DFM file is after all a binary representation of an object.

The proper way to do the job is to write selfish C# command line utility (No more need of any external dependancy), based on the knowledge of the format of binary DFM file itself.

If the format is not disclosed, doing a reversed engineering should be feasible:

  • ObjectResourceToText source code is available (Classes.pas).
  • Converters (from binary to text and vice versa) to generate input required from tools are also available.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜