title: how to link c++ dll to c# visual studio 2015
body: <p>my question is clear from the topic, i need to send a string from c# to c++ function and vice versa (terminology may be not right i am a noob) so i watched this tutorial :<a href="https://www.youtube.com/watch?v=hwmRtnJag4A&amp;index=1&amp;list=LLTUeeaXdALOpRBhDbqiFWlQ" rel="nofollow">https://www.youtube.com/watch?v=hwmRtnJag4A&amp;index=1&amp;list=LLTUeeaXdALOpRBhDbqiFWlQ</a> .Howewer I could not get it working, after running the debugger in c# i got this error: </p>
  
  <p>Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\Viktor\Documents\Visual Studio 2015\Projects\DLL_test\DLL_test\bin\x86\Debug\DLL_test.vshost.exe'.</p>
  
  <p>here is my code:
  main.cpp</p>
  
  <pre><code>#include"Header.h"
  #include&lt;iostream&gt;
  
  void main()
  {
      myClass sumTwoClass(10, 5);
      double result = sumTwoClass.sumX_Y();
  }
  </code></pre>
  
  <p>body.cpp</p>
  
  <pre><code>myClass::myClass(double var_x, double var_y)
  {
      x = var_x;
      y = var_y;
  }
      double myClass::sumX_Y()
  {
      return x + y;
  }
  </code></pre>
  
  <p>header.h</p>
  
  <pre><code>#pragma once
  
  class myClass 
  {
  public:
      myClass(double var_x, double var_y);
      double sumX_Y();
  private:
      double x;
      double y;
      };
  </code></pre>
  
  <p>CppClassDll
  main.cpp</p>
  
  <pre><code>#include"C:\\Users\\Viktor\\Documents\\Visual Studio 2015\\Projects\\MyClassCpp\\MyClassCpp\\Header.h"
  #include"C:\\Users\\Viktor\\Documents\\Visual Studio 2015\\Projects\\MyClassCpp\\MyClassCpp\\body.cpp"
  
  extern "C" __declspec(dllexport) double sumTwo(double var_x, double var_y)
  {
      myClass MC(var_x, var_y);
      return MC.sumX_Y();
  }
  </code></pre>
  
  <p>Dll_test
  Program.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Runtime.InteropServices;
  
  namespace DLL_test
  {
      class Program
      {
          [DllImport("C:\\Users\\Viktor\\Documents\\Visual Studio  2015\\Projects\\CppClassDll\\Debug\\CppClassDll.dll")]
          public static extern double sumTwo(double var_x, double var_y);
  
          static void Main(string[] args)
          {
              double var_x = 5;
              double var_y = 30;
  
              double result = sumTwo(var_x,var_y);
              Console.WriteLine(result);
              Console.ReadLine();
          }
      }
  }
  </code></pre>
  
  <p>Now the interesting thing is that when i run this i should get the result (35) in console but got the error i described above ...however today i ran the code (changed nothing) and the console showed up with the value 35, i was like what? i tried to run the program again and got the same error. Am i cursed or what?</p>
  
  <p>At debugging i got 0 errors, debugger set to x86.</p>