title: Invoke C++/CLI code from C#
body: <p>There are two projects (C# and Managed C++). C# projects needs to call a method in ManagedCPP project. in reference section of c# project the managed cpp dll is added.</p>
  
  <p>if Line 1, 2 and 3 are uncommented and Line 4 is commented, then also following problem occurs:</p>
  
  <blockquote>
    <p>Unhandled Exception: System.BadImageFormatException</p>
  </blockquote>
  
  <p>The code base is as follows:</p>
  
  <p><code>C# class:</code></p>
  
  <pre><code>using System.Runtime.InteropServices;
  using ManagedCPP;
  
  namespace CSharp
  {
  class Program
  {
      //[DllImport("ManagedCPP")]                   // Line 1
      //public static extern void Convert();        // Line 2
  
      static void Main(string[] args)
      {
          new Program().CreateObject();
      }
  
      private void CreateObject()
      {
          //Convert();                              // Line 3
          ManagedCPPEntryClass c = new ManagedCPPEntryClass();  // Line 4
  
          Console.ReadKey();
      }
  }
  }
  </code></pre>
  
  <p><code>C++/CLI class(.h):</code></p>
  
  <pre><code>namespace ManagedCPP 
  {
  public ref class ManagedCPPEntryClass
  {
      public:
          void Convert();
  };
  }
  </code></pre>
  
  <p><code>C++/CLI class(.cpp):</code></p>
  
  <pre><code>#include "ManagedCPP.h"
  using namespace ManagedCPP;
  
  void ManagedCPPEntryClass::Convert()
  {
  }
  </code></pre>