Is IMAGE_DOS_SIGNATURE valid
I want to check that the file has a valid IMAGE_DOS_SIGNATURE (MZ)
function isMZ(FileName : String) : boolean;
var
Signature: Word;
fexe: TFileStream;
begin
result:=false;
try
fexe := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
fexe.ReadBuffer(Signature, SizeOf(Signature));
if Signature = $5A4D { 'MZ' } then
result:=true;
finally
fexe.free;
end;
end;
I know that I can use some code in the windows block to check IMAGE_DOS_SIGNATURE. The problem is I want to check IMAGE_DOS_SIGNATURE the fastest (for a large file). I need your suggestion for my code or maybe new code?
thanks
a source to share
The file size doesn't matter because your code only reads the first two bytes.
Any allocation and usage overhead TFileStream
that goes through SysUtils.FileRead
before reaching Win32 ReadFile
should be almost invisible noise compared to the cost of searching in a single situation where it matters, where you are browsing hundreds of executables.
There might be some benefit in setting up Windows caching using raw WinAPI, but I expect it to be very minor.
a source to share