有效 IMAGE_DOS_SIGNATURE
我想检查一个文件是否具有有效的 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;
我知道我可以在 Windows 单元中使用一些代码来检查 IMAGE_DOS_SIGNATURE。问题是我想要最快的方式来检查 IMAGE_DOS_SIGNATURE (对于大文件)。我需要您对我的代码或者新代码提出一些建议?
谢谢
I want to check a 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 I can use some code in Windows unit to check the IMAGE_DOS_SIGNATURE. The problem is I want the fastest way to check IMAGE_DOS_SIGNATURE (for a big file). I need your some suggestion about my code or maybe a new code?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文件的大小并不重要,因为您的代码仅读取前两个字节。
分配和使用
TFileStream
的任何开销(在到达 Win32ReadFile
之前会经过SysUtils.FileRead
),与在唯一重要的情况下进行搜索的成本,即您正在扫描数百个可执行文件。使用原始 WinAPI 调整 Windows 缓存可能会带来一些好处,但我预计它的好处非常有限。
The size of the file doesn't matter because your code only reads the first two bytes.
Any overhead from allocating and using a
TFileStream
, which goes throughSysUtils.FileRead
before reaching Win32ReadFile
, ought to be all but invisible noise compared to the cost of seeking in the only situation where it should matter, where you're scanning through hundreds of executables.There might possibly be some benefit in tweaking Windows' caching by using the raw WinAPI, but I would expect it to be very marginal.