加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Delphi中CONTAINING_RECORD C宏的等效函数是什么?

发布时间:2020-12-15 09:32:09 所属栏目:大数据 来源:网络整理
导读:C中的 CONTAINING_RECORD macro基于结构内的字段成员的地址返回结构/记录类型变量的基址.在我只能将预定义的记录指针传递给某些触发回调的Windows API函数的情况下,它非常有用. 例如,如果我有一些类型,如: type tInnerRecord = record x,y : integer; end;
C中的 CONTAINING_RECORD macro基于结构内的字段成员的地址返回结构/记录类型变量的基址.在我只能将预定义的记录指针传递给某些触发回调的Windows API函数的情况下,它非常有用.

例如,如果我有一些类型,如:

type
   tInnerRecord = record
      x,y : integer;
   end;
   pInnerRecord = ^tInnerRecord

tOuterRecord = record
   field1 : integer;
   inner : tInnerRecord;
   field2 : integer;
end;
pOuterRecord = ^tOuterRecord;

我希望能够做到这样的事情:

procedure SomeCallback( pIn : pInnerRecord ); stdcall;
var
   Out : pOuterRecord;
begin
   Out := CONTAINING_RECORD(pIn,tOuterRecord,inner);
   Out.field1 := pIn.x + pIn.y;
end;

在我的特定情况下,我想传递我的对象指针以及ReadFileEx(Windows异步I / O)的重叠数据指针,以便我可以访问回调中的对象.

在Delphi(2006)中是否有一些提供类似功能的等效功能?

解决方法

我刚才没有编译器,但这应该可以解决问题:

Out := Pointer( Cardinal(pIn) - Cardinal(@TOuterRecord(nil^).inner));

David解释了为什么在Delphi中没有直接的等效函数.
所以这是一个最接近的函数:

function ContainingRecord( var aField; aFieldOffsetRef : Pointer) : Pointer;
{$IF Declared(NativeUInt) = False}
type
  NativeUInt = Cardinal;
{$IFEND}
begin
  Result := Pointer(NativeUInt(@aField) - NativeUInt(aFieldOffsetRef));
end;

调用示例:

Out := ContainingRecord(pIn^,@pOuterRecord(nil).inner);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读