title: using a c# dll in c++
body: <p>I am trying to use a c# dll in c++. I developed a c# dll.  </p>
  
  <p>I tried to add it as a reference to a c++ file, it showed warning as</p>
  
  <blockquote>
    <p>Could not add a reference<br>
     - Targets a higher version of .NET Framework<br>
     - Not a .Net assembly<br>
     - Not a registered ActiveX control  </p>
  </blockquote>
  
  <p>so I added the dll like as follows in my c++wrapper file. I could able to access the classes present in the c# dll.</p>
  
  <pre><code>#using &lt;C:\Users\New\Documents\Visual Studio 2015\Projects\ClassLibrary1\ClassLibrary1\bin\Debug\ClassLibrary1.dll&gt;
  using namespace csharp; //namespace in which managed class is present
  #include "Header.h"
  extern "C"
  {
      __declspec(dllexport)  int func()
      {
          managedClass^ r = gcnew managedClass();
          someClass^s = gcnew someClass();
          s-&gt;name = "xyz";
          return r-&gt;getString(s);
      }
  } 
  </code></pre>
  
  <p>Then I imported the wrapper dll to c++ console application as follows  </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;Windows.h&gt;
  #pragma comment(lib,"C:\\Users\\New\\Documents\\Visual Studio 2015\\Projects\\C++Wrapper\\Debug\\C++Wrapper.lib")
  #include &lt;C:\Users\New\Documents\Visual Studio 2015\Projects\C++Wrapper\C++Wrapper\Header.h&gt;
  void main()
  {
  
      int result = func();
      std::cout &lt;&lt; result;
      getchar();
  }
  </code></pre>
  
  <p>But I am getting error as follows:  </p>
  
  <blockquote>
    <p>Unhandled exception at 0x7479D928 (KernelBase.dll) in
    C++ClientPro.exe: 0xE0434352 (parameters: 0x80070002, 0x00000000,
    0x00000000, 0x00000000, 0x73790000).  </p>
  </blockquote>
  
  <p>I am getting the same error when I am trying to load the dll using LoadLibrary(libpath) function.<br>
  When I searched for this error, I got to know that It could not able to locate the file. In which step I was wrong. What I have to correct to use the c# dll in c++?<br>
  <strong>Edit</strong><br>
  1) I've edited my c++wrapper file. I want to create an instance for a class in c# dll and also pass it as a function parameter to the same dll.<br>
  That's why I did not go for another method in which we could export only the functions using public interface, we could not able to access the classes.<br>
  2) I enabled /clr in my projects.  </p>