内存扫描程序始终返回相同的结果

时间:2012-04-04 18:48:27

标签: delphi memory

使用此代码,我猜扫描更快,但扫描始终返回SAME地址。

E.G:

00123456
00124567
00135478
00145893
00123456 //start repeat 
00124567
00135478
00145893
00123456 //start repeat 
00124567
00135478
00145893

这是我的程序:

procedure SCANBYTE(value: integer);
var
 lpflOldProtect: dword;
 s: size_t;
 mbi: MEMORY_BASIC_INFORMATION;
 SI: SYSTEM_INFO;
 lpStartAddress, lpStopAddress: dword;
 addr: dword;
 i: dword;
begin
 GetSystemInfo(si);
 lpStartAddress := dword(SI.lpMinimumApplicationAddress);
 lpStopAddress := dword(SI.lpMaximumApplicationAddress);
 for addr := lpStartAddress to lpStopAddress do begin
  S:= VirtualQuery(Pointer(addr), MBI, SizeOf(MEMORY_BASIC_INFORMATION));
  if (S=SizeOf(MEMORY_BASIC_INFORMATION)) and (MBI.State = MEM_COMMIT) and (MBI.Type_9 = MEM_PRIVATE) and (MBI.RegionSize>0) and (MBI.Protect = PAGE_READWRITE) then begin
   for i := dword(MBI.BaseAddress) to (dword(MBI.BaseAddress) + dword(MBI.RegionSize)) - 4096 do begin
     if value = PBYTE(i)^ then ListBox1.Items.Add(IntToHex(i,8));
   end;
  end;
 end;
end;

我猜问题出在最后的FOR循环中:

(...)
for i := dword(MBI.BaseAddress) to (dword(MBI.BaseAddress) + dword(MBI.RegionSize)) - 4096 do begin
(...)

但我真的不知道.. 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:8)

您可以在从起始地址到结束地址的循环中运行代码。地址addr每次在循环周围增加 1 VirtualQuery为您提供有关整个网页的信息。页面中的所有地址都具有相同的基址。 The documentation告诉您,“此值向下舍入到下一页边界。”

仔细观察,您应该看到mbi.BaseAddress对于外循环的4096次迭代保持不变(假设页面大小为4096)。因此,您将一遍又一遍地重新扫描同一块内存。 (这也可能解释why your code is slow。)