delphi 如何读取内存中的数据?


unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls;

type
 TForm1 = class(TForm)
 Button1: TButton;
 Button2: TButton;
 procedure Button1Click(Sender: TObject);
 procedure Button2Click(Sender: TObject);
 private
 { Private declarations }
 public
 { Public declarations }
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}
 {用十六进制查看内存的函数; 参数1是内存起点, 参数2是以字节为单位的长度}
function ToHex(P:PByteArray; Bit: Integer):string;
var
 i: Integer;
begin
 for i := 0 to Bit - 1 do
 Result := IntToHex(P^[i],2)+Chr(32)+Result;
 Result := TrimRight(Result);

end;

{用二进制查看内存的函数; 参数1是内存起点, 参数2是以字节为单位的长度}
function ToBin(p:PByteArray;bit:Integer):string;
const
 Convert: array['0'..'F'] of string = (
 '0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001',
 '', '', '', '', '', '', '', '1010', '1011', '1100', '1101', '1110', '1111');
var
 i: Integer;
 s: string;
begin
 S:= ToHex(p,Bit);
 for I := 1 to Length(s) do
 if s[i]<>chr(32) then
 Result := Result + Convert[s[i]]
 else
 Result:= Result + chr(32);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 Num: Integer;
begin
 Randomize;
 Num:= Random(MaxInt);
 ShowMessage(IntToStr(num)+ #10#13#10#13+ToHex(@num,4)+#10#13#10#13+ToBin(@num,4));
end;

procedure TForm1.Button2Click(Sender: TObject);
var
 str: string;
begin
 str:= 'I Like Delphi';
 ShowMessage(str + #10#13#10#13 +
 ToHex(@str[1], Length(str)*SizeOf(str[1])) + #10#13#10#13 +
 ToBin(@str[1], Length(str)*SizeOf(str[1])));;
end;

end.

联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678