error C2664 - Need help and clarification

84 Views Asked by At
  1. I have the COM compnent ( dll ) ThirdParty defined the interface and some COM class ThirdPartyObjectClass implementing this interface. I have the appropriate files ThirdParty.h and ThirdParty_i.c allow to compile it in C++.

       IThirdParty
       {
            HRESULT ComFoo();
       }
    
  2. I build the interop dll called ThirdPartyInterop.dll using "tlbimp /sysarray", that exposes the .Net interface ThirdPartyObject

  3. I write the new C# component, that has a reference to ThirdPartyInterop.dll

      using ThirdPartyInterop;
      namespace CsComponent
      {
            public class CsClass
            {
                public void NetFoo( ThirdPartyObject thirdPrty )
                {
                    thirdPrty.ComFoo();
                }
            } 
       }
    

The metadada of ThirdPartyClass is:

   using System.Runtime.InteropServices;

   namespace ThirdPartyInterop
   {
       [CoClass(typeof(ThirdPartyObjectClass))]
       [Guid("xxxx")]
       public interface ThirdPartyObject : IThirdParty
       {
       }
   }

and

   using System;
   using System.Runtime.InteropServices;

   namespace ThirdPartyInterop
   {
       [TypeLibType(4160)]
       [Guid("yyyy")]
       public interface IThirdParty
       {
           [DispId(1)]
           void ComFoo();
       }
   }

I have an OLD code written in managed C++.

with the following:

   #include "stdafx.h"
   #pragma managed(push, off)
   #include "ThirdParty_i.c"
   #include "ThirdParty.h"
   #pragma managed(pop)

   void CppFoo( IThirdParty* thirdParty )
   {
       ...
       thirdParty -> ComFoo();
       ...
   }

I need to change it to use my CsClass:

   #include "stdafx.h"
   #pragma managed(push, off)
   #include "ThirdParty_i.c"
   #include "ThirdParty.h"
   #pragma managed(pop)

   void CppFoo( IThirdParty* thirdParty )
   {
       ...
       //thirdParty -> ComFoo();
       CsComponent::CsClass^ csClass = gcnew CsComponent::CsClass();
       csClass.NetFoo( thirdParty );
       ...
   }

But this can't be compiled: error C2664: 'CsComponent::CsClass::NetFoo' : cannot convert parameter 1 from 'IThirdParty *' to 'ThirdPartyInterop::ThirdPartyObject ^'

The following implementation is OK:

   #include "stdafx.h"
   #pragma managed(push, off)
   #include "ThirdParty_i.c"
   #include "ThirdParty.h"
   #pragma managed(pop)

   void CppFoo( IThirdParty* thirdParty )
   {
       ...
       //thirdParty -> ComFoo();
       CsComponent::CsClass^ csClass = gcnew CsComponent::CsClass();
       ThirdPartyInterop::ThirdPartyObject^ _thirdParty = gcnew ThirdPartyInterop::ThirdPartyObject();
       //csClass.NetFoo( thirdParty );
       csClass.NetFoo( _thirdParty );
       ...
   }

But I need use the CppFoo's argument thirdParty.

My question is:

How to create the ThirdPartyInterop::ThirdPartyObject from given IThirdParty* ?

1

There are 1 best solutions below

0
On

Since, in your case, every IThirdParty is actually a ThirdPartyObject, you could just use a cast. Hovewer, except in a new-Instruction, you should never use the concrete type of a com object, always only the interface. Change your NetFoo() method to take an IThirdParty as argument.