Marc Weustink het geskryf:
> 
> or functions like supports() (which uses queryinterface)

Actually, that *does* work with FPC 2.4.0.  See attached console application.

I used supports() and a cast to get a Corba interface reference.
It seems the compiler does some extra magic (probably with the GUID) to
identify the interface via the Supports() call - without the need for
QueryInterface.


begin
 ins := TAddCommand.Create;
 if Supports(ins, ICommand, cmd) then
 begin
   writeln('1. It worked');
   cmd.Execute;               // works
 end;

 cmd := nil;
 cmd := ins as ICommand;
 if Assigned(cmd) then
 begin
   writeln('2. It worked');
   cmd.Execute;                // works
 end;

 ins.Free;
end.

....and the the output generated...

1. It worked
TAddCommand.Execute
2. It worked
TAddCommand.Execute




Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

program interface_test;
{$ifdef FPC}{$mode objfpc}{$h+}{$endif}
{$ifdef mswindows}{$apptype console}{$endif}
{$Interfaces Corba}
uses
 sysutils;
 
type
 ICommand = interface
 ['{28D72102-D883-41A1-9585-D86B24D9C628}']
   procedure   Execute;
 end;


 TAddCommand = class(TObject, ICommand)
 public
   procedure   Execute;
 end;
 
 
procedure TAddCommand.Execute;
begin
  writeln(Classname + '.Execute');
end;



var
 cmd: ICommand;
 ins: TAddCommand;
begin
 ins := TAddCommand.Create;
 if Supports(ins, ICommand, cmd) then
 begin
   writeln('1. It worked');
   cmd.Execute;
 end;
 
 cmd := nil;
 cmd := ins as ICommand;
 if Assigned(cmd) then
 begin
   writeln('2. It worked');
   cmd.Execute;
 end;
   
 ins.Free;
end.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to