I would like to get information of an interface reference.
The IDE can display for example 'TMyObject($5864933A) as IMyInterface' when I move the mouse over an interface reference while debugging and I would like to print out something similar of my references (which seem to go haywire).
So, basically, I would like to call
type
IMyInterface = interface
['{ABDA7685-DB67-43C1-947F-4B9535142355}']
end;
TMyObject = class(TInterfacedObject, IMyInterface)
end;
var
T: PTypeInfo;
I: IMyInterface;
begin
I := TMyObject.Create;
T := TypeInfo(I);
...
and use the TypeInfo to find out more about the interface type.
In real world, 'I' would be just any interface pointer. Since TypeInfo requires a type and not an instance, this is not possible.
So, I tried to use the old hack by Hallvard as described at http://hallvards.blogspot.com/2006/09/hack11-get-guid-of-interface-reference.html
That would give me the IID, which I could then use to fetch more information. However, while running the code in Delphi 10.2, it doesn't seem to work any more.
First problem I encountered is that when I call the following method:
function GetInterfaceIID(const I: IInterface; var IID: TGUID): boolean;
var
InterfaceEntry: PInterfaceEntry;
begin
InterfaceEntry := GetInterfaceEntry(I);
Result := Assigned(InterfaceEntry);
if Result then
IID := InterfaceEntry.IID;
end;
the reference 'I' is always 'IInterface' no matter with which variable I call the method.
Second, the test application
var
MyInterface: IMyInterface;
Unknown: IUnknown;
Instance: TObject;
IID: TGUID;
begin
MyInterface := TMyObject.Create;
// Instance := GetImplementingObject(MyInterface); // not necessary since D2010
// Writeln(Instance.ClassName);
if GetInterfaceIID(MyInterface, IID) then // Results in Access Violation
writeln('MyInterface IID = ', GUIDToString(IID));
...
gives me an access violations.
Apparently, the details of the class and interface internals have changed since 2006.
So could anyone provide a working version of that code or some other means to get out information about the interface reference?
E: Clarified the target and what fails
GetImplementingObject()
hack anymore, anIInterface
can be type-casted directly to its implementingTObject
, eg:Writeln((MyInterface as TObject).ClassName);
– Remy Lebeau