Advertisement
sglienke

Get GUID from interface reference

Aug 19th, 2016
172
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function GetInterfaceEntry(const I: IInterface): PInterfaceEntry;
  2. var
  3.   Instance: TObject;
  4.   InterfaceTable: PInterfaceTable;
  5.   j: integer;
  6.   CurrentClass: TClass;
  7. begin
  8.   Instance := I as TObject;
  9.   if Assigned(Instance) then
  10.   begin
  11.     CurrentClass := Instance.ClassType;
  12.     while Assigned(CurrentClass) do
  13.     begin
  14.       InterfaceTable := CurrentClass.GetInterfaceTable;
  15.       if Assigned(InterfaceTable) then
  16.         for j := 0 to InterfaceTable.EntryCount-1 do
  17.         begin
  18.           Result := @InterfaceTable.Entries[j];
  19.           if Result.IOffset <> 0 then
  20.           begin
  21.             if Pointer(NativeInt(Instance) + Result^.IOffset) = Pointer(I) then
  22.               Exit;
  23.           end;
  24.           // TODO: implement checking interface implemented via implements delegation
  25.           // see System.TObject.GetInterface/System.InvokeImplGetter
  26.         end;
  27.       CurrentClass := CurrentClass.ClassParent
  28.     end;
  29.   end;
  30.   Result := nil;
  31. end;
  32.  
  33. function GetInterfaceIID(const I: IInterface; var IID: TGUID): boolean;
  34. var
  35.   InterfaceEntry: PInterfaceEntry;
  36. begin
  37.   InterfaceEntry := GetInterfaceEntry(I);
  38.   Result := Assigned(InterfaceEntry);
  39.   if Result then
  40.     IID := InterfaceEntry.IID;
  41. end;
Advertisement
RAW Paste Data Copied
Advertisement