Ошибка C2664 - нужна помощь и уточнение

  1. У меня COM-компонент ( dll) ThirdParty определил интерфейс, а некоторые COM-класс ThirdPartyObjectClass реализуют этот интерфейс. У меня есть соответствующие файлы ThirdParty.h и ThirdParty_i.c, позволяющие скомпилировать его в C++.

       IThirdParty
       {
            HRESULT ComFoo();
       }
    
  2. Я создаю dll взаимодействия с именем ThirdPartyInterop.dll, используя "tlbimp /sysarray", который предоставляет интерфейс.Net ThirdPartyObject.

  3. Я пишу новый компонент C#, который имеет ссылку на ThirdPartyInterop.dll

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

Метадада ThirdPartyClass - это:

   using System.Runtime.InteropServices;

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

а также

   using System;
   using System.Runtime.InteropServices;

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

У меня есть старый код, написанный на управляемом 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();
       ...
   }

Мне нужно изменить его, чтобы использовать мой 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 );
       ...
   }

Но это не может быть скомпилировано: ошибка C2664: "CsComponent::CsClass::NetFoo": невозможно преобразовать параметр 1 из "IThirdParty *" в "ThirdPartyInterop::ThirdPartyObject ^"

Следующая реализация в порядке:

   #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 );
       ...
   }

Но мне нужно использовать аргумент CppFoo thirdParty.

Мой вопрос:

Как создать ThirdPartyInterop:: ThirdPartyObject из заданного IThirdParty*?

1 ответ

Поскольку в вашем случае каждый IThirdParty на самом деле является ThirdPartyObject, вы можете просто использовать приведение. Однако, за исключением новой инструкции, вы никогда не должны использовать конкретный тип com-объекта, всегда только интерфейс. Измените ваш метод NetFoo(), чтобы он принимал IThirdParty в качестве аргумента.

Другие вопросы по тегам