Mar*_*adu 17 delphi record offset
我正在寻找获得Delphi记录中字段偏移的方法.以下两种方法有效,但我希望有一种更清洁的方式.基本上我会喜欢第三个showmessage工作.有任何想法吗?
type
rec_a=record
a:longint;
b:byte;
c:pointer;
end;
{$warnings off}
function get_ofs1:longint;
var
abc:^rec_a;
begin
result:=longint(@abc.c)-longint(abc);
end;
{$warnings on}
function get_ofs2:longint;
asm
mov eax,offset rec_a.c
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(inttostr(get_ofs1));
showmessage(inttostr(get_ofs2));
// showmessage(inttostr(longint(addr(rec_a.c)))); // is there a way to make this one work?
end;
Run Code Online (Sandbox Code Playgroud)
编辑:好的,下面的答案很好,谢谢!作为参考,这里是各种选项的汇编输出:
---- result:=longint(@abc.c)-longint(abc); ----
lea edx,[eax+$08]
sub edx,eax
mov eax,edx
---- mov eax,offset rec_a.c ----
mov eax,$00000008
---- result:=longint(@rec_a(nil^).c); ----
xor eax,eax
add eax,$08
Run Code Online (Sandbox Code Playgroud)
edit2:看起来这是前一个问题的重复:RRUZ下面提到的类似问题.如图所示,另一种方法是声明一个全局变量并按如下方式使用它.奇怪的是,编译器仍然无法在编译时分配正确的值,如汇编器输出中所示,因此为了效率和可读性,最好使用nil方法.
---- var ----
---- rec_a_ofs:rec_a; ----
---- ... ----
---- result:=longint(@rec_a_ofs.c)-longint(@rec_a_ofs); ----
mov eax,$0045f5d8
sub eax,$0045f5d0
Run Code Online (Sandbox Code Playgroud)
edit3:修改后的代码,用所有已知的方法来完成此任务.请注意,为第3,第4和第5(类方法)方式生成的汇编程序代码是相同的,无论它们是否内联.当你做这些事情时,选择你最喜欢的方式!
type
prec_a=^rec_a;
rec_a=record
a:longint;
b:byte;
c:pointer;
class function offset_c:longint;static;inline;
end;
//const
// rec_a_field_c_offset=longint(@rec_a(nil^).c); // no known way to make this work
{$warnings off}
function get_ofs1:longint;inline;
var
abc:^rec_a;
begin
result:=longint(@abc.c)-longint(abc);
end;
{$warnings on}
function get_ofs2:longint;
asm
mov eax,offset rec_a.c
end;
function get_ofs3:longint;inline;
begin
result:=longint(@rec_a(nil^).c);
end;
function get_ofs4:longint;inline;
begin
result:=longint(@prec_a(nil).c);
end;
class function rec_a.offset_c:longint;
begin
result:=longint(@prec_a(nil).c);
end;
var
rec_a_ofs:rec_a;
function get_ofs6:longint;inline;
begin
result:=longint(@rec_a_ofs.c)-longint(@rec_a_ofs);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(inttostr(get_ofs1));
showmessage(inttostr(get_ofs2));
showmessage(inttostr(get_ofs3));
showmessage(inttostr(get_ofs4));
showmessage(inttostr(rec_a.offset_c));
showmessage(inttostr(get_ofs6));
// showmessage(inttostr(rec_a_field_c_offset));
end;
Run Code Online (Sandbox Code Playgroud)
Dav*_*nan 18
我总是使用这种方法:
Offset := Integer(@rec_a(nil^).c);
Run Code Online (Sandbox Code Playgroud)
不要让nil^你的使用让你失望,这是非常安全的.并且不用担心64位指针截断.如果你有一个大小超过4GB的记录,你就会遇到更大的问题!