同样的代码为何在delphi7编译运行正常~而在Xe2会出错呢?
代码如下:
unit UnitUtil;interfaceuses System.SysUtils, System.Classes, Winapi.Windows;function HexToString(S:string):string;function HexScan(t:tstream; s:string):integer;function HexRead(t:tstream; s:DWORD; b:Byte):Integer;function ReadAddress(FlagCode:string;MyFile:TFileStream;Offset:Integer):string;function ReadOffsetAdress(FlagCode:string;MyFile:TFileStream;Offset:Integer;const nSize:Integer=4):string;function ReadCallAddress(FlagCode:string;MyFile:TFileStream;Offset,Modify:Integer):string;implementationfunction HexToString(S: string): string;//转十六进制字符串var i: integer; tmpstr:string;Begin Result := ''; for i := 1 to Length(S) do begin if ((i mod 2)=1) then begin tmpstr:=Copy(S, i, 2); if tmpstr='**' then Result:=Result+'*' else Result:=Result + Chr(StrToInt('0x' + Copy(S, i, 2))); end; end;end;function HexScan(t:tstream; s:string):integer; //找地址所在位置var i, j: integer; p: pchar;begin Result:=-1; getmem(p, t.size); // ---- 分配内存 t.readbuffer(p^, t.size ); // ---- 读 for i:=0 to t.size -1 do begin for j:=1 to length(s) do begin if s[j]='*' then //匹配通配符'*',强制下一轮循环 Continue; if p[i+j]<>s[j] then // ---- 有一个不同即退出 begin break; end; end; if j>length(s) then // ---- 依据 begin Result:=i+1; break; end; end; freemem(p); t.seek(0, soBeginning); // ---- 返回文件头end;function HexRead(t:tstream; s:DWORD; b:Byte):Integer;//读地址所在位置var d: DWORD;begin t.seek(s, soBeginning); // ---- 到Length位置; t.Read(d, b); // ---- 读b字节 Result:=d; t.seek(0, soBeginning); // ---- 返回文件头end;function ReadAddress(FlagCode:string;MyFile:TFileStream;Offset:Integer):string;var Addr:Integer;begin Addr := HexScan(MyFile, HexToString(FlagCode)); if Addr = -1 then Result := 'Null' else Result :='$' + IntToHex($400000 + Addr + Offset, 1);end;function ReadOffsetAdress(FlagCode:string;MyFile:TFileStream;Offset:Integer;const nSize:Integer=4):string;var Addr:Integer;begin Addr:=HexScan(MyFile,HexToString(FlagCode)); if Addr=-1 then Result:='Null' else Result:='$'+ IntToHex(HexRead(MyFile, Addr + Offset, nSize),1)end;function ReadCallAddress(FlagCode:string;MyFile:TFileStream;Offset,Modify:Integer):string;var Addr:Integer;begin Addr:=HexScan(MyFile,HexToString(FlagCode)); if Addr=-1 then Result:='Null' else Result := '$' + IntToHex(($400000 + Addr) + HexRead(MyFile, Addr+Offset, 4) + Modify, 1)end;end.unit UnitMain;interfaceuses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, UnitUtil;type TMainForm = class(TForm) edt_Path: TEdit; Label1: TLabel; btn_GetClient: TButton; dlgOpen: TOpenDialog; lv_Show_Addr: TListView; procedure btn_GetClientClick(Sender: TObject); private { Private declarations } public { Public declarations } end;var MainForm: TMainForm;implementation{$R *.dfm}procedure TMainForm.btn_GetClientClick(Sender: TObject);var MyFile: TFileStream; VerionFilePath:string; VerionFile:TextFile; Version:string; tmp_Addr:Integer;begin if not dlgOpen.Execute then Exit; edt_Path.Text := dlgOpen.FileName; VerionFilePath := ExtractFilePath(edt_Path.Text)+'..\'+'config\element\version.sw'; if FileExists(VerionFilePath) then begin AssignFile(VerionFile,VerionFilePath); Reset(VerionFile); Readln(VerionFile,Version); Self.Caption:=Self.Caption+'--客户端版本号:'+Copy(Version,1,3); end else begin Self.Caption:=Self.Caption+'--无法检测到客户端版本'; end; if edt_Path.Text<>'' then begin MyFile:=TFileStream.Create(edt_Path.Text,fmShareDenyNone); lv_Show_Addr.Items.Clear; try with lv_Show_Addr.Items.Add do begin Caption:='基址'; SubItems.Add(ReadOffsetAdress('53578b4820e8',MyFile,-$4)); end; except on Err:Exception do begin ShowMessage('异常类名称:'+Err.ClassName+#13#10+'异常信息'+Err.Message); end; end; end;end;end.
12,还是PChar
注意在2010中是这样的:
PChar= Pointer to a WideChar array;
PAnsiChar = Pointer to a AnsiChar array;
如果你还像是在Delphi 7中那样:PChar(AnsiString)那后果过是很严重的。
[解决办法]
HexScan里
for i := 0 to t.Size div 2 - 1 do
[解决办法]