C中的CONTAINING_RECORD宏根据结构中字段成员的地址返回结构/记录类型变量的基址.在我只能将预定义的记录指针传递给某些触发回调的Windows API函数的情况下,它非常有用.
例如,如果我有一些类型,如:
type
tInnerRecord = record
x, y : integer;
end;
pInnerRecord = ^tInnerRecord
tOuterRecord = record
field1 : integer;
inner : tInnerRecord;
field2 : integer;
end;
pOuterRecord = ^tOuterRecord;
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这样的事情:
procedure SomeCallback( pIn : pInnerRecord ); stdcall;
var
Out : pOuterRecord;
begin
Out := CONTAINING_RECORD(pIn, tOuterRecord, inner);
Out.field1 := pIn.x + pIn.y;
end;
Run Code Online (Sandbox Code Playgroud)
在我的特定情况下,我想传递我的对象指针以及ReadFileEx(Windows异步I/O)的重叠数据指针,以便我可以访问回调中的对象.
在Delphi(2006)中是否有一些提供类似功能的等效功能?
我想得到一个结构/记录的"位置".
说我有这个记录:
type
MyStruct = record
MyInteger : Integer;
MyInteger2 : Integer;
MyInteger3 : Integer;
MyFunc : function (FirstParam : WideString; SecondParam : String) : Integer;
MyString : String;
MyString2 : WideString;
MyPchar : pchar;
end;
Run Code Online (Sandbox Code Playgroud)
如您所见,此记录的大小为28字节(7个字符x 4个字节).基本上因为所有变量都是4字节变量(如整数)或指针(也是4字节).现在假设我们将这个结构加载到一个内存地址(X = 0)(这也意味着MyInteger的地址为0).MyInteger3的地址(例如)将为8(请注意X = 0!)如何动态获取结构的位置(数字/地址)?
希望你们知道我的意思吗?提前致谢.
BTW:结构中任何Var总是4个字节吗?编辑:如果你修复spcae这是错误的:String[100]