简体   繁体   English

错误C2664-需要帮助和说明

[英]error C2664 - Need help and clarification

  1. I have the COM compnent ( dll ) ThirdParty defined the interface and some COM class ThirdPartyObjectClass implementing this interface. 我有COM组件(dll)ThirdParty定义了接口,并且有一些COM类ThirdPartyObjectClass实现了此接口。 I have the appropriate files ThirdParty.h and ThirdParty_i.c allow to compile it in C++. 我有相应的文件ThirdParty.h和ThirdParty_i.c允许在C ++中对其进行编译。

      IThirdParty { HRESULT ComFoo(); } 
  2. I build the interop dll called ThirdPartyInterop.dll using "tlbimp /sysarray", that exposes the .Net interface ThirdPartyObject 我使用“ tlbimp / sysarray”构建了一个名为ThirdPartyInterop.dll的互操作dll,该DLL公开了.Net接口ThirdPartyObject

  3. I write the new C# component, that has a reference to ThirdPartyInterop.dll 我编写了新的C#组件,该组件具有对ThirdPartyInterop.dll的引用

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

The metadada of ThirdPartyClass is: ThirdPartyClass的metadada为:

   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++. 我有一个用托管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: 我需要更改它以使用我的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 ^' 但这无法编译:错误C2664:'CsComponent :: CsClass :: NetFoo':无法将参数1从'IThirdParty *'转换为'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. 但是我需要使用CppFoo的参数thirdParty。

My question is: 我的问题是:

How to create the ThirdPartyInterop::ThirdPartyObject from given IThirdParty* ? 如何从给定的IThirdParty *创建ThirdPartyInterop :: ThirdPartyObject?

Since, in your case, every IThirdParty is actually a ThirdPartyObject, you could just use a cast. 由于在您的情况下,每个IThirdParty实际上都是ThirdPartyObject,因此您可以只使用强制类型转换。 Hovewer, except in a new-Instruction, you should never use the concrete type of a com object, always only the interface. 但是,除了在新指令中,永远不要使用com对象的具体类型,而只能使用接口。 Change your NetFoo() method to take an IThirdParty as argument. 更改您的NetFoo()方法以将IThirdParty用作参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM