0
  1. 私はCOMコンポーネント( dll )ThirdPartyがインターフェースを定義し、このインターフェースを実装するいくつかのCOMクラス ThirdPartyObjectClass を持っています。C++ でコンパイルできる適切なファイル ThirdParty.h と ThirdParty_i.c があります。

       IThirdParty
       {
            HRESULT ComFoo();
       }
    
  2. 「tlbimp /sysarray」を使用して、ThirdPartyInterop.dll という名前の相互運用 dll をビルドします。これにより、.Net インターフェイス ThirdPartyObject が公開されます。

  3. ThirdPartyInterop.dll への参照を持つ新しい C# コンポーネントを作成します。

      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 を使用する必要があります。

私の質問は:

指定された IThirdParty* から ThirdPartyInterop::ThirdPartyObject を作成する方法は?

4

1 に答える 1