当任何TGraphic后代使用类过程TPicture.RegisterFileFormat()注册其自己的图形文件格式时,它们都存储在Graphics.FileFormats全局变量中。
太糟糕了,FileFormats变量不在“Graphics.pas”的“interface”部分,所以我无法访问它。我需要读取此变量来为我的文件列表控件实现一个特殊的过滤器。
我是否可以在不手动修复Graphics.pas源代码的情况下获取该列表?
答案 0 :(得分:20)
您正在使用文件列表控件,因此可能是文件名列表。如果您不需要知道已注册的实际TGraphic
类类型,则只需注册是否注册了给定的文件扩展名(例如检查以后对TPicture.LoadFromFile()
的调用是否可能成功),您可以使用公共GraphicFileMask()
函数获取已注册文件扩展名列表,然后将您的文件名与该列表进行比较。例如:
uses
SysUtils, Classes, Graphics, Masks;
function IsGraphicClassRegistered(const FileName: String): Boolean;
var
Ext: String;
List: TStringList;
I: Integer;
begin
Result := False;
Ext := ExtractFileExt(FileName);
List := TStringList.Create;
try
List.Delimiter := ';';
List.StrictDelimiter := True;
List.DelimitedText := GraphicFileMask(TGraphic);
for I := 0 to List.Count-1 do
begin
if MatchesMask(FileName, List[I]) then
begin
Result := True;
Exit;
end;
end;
finally
List.Free;
end;
end;
或者,您只需加载文件,看看会发生什么:
uses
Graphics;
function GetRegisteredGraphicClass(const FileName: String): TGraphicClass;
var
Picture: TPicture;
begin
Result := nil;
try
Picture := TPicture.Create;
try
Picture.LoadFromFile(FileName);
Result := TGraphicClass(Picture.Graphic.ClassType);
finally
Picture.Free;
end;
except
end;
end;
更新:如果您要提取扩展程序和说明,可以使用TStringList.DelimitedText
来解析GraphicFilter()
函数的结果:
uses
SysUtils, Classes, Graphics;
function RPos(const ASub, AIn: String; AStart: Integer = -1): Integer;
var
i: Integer;
LStartPos: Integer;
LTokenLen: Integer;
begin
Result := 0;
LTokenLen := Length(ASub);
// Get starting position
if AStart < 0 then begin
AStart := Length(AIn);
end;
if AStart < (Length(AIn) - LTokenLen + 1) then begin
LStartPos := AStart;
end else begin
LStartPos := (Length(AIn) - LTokenLen + 1);
end;
// Search for the string
for i := LStartPos downto 1 do begin
if Copy(AIn, i, LTokenLen) = ASub then begin
Result := i;
Break;
end;
end;
end;
procedure GetRegisteredGraphicFormats(AFormats: TStrings);
var
List: TStringList;
i, j: Integer;
desc, ext: string;
begin
List := TStringList.Create;
try
List.Delimiter := '|';
List.StrictDelimiter := True;
List.DelimitedText := GraphicFilter(TGraphic);
i := 0;
if List.Count > 2 then
Inc(i, 2); // skip the "All" filter ...
while i <= List.Count-1 do
begin
desc := List[i];
ext := List[i+1];
j := RPos('(', desc);
if j > 0 then
desc := TrimRight(Copy(desc, 1, j-1)); // remove extension mask from description
AFormats.Add(ext + '=' + desc);
Inc(i, 2);
end;
finally
List.Free;
end;
end;
更新2:如果您只对注册的图形文件扩展名列表感兴趣,那么假设List
是已创建的TStrings
后代,请使用:< / p>
ExtractStrings([';'], ['*', '.'], PChar(GraphicFileMask(TGraphic)), List);
答案 1 :(得分:11)
GlScene项目有一个 PictureRegisteredFormats.pas 单元,用于实现黑客攻击。
答案 2 :(得分:9)
这是一个替代黑客,可能更安全然后是GLScene
解决方案。 它仍然是一个黑客,因为所需的结构是全局的,但在Graphics.pas
单元的实现部分,但我的方法使用了很少的“maigc常量”(硬编码偏移到代码)并使用两种不同的方法来检测GetFileFormats
中的Graphics.pas
函数。
我的代码利用了TPicture.RegisterFileFormat
和TPicture.RegisterFileFormatRes
都需要立即调用Graphics.GetFileFormats
函数的事实。代码检测相对偏移CALL
操作码并注册两者的目标地址。如果两个结果相同,则仅向前移动,这增加了安全系数。另一个安全因素是检测方法本身:即使编译器生成的序言发生变化,只要调用的第一个函数是GetFileFormats
,该代码就会找到它。
我不打算将"Warning: This will crash when Graphics.pas is compiled with the 'Use Debug DCUs' option."
放在单元的顶部(如GLScene
代码中所示),因为我已经使用debug dcu和没有调试dcu进行了测试工作。还使用包测试,它仍然有效。
此代码仅适用于32位目标,因此Integer
广泛用于指针操作。一旦我安装了Delphi XE2编译器,我将尝试为64位目标做这项工作。
更新:可在此处找到支持64位的版本:https://stackoverflow.com/a/35817804/505088
unit FindReigsteredPictureFileFormats;
interface
uses Classes, Contnrs;
// Extracts the file extension + the description; Returns True if the hack was successful,
// False if unsuccesful.
function GetListOfRegisteredPictureFileFormats(const List: TStrings): Boolean;
// This returns the list of TGraphicClass registered; True for successful hack, false
// for unsuccesful hach
function GetListOfRegisteredPictureTypes(const List:TClassList): Boolean;
implementation
uses Graphics;
type
TRelativeCallOpcode = packed record
OpCode: Byte;
Offset: Integer;
end;
PRelativeCallOpcode = ^TRelativeCallOpcode;
TLongAbsoluteJumpOpcode = packed record
OpCode: array[0..1] of Byte;
Destination: PInteger;
end;
PLongAbsoluteJumpOpcode = ^TLongAbsoluteJumpOpcode;
TMaxByteArray = array[0..System.MaxInt-1] of Byte;
PMaxByteArray = ^TMaxByteArray;
TReturnTList = function: TList;
// Structure copied from Graphics unit.
PFileFormat = ^TFileFormat;
TFileFormat = record
GraphicClass: TGraphicClass;
Extension: string;
Description: string;
DescResID: Integer;
end;
function FindFirstRelativeCallOpcode(const StartOffset:Integer): Integer;
var Ram: PMaxByteArray;
i: Integer;
PLongJump: PLongAbsoluteJumpOpcode;
begin
Ram := nil;
PLongJump := PLongAbsoluteJumpOpcode(@Ram[StartOffset]);
if (PLongJump^.OpCode[0] = $FF) and (PLongJump^.OpCode[1] = $25) then
Result := FindFirstRelativeCallOpcode(PLongJump^.Destination^)
else
begin
for i:=0 to 64 do
if PRelativeCallOpcode(@Ram[StartOffset+i])^.OpCode = $E8 then
Exit(StartOffset + i + PRelativeCallOpcode(@Ram[StartOffset+i])^.Offset + 5);
Result := 0;
end;
end;
procedure FindGetFileFormatsFunc(out ProcAddr: TReturnTList);
var Offset_from_RegisterFileFormat: Integer;
Offset_from_RegisterFileFormatRes: Integer;
begin
Offset_from_RegisterFileFormat := FindFirstRelativeCallOpcode(Integer(@TPicture.RegisterFileFormat));
Offset_from_RegisterFileFormatRes := FindFirstRelativeCallOpcode(Integer(@TPicture.RegisterFileFormatRes));
if (Offset_from_RegisterFileFormat = Offset_from_RegisterFileFormatRes) then
ProcAddr := TReturnTList(Pointer(Offset_from_RegisterFileFormat))
else
ProcAddr := nil;
end;
function GetListOfRegisteredPictureFileFormats(const List: TStrings): Boolean;
var GetListProc:TReturnTList;
L: TList;
i: Integer;
begin
FindGetFileFormatsFunc(GetListProc);
if Assigned(GetListProc) then
begin
Result := True;
L := GetListProc;
for i:=0 to L.Count-1 do
List.Add(PFileFormat(L[i])^.Extension + '=' + PFileFormat(L[i])^.Description);
end
else
Result := False;
end;
function GetListOfRegisteredPictureTypes(const List:TClassList): Boolean;
var GetListProc:TReturnTList;
L: TList;
i: Integer;
begin
FindGetFileFormatsFunc(GetListProc);
if Assigned(GetListProc) then
begin
Result := True;
L := GetListProc;
for i:=0 to L.Count-1 do
List.Add(PFileFormat(L[i])^.GraphicClass);
end
else
Result := False;
end;
end.