我该如何使用C#将Delphi的DFM表单从二进制格式转换为文本格式?
最好的方法可能是调用CONVERT.EXE
,这是Delphi附带的命令行应用程序。以下是Delphi中的示例。您也可以在C#中执行相同的操作。
我使用以下四种方法来测试DFM文件格式并进行转换:
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;
Delphi的二进制DFM文件实际上是一个对象的二进制表示。
完成此工作的正确方法是编写自私的C#命令行实用程序(不再需要任何外部依赖项),基于对二进制DFM文件本身格式的了解。
如果格式未公开,进行逆向工程应该是可行的: