title: c++ unmanaged dll used by dllimport in C# produces entry point not found error
body: <p>I know this question has been asked here many times before.  I've read all those questions that I can find along with information outside of stackoverflow.  So far I haven't found an answer i can figure out that will solve the specific problem that I'm having.</p>
  
  <p>here is the code for the unmanaged c++ dll header file.</p>
  
  <pre><code>namespace MyWin32DLL
  {
      class MyWin32ClassOne
      {
  
      public:
          static __declspec(dllexport) int Getvar();
  
      };
  }
  </code></pre>
  
  <p>Here is the code for the c++ dll cpp file</p>
  
  <pre><code>#include "MyWin32ClassOne.h"
  
  namespace MyWin32DLL
  {
  
      int MyWin32ClassOne::Getvar()
      {
          return 123;
      }
  }
  </code></pre>
  
  <p>This code i've put together from various sources so it may not be right at all.  I'm not very experienced with c++ or dll's.</p>
  
  <p>Here is the code from my silly little c# winforms prog that I attempt to access the dll with. <strong>(edited to correct type mismatch as pointed out by  tolanj in the comments)</strong></p>
  
  <pre><code>namespace TestDll
  {
      public partial class Form1 : Form
      {
  
          [DllImport("MyWin32CppDll.dll", CallingConvention = CallingConvention.StdCall)]
          public static extern int Getvar();
  
          public Form1()
          {
              InitializeComponent();
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              string response = Getvar().ToString();
  
              MessageBox.Show(response, "title", MessageBoxButtons.OK);
          }
      }
  }
  </code></pre>
  
  <p>Now, at this point i understand that i'm probalby getting the "no entry point" error because of how the c++ compiler mangles the names of methods and functions.</p>
  
  <p>From what i've read there are two things I can do to solve the problem.</p>
  
  <p>Thing 1
  Add extern "c" before my declaration so the name doesn't get mangled by the compiler.</p>
  
  <pre><code>namespace MyWin32DLL
  {
      class MyWin32ClassOne
      {
  
      public:
          extern "C" static __declspec(dllexport) int Getvar();
  
      };
  }
  </code></pre>
  
  <p>When i try this I get an error from Visual Studio stating that "linkage specification isn't allowed".</p>
  
  <p>Ok, so I try thing 2 which is to use dumpbin to find the mangled name of my function and use the mangled name as the entry point in the dllimport call.</p>
  
  <p>So i run dumpbin /symbols on my dll and i get no function name, mangled or otherwise.</p>
  
  <pre><code>Dump of file mywin32cppdll.dll
  
  File Type: DLL
  
    Summary
  
      1000 .data
      1000 .idata
      2000 .rdata
      1000 .reloc
      1000 .rsrc
      4000 .text
     10000 .textbss
  </code></pre>
  
  <p>Next i try dumpbin /exports</p>
  
  <pre><code>Dump of file mywin32cppdll.dll
  
  File Type: DLL
  
  Section contains the following exports for MyWin32CppDll.dll
  
  00000000 characteristics
  554CF7D4 time date stamp Fri May 08 13:52:20 2015
      0.00 version
         1 ordinal base
         1 number of functions
         1 number of names
  
  ordinal hint RVA      name
  
        1    0 00011005 ?Getvar@MyWin32ClassOne@MyWin32DLL@@SAHXZ = @ILT+0(?Getvar@MyWin32ClassOne@MyWin32DLL@@SAHXZ)
  
  Summary
  
      1000 .data
      1000 .idata
      2000 .rdata
      1000 .reloc
      1000 .rsrc
      4000 .text
     10000 .textbss
  </code></pre>
  
  <p>Looking at that I don't see a mangled or decorated name to use.  But as a larth i use "Getvar@MyWin32ClassOne@MyWin32DLL@@SAHXZ" as my entry point and still get the same error in my c# program.</p>
  
  <p>Obviously I've missed something.  How do I access the dll function from my c# program?</p>