如何使用C#将Delphi的DFM表单从二进制格式转换为文本格式?
答案 0 :(得分:15)
最好的办法是调用CONVERT.EXE
,这是Delphi附带的命令行应用程序。 Here's an example in Delphi。您可以在C#中执行相同的操作。
答案 1 :(得分:3)
我使用这四种方法来测试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;
答案 2 :(得分:-1)
Delphi的二进制DFM文件毕竟是对象的二进制表示。
完成这项工作的正确方法是根据二进制DFM文件本身的格式的知识编写自私的C#命令行实用程序(不再需要任何外部依赖)。
如果没有公开格式,那么执行逆向工程应该是可行的: