Delphi judges whether a class implements an interface

TObject.GetInterface can obtain an instance of an object to implement an interface. The prerequisite is that the object must be instantiated before running GetInterface

The following method can obtain whether the class implements an interface , And return the offset of the interface:

1
2
3
4
5

6
7
8
9
10
11
12
13
14
15
16

17
18
19
20
21
22
23
24
function FindInterface(AClass: TClass; GUID:TGUID; var Offset:NativeInt): Boolean ;
var
i : integer ;
InterfaceTable: PInterfaceTable;
InterfaceEntry: PInterfaceEntry;
begin
while Assigned(AClass) do
begin
InterfaceTable := AClass < code class="delphi value ">. GetInterfaceTable;
if Assigned(InterfaceTable) then

< div class="line number11 index10 alt2"> begin

for i := 0 to InterfaceTable . EntryCount- 1 do
begin
InterfaceEntry := @InterfaceTable . Entries[i];
if InterfaceEntry code> . IID=GUID then
begin
Offset:=InterfaceEntry . IOffset;
Exit( True< /code> );
end ;
code> end ;
end ;
AClass := AClass . ClassParent;
end ;
end ;

  Let’s take a look at the speed of passing the offset Quickly obtain the object interface, and quickly obtain the object through the interface:

Quickly obtain the object interface:

1
2
3
4
5
6
7

8
9
10
11
12
13
14
15
16
17
18
19
type
TSomeObject= class (TXXX, ISomeInterface)
......
end ;
var
FItemObjectOffset:NativeInt;
// Get offset
FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset)

< div class="line number12 index11 alt1"> var

code> P:TSomeObject;
Intf:ISomeInterface;
............ ........................
//Directly through the object Get interface
PNativeInt(@Intf)^ :=PNativeInt(@P)^+ FItemObjectOffset;
Intf . _AddRef;

  

Get objects quickly through the interface

1
2
3
4
5
6
7
8
9

10
11
12
13
14
15
16
17
18
type
TSomeObject= class (TXXX, ISomeInterface)
......
end ;
var
/code> FItemObjectOffset:NativeInt;
//Get offset
FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset )
var < /div>

P:TSomeObject;
Intf:ISomeInterface;
................................... .
//Get objects through the interface
P:=TSomeObject( Pointer (PNativeInt(@Intf)^- FItemObjectOffset));

https://www.cnblogs.com/hezihang/p/5735897.html

< /p>

1
2
3
4 < /div>

5
6
7
8
9
10

< div class="line number11 index10 alt2"> 11

12
13
14
15
16

17
18
19
20
21
22
23
24
function FindInterface(AClass: T Class; GUID:TGUID; var Offset:NativeInt): Boolean ;
var < /div>

i : integer ;
InterfaceTable: PInterfaceTable;
InterfaceEntry: PInterfaceEntry;
begin
while Assigned(AClass) do
begin
InterfaceTable := AClass . GetInterfaceTable;
if < code class="delphi plain">Assigned(InterfaceTable) then
begin
for i := 0 to InterfaceTable . EntryCount- 1 do
begin< /code>
InterfaceEntry := @InterfaceTable . Entries[ i];
if < code class="delphi plain">InterfaceEntry . IID=GUID then
begin < /div>

Offset:=InterfaceEntry . IOffset;
Exit( True );
end ;
end ;
< code class="delphi spaces"> end ;
AClass := AClass . ClassParent;
end ;
end ;

1
2
3
4

5

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function FindInterface(AClass: TClass; GUID:TGUID; var Offset:NativeInt): Boolean ;< /code>
var
i : integer ;
InterfaceTable: PInterfaceTable;
InterfaceEntry: PInterfaceEntry;
begin code>
while Assigned(AClass) do
/code> begin
InterfaceTable := AClass .< /code> GetInterfaceTable;
if Assigned(InterfaceTable) then
begin
for i := 0 to InterfaceTable . EntryCount- 1 do
begin
InterfaceEntry := @InterfaceTable< /code> . Entries[i];
if InterfaceEntry . IID=GUID then< /code>
begin

< div class="line number17 index16 alt2">            Offset:=InterfaceEntry . IOffset;

           Exit( True );
         end ;
       end ;
     end ;
     AClass := AClass . ClassParent;
   end ;
end ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function  FindInterface(AClass: TClass; GUID:TGUID;  var  Offset:NativeInt): Boolean ;
var
   i :  integer ;
   InterfaceTable: PInterfaceTable;
   InterfaceEntry: PInterfaceEntry;
begin
   while  Assigned(AClass)  < code class="delphi keyword">do
   begin
     InterfaceTable := AClass . GetInterfaceTable;
     if  Assigned(InterfaceTable)  then
     begin
       for  i :=  0  to  InterfaceTable . EntryCount- 1  do
       begin
         InterfaceEntry := @InterfaceTable . Entries[i];
         if  InterfaceEntry . IID=GUID  then
         begin
           Offset:=InterfaceEntry . IOffset;
           Exit( True );
         end ;
       end ;
     end ;
     AClass := AClass . ClassParent;
   end ;
end ;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

function  FindInterface(AClass: TClass; GUID:TGUID;  var  Offset:NativeInt): Boolean ;
var
   i :  integer ;
   InterfaceTable: PInterfaceTable;
   InterfaceEntry: PInterfaceEntry;
begin
   while  Assigned(AClass)  do
   begin
     InterfaceTable := AClass . GetInterfaceTable;
     if  Assigned(InterfaceTable)  then
     begin
       for  i :=  0  to  InterfaceTable . EntryCount- 1  do
       begin
         InterfaceEntry := @InterfaceTable . Entries[i];
         if  InterfaceEntry . IID=GUID  then
         begin
           Offset:=InterfaceEntry . IOffset;
           Exit( True );
         end ;
       end ;
     end ;
     AClass := AClass . ClassParent;
   end ;
end ;

function  FindInterface(AClass: TClass; GUID:TGUID;  var  Offset:NativeInt): Boolean ;

var

   i :  integer ;

   InterfaceTable: PInterfaceTable;

   InterfaceEntry: PInterfaceEntry;

begin

   while  Assigned(AClass)  do

< code class="delphi spaces">   begin

     InterfaceTable := AClass . GetInterfaceTable;

     if  Assigned(InterfaceTable)  then

     begin

       for  i :=  0  to  InterfaceTable . EntryCount- 1  do

       begin

         InterfaceEntry := @InterfaceTable . Entries[i];

         if  InterfaceEntry . IID=GUID  then

         begin

           Offset:=InterfaceEntry . IOffset;

           Exit( True );

         end ;

       end ;

     end ;

     AClass := AClass . ClassParent;

   end ;

end ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
type
   TSomeObject= class (TXXX, ISomeInterface)
   ......
   end ;
 
var
   FItemObjectOffset:NativeInt;
 
//获取偏移量
FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset)
 
var
   P:TSomeObject;
   Intf:ISomeInterface;
....................................
 
//通过对象直接获取接口
     PNativeInt(@Intf)^:=PNativeInt(@P)^+ FItemObjectOffset;
     Intf . _AddRef;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
type
   TSomeObject= class (TXXX, ISomeInterface)
   ......
   end ;
 
var
   FItemObjectOffset:NativeInt;
 
//获取偏移量
FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset)
 
var
   P:TSomeObject;
   Intf:ISomeInterface;
....................................
 
//通过对象直接获取接口
     PNativeInt(@Intf)^:=PNativeInt(@P)^+ FItemObjectOffset;
     Intf . _AddRef;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
type
   TSomeObject= class (TXXX, ISomeInterface)
   ......
   end ;
 
var
   FItemObjectOffset:NativeInt;
 
//获取偏移量
FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset)
 
var
   P:TSomeObject;
   Intf:ISomeInterface;
....................................
 
//通过对象直接获取接口
     PNativeInt(@Intf)^:=PNativeInt(@P)^+ FItemObjectOffset;
     Intf . _AddRef;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

type
   TSomeObject= class (TXXX, ISomeInterface)
   ......
   end ;
 
var
   FItemObjectOffset:NativeInt;
 
//获取偏移量
FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset)
 
var
   P:TSomeObject;
   Intf:ISomeInterface;
....................................
 
//通过对象直接获取接口
     PNativeInt(@Intf)^:=PNativeInt(@P)^+ FItemObjectOffset;
     Intf . _AddRef;

type

   TSomeObject= class (TXXX, ISomeInterface)

   ......

   end ;

 

var

   FItemObjectOffset:NativeInt;

 

//获取偏移量

FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset)

 

var

   P:TSomeObject;

   Intf:ISomeInterface;

....................................

 

//通过对象直接获取接口

     PNativeInt(@Intf)^:=PNativeInt(@P)^+ FItemObjectOffset;

     Intf . _AddRef;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
type
   TSomeObject= class (TXXX, ISomeInterface)
   ......
   end ;
 
var
   FItemObjectOffset:NativeInt;
 
//获取偏移量
FindInterface(TSomeObject, I SomeInterface, FItemObjectOffset)
 
var
   P:TSomeObject;
   Intf:ISomeInterface;
....................................
 
//通过接口获取对象
   P:=TSomeObject( Pointer (PNativeInt(@Intf)^- FItemObjectOffset));

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
type
   TSomeObject= class (TXXX, ISomeInterface)
   ......
   end ;
 
var
   FItemObjectOffset:NativeInt;
 
//获取偏移量
FindInt erface(TSomeObject, ISomeInterface, FItemObjectOffset)
 
var
   P:TSomeObject;
   Intf:ISomeInterface;
....................................
 
//通过接口获取对象
   P:=TSomeObject( Pointer (PNativeInt(@Intf)^- FItemObjectOffset));

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
type
   TSomeObject= class (TXXX, ISomeInterface)
   ......
   end ;
 
var
   FItemObjectOffset:NativeInt;
 
//获取偏移量
FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset)
 
var
   P:TSomeObject;
   Intf:ISomeInterface;
....................................
 
//通过接口获取对象
   P:=TSomeObject( Pointer (PNativeInt(@Intf)^- FItemObjectOffset));

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

type
   TSomeObject= class (TXXX, ISomeInterface)
   ......
   end ;
 
var
   FItemObjectOffset:NativeInt;
 
//获取偏移量
FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset)
 
var
   P:TSomeObject;
   Intf:ISomeInterface;
....................................
 
//通过接口获取对象
   P:=TSomeObject( Pointer (PNativeInt(@Intf)^- FItemObjectOffset));

type

   TSomeObject= class (TXXX, ISomeInterface)

   ......

   end ;

 

var

   FItemObjectOffset:NativeInt;

 

//获取偏移量

FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset)

 

var

   P:TSomeObject;

   Intf:ISomeInterface;

....................................

 

//通过接口获取对象

   P:=TSomeObject( Pointer (PNativeInt(@Intf)^- FItemObjectOffset));

Leave a Comment

Your email address will not be published.