Results 1 to 7 of 7

Thread: Getting GUID from an interface

  1. #1
    Legendary Member NecroDOME's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands, Eindhoven
    Posts
    1,059

    Getting GUID from an interface

    Hi all,

    I have an interface.

    Code:
    type
     IMyInterface = interface(IInterface)
      ['{BCDDF1B6-73CC-406C-912F-7148095F1F4C}']
     end;
    How can I retrieve the GUID (BCDDF1B6-73CC-406C-912F-7148095F1F4C) from this interface without creating an instance?? I really DON'T want to create an instance. What I want is this:

    Code:
    function GetInterfaceByGUID(cosnt i: IInterface): TGUID;
    begin
     Result := {do some magic} I.IID;
    end;
    But this doesn't work. I know I have to do something manually, but where I look at the net, everywhere they construct an object. Which I don't want.

    Using Delphi 2007
    using Windows XP and up.
    NecroSOFT - End of line -

  2. #2

    Re: Getting GUID from an interface

    I just googled and found this. Seems a little hackish though:

    http://hallvards.blogspot.com/2006/09/hack11-get-guid-of-interface-reference.html


    Where do you need the GUID for?
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  3. #3
    Legendary Member NecroDOME's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands, Eindhoven
    Posts
    1,059

    Re: Getting GUID from an interface

    I already took a look at that page. It seems they also create an instance before retrieving the GUI.

    I need it for dynamic instancing. Searching trough interfaces.
    NecroSOFT - End of line -

  4. #4

    Re: Getting GUID from an interface

    I'm not sure how useful this will be (i usually get the concept wrong ). It's not dynamic, but maybe some of the "professionals" on this site can do something with it.

    This produces the GUID for a hard coded interface and as it just checks the interface Type, there's no need for any instancing to be done:
    [pascal]

    uses
    TypInfo;

    function GetGUID: string;
    var
    pinfo: PTypeInfo;
    pdata: PTypeData;
    begin
    pinfo:=TypeInfo(IMyInterface);
    if pinfo<>nil then begin
    pdata:=GetTypeData(pinfo);
    Result:=pinfo.name+': '+GUIDtoString(pdata.Guid);
    end
    else Result:='No GUID';
    end;
    [/pascal]

    Like i said, this won't work dynamically, and i couldn't find a way (yet) of passing anything other than a type. I tried typecasting and all sorts
    Windows Vista x64 Ultimate<br />AMD Athlon x64 4000+<br />Codegear Delphi 2009, Delphi 2007, Borland Delphi 7

  5. #5
    Legendary Member NecroDOME's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands, Eindhoven
    Posts
    1,059

    Re: Getting GUID from an interface

    Thanks

    That does the trick!
    NecroSOFT - End of line -

  6. #6

    Re: Getting GUID from an interface

    You're welcome
    Windows Vista x64 Ultimate<br />AMD Athlon x64 4000+<br />Codegear Delphi 2009, Delphi 2007, Borland Delphi 7

  7. #7
    Legendary Member NecroDOME's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands, Eindhoven
    Posts
    1,059

    Re: Getting GUID from an interface

    It would be nicer if TypeInfo would check stuff run time instead of compile time. But I worked around that.

    I just finished my implementation with it.
    I can now say to my game engine: I have an interface ITestInterface.
    My engine says: Ok, that's good, let me see if I can construct any registered stuff with that.
    If yes, I get back an instance of a new object that implements that interface.

    This goes over DLL boundaries.
    NecroSOFT - End of line -

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •