4

How can I convert Delphi's DFM forms from the binary format into text format using C#?

Ozan
  • 4,345
  • 2
  • 23
  • 35
Lennie
  • 3,197
  • 4
  • 24
  • 26

3 Answers3

17

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#.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • 1
    Tip: Associate the *.DFM file type with CONVERT.EXE. That way you can select multiple DFM-files in the explorer and select "Open with Convert" in the context menu. – Jørn E. Angeltveit Aug 17 '10 at 10:15
3

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;
Bill
  • 2,993
  • 5
  • 37
  • 71
  • I'd vote this up if you hadn't ignored the "from C#" part of the question. – Ken White Aug 17 '10 at 12:50
  • It is unclear why you show *four* functions and not three or five (or nine?). I mean, the relevance of `DfmFile2Stream` is probably as high as that of `DfmStream2File`. – Wolf Mar 14 '22 at 14:05
0

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.
menjaraz
  • 7,551
  • 4
  • 41
  • 81