错误C2664 - 需要帮助和澄清

时间:2014-07-24 04:27:18

标签: c# c++ c2664

  1. 我有COM compnent(dll)ThirdParty定义了界面 和一些实现此接口的COM类ThirdPartyObjectClass。 我有适当的文件ThirdParty.h和ThirdParty_i.c允许用C ++编译它。

       IThirdParty
       {
            HRESULT ComFoo();
       }
    
  2. 我使用" tlbimp / sysarray"构建名为ThirdPartyInterop.dll的interop dll, 暴露.Net接口ThirdPartyObject

  3. 我编写了新的C#组件,它引用了ThirdPartyInterop.dll

      using ThirdPartyInterop;
      namespace CsComponent
      {
            public class CsClass
            {
                public void NetFoo( ThirdPartyObject thirdPrty )
                {
                    thirdPrty.ComFoo();
                }
            } 
       }
    
  4. 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' :无法从' IThirdParty *'转换参数1 to' 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。

    我的问题是:

    如何从给定的IThirdParty *创建ThirdPartyInterop :: ThirdPartyObject?

1 个答案:

答案 0 :(得分:1)

因为在你的情况下,每个IThirdParty实际上都是一个ThirdPartyObject,你可以使用强制转换。 Hovewer,除了在新的指令中,你不应该使用com对象的具体类型,总是只使用接口。更改您的NetFoo()方法以将IThirdParty作为参数。