小编yan*_*eke的帖子

Delphi(Pascal)检查是否分配了每个字段

我在Delphi中使用windows dll,我必须检查我的功能是否分配得很好.

我声明了函数类型,以便将我的dll函数放在类属性中,如下所示:

type
  MPOS_OpenResource = function (ResID: DWORD; CplNum:BYTE; BlockingMode: DWORD):WORD;stdcall;
  MPOS_CloseResource = function (ResID: DWORD; CplNum:BYTE):WORD;stdcall;
  MPOS_GetResourceID = function (CplNum : Byte; ResID : PDWord) : word;stdcall;
  ...
Run Code Online (Sandbox Code Playgroud)

然后,我为我的dll类中的每个相应字段分配一个方法,如下所示:

@Self.m_MPOS_OpenResource := GetProcAddress( libHandler, '_MPOS_OpenResource@12' );
@Self.m_MPOS_CloseResource := GetProcAddress( libHandler, '_MPOS_CloseResource@8' );
@Self.m_MPOS_GetResourceID := GetProcAddress( libHandler, '_MPOS_GetResourceID@8');
...
Run Code Online (Sandbox Code Playgroud)

我最后检查每个分配是否使用了一个巨大的if条款:

If(not Assigned(@m_MPOS_OpenResource) OR
  not Assigned(@m_MPOS_CloseResource) OR
  not Assigned(@m_MPOS_GetResourceID) OR 
...) then { Some code for exception}
Run Code Online (Sandbox Code Playgroud)

我想避免if使用反射的giantic 子句,但我找不到有用的东西.我尝试了很多东西,最后一个开始:

for f in rttiType.GetFields() do 
  if(not Assigned(rttiType.GetField(f.Name).GetValue(Self)) 
      OR …
Run Code Online (Sandbox Code Playgroud)

delphi dll pascal rtti

2
推荐指数
1
解决办法
139
查看次数

标签 统计

delphi ×1

dll ×1

pascal ×1

rtti ×1