有效 IMAGE_DOS_SIGNATURE

发布于 2024-08-30 04:10:51 字数 536 浏览 1 评论 0原文

我想检查一个文件是否具有有效的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随风而去 2024-09-06 04:10:51

文件的大小并不重要,因为您的代码仅读取前两个字节。

分配和使用 TFileStream 的任何开销(在到达 Win32 ReadFile 之前会经过 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 through SysUtils.FileRead before reaching Win32 ReadFile, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文