title: how to call a C++ dll exported function from c#
body: <p>This is the first time I'm trying to mix c# an unmanaged C++ so this might be a very simple question , but I don't get it.</p>
  
  <p>I need to call some functions from a C++ dll into C# code. Here is the code for the dll project:</p>
  
  <p>the .h file :</p>
  
  <pre><code>#pragma once 
  #include &lt;iostream&gt;
  #if defined FIRSTDLL_EXPORTS
      #define DECLDIR __declspec(dllexport)
  #else
      #define DECLDIR __declspec(dllimport)
  #endif
  
  extern "C"
      {
        DECLDIR int Add( int a, int b );
        DECLDIR void Function( void );
      }
  </code></pre>
  
  <p>the .cpp file</p>
  
  <pre><code>#include "stdafx.h"
  #include "myFct.h"
  #include &lt;iostream&gt;
  
  extern "C"
  {
        DECLDIR int Add( int a, int b )
        {
            return( a + b );
  }
  
        DECLDIR void Function( void )
        {
            std::cout &lt;&lt; "DLL Called!" &lt;&lt; std::endl;
        }
  }
  </code></pre>
  
  <p>I compiled this for both the debug and releas and copied it in the <code>debug</code> folder of my C# project. Neither version worked.</p>
  
  <p>Here is the c# code:</p>
  
  <pre><code>[DllImport("firstDLL.Dll")]
  public static extern int Add(int a, int b);
  
  
  var cyu = Add(3, 5);
  </code></pre>
  
  <p>And when I try to run this I get </p>
  
  <blockquote>
  "Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Program Files\Microsoft Office\Office14\WINWORD.EXE'.
  Additional Information: A call to PInvoke function 'MyAddin!MyAddin.ThisAddIn::Add' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."</blockquote>
  
  <p>But as I see the signatures are the same. What am I missing??</p>
  
  <p>Thanks!</p>