其实Delphi为了速度的原因,对于接口是采用直接解析接口在实例中的偏移来得到的.这个可以从下面的代码中看到:
其中 mInt := TTestInterface2.Create 代码为:
而 mInt2 := TTestInterface2.Create 代码为:
但 对于接口之间的转化时, CSDN的halfdream(哈欠) 兄说的很对, Delphi在call @IntfCast里调用了@QueryInterface, 而这里面的代码, 其实是在System的TObject对象的GetInterface这个方法中, 但这个方法有个问题, 它需要GUID, 但我们实际可以在Delphi中使用无GUID的接口, 那么就会发现, 根本无法进行转化
比如:
编译就会出错了, Operator not applicable to this operand type 根本就不支持. 所以表明接口时还是随手加上GUID的比较好
var
mInt: ITestInterface;
mInt2: ITestInterface2;
begin
mInt : = TTestInterface2.Create;
mInt2 : = TTestInterface2.Create;
end;
mInt: ITestInterface;
mInt2: ITestInterface2;
begin
mInt : = TTestInterface2.Create;
mInt2 : = TTestInterface2.Create;
end;
其中 mInt := TTestInterface2.Create 代码为:
mov dl, $
01
mov eax, [$00452e4]
call TObject.Create
mov edx, eax
jz + $ 03
sub edx, - $0C
lea eax, [ebp - $ 04 ]
call @IntfCopy
mov eax, [$00452e4]
call TObject.Create
mov edx, eax
jz + $ 03
sub edx, - $0C
lea eax, [ebp - $ 04 ]
call @IntfCopy
而 mInt2 := TTestInterface2.Create 代码为:
mov dl, $
01
mov eax, [$00452e4]
call TObject.Create
mov edx, eax
jz + $ 03
sub edx, - $0C
lea eax, [ebp - $ 08 ]
call @IntfCopy
mov eax, [$00452e4]
call TObject.Create
mov edx, eax
jz + $ 03
sub edx, - $0C
lea eax, [ebp - $ 08 ]
call @IntfCopy
但 对于接口之间的转化时, CSDN的halfdream(哈欠) 兄说的很对, Delphi在call @IntfCast里调用了@QueryInterface, 而这里面的代码, 其实是在System的TObject对象的GetInterface这个方法中, 但这个方法有个问题, 它需要GUID, 但我们实际可以在Delphi中使用无GUID的接口, 那么就会发现, 根本无法进行转化
比如:
INoGUIDInterface
=
interface
procedure NoGUID;
end;
INoGUIDInterface2 = interface
procedure NoGUID2;
end;
var
mInt: INoGUIDInterface;
mInt2: INoGUIDInterface2;
begin
mInt : = TTestNoGUIDInterface2.Create;
mInt.NoGUID;
mInt2 : = mInt as INoGUIDInterface2;
mInt2.NoGUID2;
end;
procedure NoGUID;
end;
INoGUIDInterface2 = interface
procedure NoGUID2;
end;
var
mInt: INoGUIDInterface;
mInt2: INoGUIDInterface2;
begin
mInt : = TTestNoGUIDInterface2.Create;
mInt.NoGUID;
mInt2 : = mInt as INoGUIDInterface2;
mInt2.NoGUID2;
end;
编译就会出错了, Operator not applicable to this operand type 根本就不支持. 所以表明接口时还是随手加上GUID的比较好