⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cantumemtest.pas

📁 source code for Marco Cantu s book Delphi 2007 Handbook
💻 PAS
字号:
unit CantuMemTest;

interface

uses
  SysUtils, Windows;

// tries to extract the pointer to the class which is at the
// beginning of an object memory block, and checks whether this
// is actually a class (empirically)
function IsPointerToObject (Address: Pointer): Boolean;

// verifies if an address is the address of a string (by looking to its
// reference count and length information). First version to be used with the
// pchar extracted by a string, the second with a pointer to a memory block
// including a string
function IsPointerToString (Address: Pointer): Boolean;
function IsPointerToStringBlock (Address: Pointer): Boolean;

implementation

function IsPointerToObject (Address: Pointer): Boolean;
var
  classPointer, vmtPointer: PChar;
  instsize: Integer;
begin
  Result := False;
  if (FindHInstance (Address) > 0) then
  begin
    vmtpointer := pchar(Address^);
    classpointer := vmtpointer + vmtSelfPtr;
    if Assigned (vmtpointer) and (FindHInstance (vmtpointer) > 0) then
    begin
      instsize := (PInteger(vmtpointer + vmtInstanceSize))^;
      // check self pointer and "reasonable" instance size
      if (pointer(pointer(classpointer)^) = pointer(vmtpointer)) and
          (instsize > 0) and (instsize < 10000) then
        Result := True;
    end;
  end;
end;

function IsPointerToString (Address: Pointer): Boolean;
begin
  Result := IsPointerToStringBlock (pChar(Address)-8);
end;

function IsPointerToStringBlock (Address: Pointer): Boolean;
var
  strlen: cardinal;
  strref: Integer;
  pch: PChar;
begin
  // NOTICE: this is rough!!!
  Result := False;
  if (FindHInstance (Address) > 0) then
  begin
    pch := Address;
    strlen := pinteger(pch+4)^;
    strref := pinteger(pch)^;
    pch := pch + 8;
    // is this a string?
    if ((strref >= -1) and (strref < 100)) and
        ((strlen < 1000000) or (strlen = sysutils.strlen (pch+8))) then
      Result := True;
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -