稍微改了一下
procedure TForm1.WriteLn(const Buffer; Count: Integer);
var
P : PByteArray;
B : Byte;
C : Char;
S : String;
I : Integer;
J : Integer;
begin
P := @Buffer;
for I := 0 to (Count+15) div 16 -1 do
begin
S := Format(' %0.8X ', [I*$10]);
for J := 0 to $F do //每行以十六进制数显示16个字节
begin
if I*$10+J < Count then
begin
B := P^[I*$10+J];
if J = 7 then //第8个字符后显示一个分隔符
S := S + IntToHex(B, 2) + '-'
else
S := S + IntToHex(B, 2) + ' ';
end
else
S := S + ' '; //没有数据时以空格填充
end;
J := 0; //显示16个字节对应的字符
while J < $10 do
begin
C := ' '; //空格
if I*$10+J < Count then C := Chr(P^[I*$10+J]);
if isDBCSLeadByte(P^[I*$10+J]) and (I*$10+J+1 < Count) then //汉字
begin
S := S + C;
Inc(J);
C := Chr(P^[I*$10+J]);
S := S + C;
end
else
begin
if (C < ' ') or (C > '~') then C := '.'; //其他不可见字符以"."显示
S := S + C;
end;
Inc(J);
end;
Memo1.Lines.Add(S);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const MEMSIZE=100;
var
s:string;
hfilemap:hwnd;
pmem
ointer;
p
char;
c:char;
i:integer;
begin
hFileMap:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,memsize,'Share');
if hFileMap=0 then
begin
CloseHandle(hFileMap);
ShowMessage('打开内存映射文件错误');
end;
//读取
pmem:=MapViewOfFile(hFileMap,File_Map_All_Access,0,0,0);// 访问整个映象文件
if pMem=nil then
begin
CloseHandle(hFileMap);
UnmapViewOfFile(pMem);
ShowMessage('读取存映射文件错误');
end;
//放一些内容
s := '123456你好, 世界!654321';
Move(S[1], pMem^, Length(s));
////////////////////////////
Self.WriteLn(pMem^, MEMSIZE);
UnmapViewOfFile(pMem);
CloseHandle(hFileMap);
end;