title: How to use C++ dll from C#
body: <p>I'm trying include c++ library (DLL) in my c# project but every time I do that I get following error message in VS2012</p>
  <pre><code>A reference to 'C:\Users\HiepDang\Desktop\mydll.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.
  </code></pre>
  <p>When I'm trying add Com+ Component, I get following error message in window</p>
  <blockquote>
  <p>One or more files do not contain components or type libraries. These files cannot be installed.</p>
  <p>Error 80110425 occurred.</p>
  <p>An unknown error occured. You should check all documentation for a solution. If no further information is available, please contract technical support</p>
  </blockquote>
  <p>I'm following thread <a href="https://stackoverflow.com/questions/20823551/use-c-dll-from-c-sharp">here</a> and <a href="https://stackoverflow.com/questions/772041/using-c-library-in-c-sharp">here</a> but my problem not solved.</p>
  <p>In C++, I can use dll like:</p>
  <pre><code>    //Header
      extern &quot;C&quot; __declspec( dllimport ) int mymethod(const char *key, char *data, size_t buflen);
      
      //Code test
      const char* key = &quot;test&quot;;
  
      char* data = new char[1024];
      int buflen = 1024;
      int result = mymethod(key,data, buflen);
  </code></pre>
  <p>In C#, I use dll as:</p>
  <pre><code>    //define dll
      [DllImport(&quot;mydll.dll&quot;, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
      public static extern int mymethod([MarshalAs(UnmanagedType.LPTStr)]string key, [MarshalAs(UnmanagedType.LPTStr)]string data, uint buflen);
  
      //method test
      private static int testdll() 
      {
          string key = &quot;123456789&quot;;
          string buf = string.Empty;
          mymethod(key, buf, 1024);
          return 0;
      }
  </code></pre>
  <p>Can you tell me any solutions to solve it.</p>
  <p>P.s: My English is not good. I'm sorry if something inconvenient</p>
  <p>Edited: I explain the variable in method in dll. &quot;key&quot; as string input has 8-13 characters, mymethod will be encrypted to generate &quot;buf&quot;. I need variable of &quot;buf&quot;.</p>