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 div>
6
7
8
9
10
11
12
13
14
15
16 div>
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; < div class="line number11 index10 alt2"> begin InterfaceEntry := @InterfaceTable . Entries[i]; begin 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 div>
8
9
10
11
12
13
14
15
16
17
18
19
|
type TSomeObject= class ...... end ; var FItemObjectOffset:NativeInt; // Get offset FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset) < div class="line number12 index11 alt1"> 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 div>
10
11
12
13
14
15
16
17
18
|
type TSomeObject= class (TXXX, ISomeInterface) ...... end ; var //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 div>
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>
end ; < code class="delphi spaces">
end ; AClass := AClass . ClassParent; end ; end ; td> |
1
2
3
4
|
function FindInterface(AClass: TClass; GUID:TGUID; var Offset:NativeInt): Boolean ;< /code> var i : integer ; InterfaceTable: PInterfaceTable; InterfaceEntry: PInterfaceEntry; begin code> while Assigned(AClass) do InterfaceTable := AClass .< /code> if Assigned(InterfaceTable) then begin for i := 0 to InterfaceTable . EntryCount- 1 do begin if InterfaceEntry . IID=GUID then< /code> begin < div class="line number17 index16 alt2"> 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)); td> |
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));