View Full Version : Getting GUID from an interface
NecroDOME
24-04-2009, 11:03 AM
Hi all,
I have an interface.
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:
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.
chronozphere
24-04-2009, 12:19 PM
I just googled and found this. Seems a little hackish though:
http://hallvards.blogspot.com/2006/09/hack11-get-guid-of-interface-reference.html (http://hallvards.blogspot.com/2006/09/hack11-get-guid-of-interface-reference.html)
Where do you need the GUID for?
NecroDOME
24-04-2009, 12:27 PM
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.
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:
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;
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 :(
NecroDOME
25-04-2009, 06:10 PM
Thanks :)
That does the trick!
NecroDOME
26-04-2009, 03:25 PM
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.