Examples TObject.GetInterface object can be obtained through the implementation of an interface, a prerequisite in order to run must instantiate an object GetInterface
The following method can be obtained whether the class implements an interface, and returns the offset 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
.
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
;
|
Here we look at an object interface to quickly get through offsets, as well as quick access to the object through the interface:
Quick access to object interfaces:
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;
|
Quickly get the object 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
FItemObjectOffset:NativeInt;
//获取偏移量
FindInterface(TSomeObject, ISomeInterface, FItemObjectOffset)
var
P:TSomeObject;
Intf:ISomeInterface;
....................................
//通过接口获取对象
P:=TSomeObject(
Pointer
(PNativeInt(@Intf)^- FItemObjectOffset));
|