我要写一个替换二进制文件中字符串的程序,由于目标文件可能比较大,在20m左右,所以打算用内存映射,但是我不清楚如何读取指定地址的n个字符(n由EDIT读入),存为String,望高手指导procedure TForm1.Button1Click(Sender: TObject);
var
h:Cardinal;
p,p1:Pointer;
ch: array[0..10] of char;
fs:TFileStream;
begin
if open.Execute then fs:=TFileStream.Create(open.FileName ,fmOpenReadWrite or fmShareExclusive);
try
h := CreateFileMapping(fs.Handle, nil, PAGE_READWRITE,0, 0,nil);
if (h = INVALID_HANDLE_VALUE) or (h = 0) then
raise Exception.Create('Error!')
else
begin
try
p := MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, 0);
*** try
p1:=(PChar(p)+$3A1C32);
copymemory(@ch,p1,20); showmessage(inttohex(ord(ch[strtoint(edit1.Text)]),8));
finally
UnMapViewOfFile(p);
*** end;
finally
CloseHandle(h);
end;
end;
finally
fs.Free;
end;其中加*的部分应该如何改进???
var
h:Cardinal;
p,p1:Pointer;
ch: array[0..10] of char;
fs:TFileStream;
begin
if open.Execute then fs:=TFileStream.Create(open.FileName ,fmOpenReadWrite or fmShareExclusive);
try
h := CreateFileMapping(fs.Handle, nil, PAGE_READWRITE,0, 0,nil);
if (h = INVALID_HANDLE_VALUE) or (h = 0) then
raise Exception.Create('Error!')
else
begin
try
p := MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, 0);
*** try
p1:=(PChar(p)+$3A1C32);
copymemory(@ch,p1,20); showmessage(inttohex(ord(ch[strtoint(edit1.Text)]),8));
finally
UnMapViewOfFile(p);
*** end;
finally
CloseHandle(h);
end;
end;
finally
fs.Free;
end;其中加*的部分应该如何改进???
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货