Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2019-12

Passing a CORBA interface as parameter

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Adriaan van Os

I would like to pass a CORBA interface-type as parameter and test for equality, So, for example, I have

{$mode objfpc}
{$interfaces corba}
type
	IMyInterface = Interface
		[  '{34aad6d0-5884-4143-97c2-b6e330305ae3}']
		Function MyFunc : Integer;
	end;

Now I can pass IMyInterface to TObject.GetInterface as the first parameter, which is of type TGuid in the declaration.

However, if I use the same TGuid parameter type in my own procedure

	procedure TestInterfaceParameter
	      ( const theInterface            : TGuid);
	    begin
	      if IsEqualGUID
	       ( theInterface, ITestInterface1)
	        then writeln
	           ( 'theInterface = ITestInterface1')
	    end;

then I run into the problem that IMyInterface is accepted as actual parameter of TestInterfaceParameter and of IsEqualGUID only if IMyInterface inherits from IUnknown (which I certainly don't want, as it is reference counted). Strangely, TObject.GetInterface does accept IMyInterface as a parameter. So where is the difference ? Is there some compiler magic involved in TObject.GetInterface ?

Regards,

Adriaan van Os

Reply by email

Zamrony P. Juhara
to Adriaan van Os
If you need to know if an instance implement certain interface, you can use is operator
if anInstance is IInterface1 thenbegin     end;

Zamrony P. Juhara

On Sat, Dec 21, 2019 at 0:25, Adriaan van Os wrote: I would like to pass a CORBA interface-type as parameter and test for equality, So, for example, I have

{$mode objfpc}
{$interfaces corba}

type
    IMyInterface = Interface
   
        [  '{34aad6d0-5884-4143-97c2-b6e330305ae3}']

        Function MyFunc : Integer;
    end;

Now I can pass IMyInterface to TObject.GetInterface as the first parameter, which is of type TGuid in the declaration.

However, if I use the same TGuid parameter type in my own procedure

    procedure TestInterfaceParameter
          ( const theInterface            : TGuid);         begin
          if IsEqualGUID
          ( theInterface, ITestInterface1)
            then writeln
              ( 'theInterface = ITestInterface1')         end;

then I run into the problem that IMyInterface is accepted as actual parameter of TestInterfaceParameter and of IsEqualGUID only if IMyInterface inherits from IUnknown (which I certainly don't want, as it is reference counted). Strangely, TObject.GetInterface does accept IMyInterface as a parameter. So where is the difference ? Is there some compiler magic involved in TObject.GetInterface ?

Regards,

Adriaan van Os

Reply by email

Adriaan van Os
to Zamrony P. Juhara
Zamrony P. Juhara wrote 7 lines
If you need to know if an instance implement certain interface, you can use is operator if anInstance is IInterface1 then begin end

Thanks for the reply, but that is not what I meant.

Regards,

Adriaan van Os

Reply by email

Sven Barth
to Adriaan van Os: I would like to pass a CORBA interface-type as parameter and test for equality, So, for…
Adriaan van Os wrote 35 lines
I would like to pass a CORBA interface-type as parameter and test for equality, So, for example, I have {$mode objfpc} {$interfaces corba} type     IMyInterface = Interface         [  '{34aad6d0-5884-4143-97c2-b6e330305ae3}']         Function MyFunc : Integer;     end; Now I can pass IMyInterface to TObject.GetInterface as the first parameter, which is of type TGuid in the declaration. However, if I use the same TGuid parameter type in my own procedure     procedure TestInterfaceParameter           ( const theInterface            : TGuid);         begin           if IsEqualGUID            ( theInterface, ITestInterface1)             then writeln                ( 'theInterface = ITestInterface1')         end; then I run into the problem that IMyInterface is accepted as actual parameter of TestInterfaceParameter and of IsEqualGUID only if IMyInterface inherits from IUnknown (which I certainly don't want, as it is reference counted). Strangely, TObject.GetInterface does accept IMyInterface as a parameter. So where is the difference ? Is there some compiler magic involved in TObject.GetInterface ?

The important difference in this regard between COM and CORBA interfaces is that for CORBA interfaces the "guid" is a ShortString, not a TGUID. Thus the compiler allows to convert a CORBA interface to a ShortString. For TObject.GetInterface this works, because it has a overload for ShortString. ;) (this is document here:
https://www.freepascal.org/docs-html/current/ref/refse47.html#x100-1220007.6 )

Regards,
Sven

Reply by email

Adriaan van Os
to Sven Barth
Sven Barth wrote 7 lines
The important difference in this regard between COM and CORBA interfaces is that for CORBA interfaces the "guid" is a ShortString, not a TGUID. Thus the compiler allows to convert a CORBA interface to a ShortString. For TObject.GetInterface this works, because it has a overload for ShortString. ;) (this is document here: https://www.freepascal.org/docs-html/current/ref/refse47.html#x100-1220007.6

Thanks, I hadn't seen that. Yes, this solves the problem. And this clarifies that the CORBA interface-identifying string doesn't need to be in GUID form, it can be something as simple as [ 'IMyInterface' ].

I will note that the interface-identifier comparison below (in procedure TestInterfaceParameter) doesn't work without the explicit typecast from interface-identifier to "shortstring". Apparently, that typecast is implicit when passing an interface-identifier as actual parameter to a "shortstring" formal parameter.

type
     InterfaceType                           = string[ 255];

procedure TestInterfaceParameter

       ( const theInterface            : InterfaceType);
     begin
       if theInterface = InterfaceType( ITestInterface1)
         then writeln
            ( 'theInterface = ITestInterface1')
     end;
begin
   TestInterfaceParameter
     ( ITestInterface1)
end.

Regards,

Adriaan van Os

Reply by email

Sven Barth
to Adriaan van Os
Adriaan van Os wrote 35 lines
Sven Barth via fpc-pascal wrote: > > The important difference in this regard between COM and CORBA > interfaces is that for CORBA interfaces the "guid" is a ShortString, > not a TGUID. Thus the compiler allows to convert a CORBA interface to > a ShortString. For TObject.GetInterface this works, because it has a > overload for ShortString. ;) (this is document here: > https://www.freepascal.org/docs-html/current/ref/refse47.html#x100-1220007.6 Thanks, I hadn't seen that. Yes, this solves the problem. And this clarifies that the CORBA interface-identifying string doesn't need to be in GUID form, it can be something as simple as [ 'IMyInterface' ]. I will note that the interface-identifier comparison below (in procedure TestInterfaceParameter) doesn't work without the explicit typecast from interface-identifier to "shortstring". Apparently, that typecast is implicit when passing an interface-identifier as actual parameter to a "shortstring" formal parameter. type     InterfaceType                           = string[ 255]; procedure TestInterfaceParameter       ( const theInterface            : InterfaceType);     begin       if theInterface = InterfaceType( ITestInterface1)         then writeln            ( 'theInterface = ITestInterface1')     end; begin   TestInterfaceParameter     ( ITestInterface1) end.

That is because the compiler only implements the *assignment* of an interface to a ShortString (or TGUID for COM interfaces), not the comparison.

Regards,
Sven

Reply by email