title: Confused over DLL entry points (entry point not found exception)
body: <p>I'm trying to learn how to use DLL's in C#.  I have a very simple DLL just to test the basics.</p>
  
  <pre><code>// MainForm.cs
  
  
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  
  using System.Runtime.InteropServices;
  
  namespace DLL_Test
  {
          public partial class Form1 : Form
          {
              [DllImport("TestDLL.dll",
                          EntryPoint="?Add@@YGHHH@Z",
                          ExactSpelling = true,
                          CallingConvention = CallingConvention.StdCall)]
              public static extern int Add(int a, int b);
  
              public Form1()
              {
                  InitializeComponent();
              }
  
              private void button1_Click(object sender, EventArgs e)
              {
                  int num;
                  try
                  {
                      num = Add(2, 3);
                      richTextBox1.AppendText(num.ToString() + "\n");
                  }
                  catch (DllNotFoundException ex)
                  {
                      MessageBox.Show(ex.ToString());
                  }
                  catch (EntryPointNotFoundException ex)
                  {
                      MessageBox.Show(ex.ToString());
                  }
               }
           }
  }
  </code></pre>
  
  <p>And the DLL code:</p>
  
  <pre><code>// TestDLL.cpp
  
  __declspec(dllexport) int __stdcall Add(int a, int b) {
  return(a + b);
  }
  </code></pre>
  
  <p>dumpbin returns the following:</p>
  
  <pre><code>ordinal   hint   RVA        name
        1      0   00011005   ?Add@@YGHHH@Z = @ILT+0(?Add@@YGHHH@Z)
  </code></pre>
  
  <p>This (and other attempts listed below) have all returned the same exception:</p>
  
  <pre><code>System.EntryPointException: Unable to find entry point named "..."
  </code></pre>
  
  <p>So I am at a loss for how to solve this.  Perhaps I do not understand how DllMain functions as the C# entry point for a DLL.  TestDLL.dll works when I test it in a C++ application.</p>
  
  <p>After searching for help, I've attempted the following changes:</p>
  
  <pre><code>// TestDLL.cpp
  
  extern "C" __declspec(dllexport) int __stdcall Add(int a, int b) {
  return(a + b);
  }
  </code></pre>
  
  <p>Which results in this from dumpbin</p>
  
  <pre><code>ordinal   hint   RVA        name
        1      0   00011005   _Add@8 = @ILT+135(_Add@8)
  </code></pre>
  
  <p>Thus, I changed my C# code:</p>
  
  <pre><code> // MainForm.cs
  
  ...
  
  [DllImport("TestDLL.dll",
                          EntryPoint="_Add",
                          ExactSpelling = true,
                          CallingConvention = CallingConvention.StdCall)]
              public static extern int Add(int a, int b);
  
  ...
  </code></pre>
  
  <p>I've also tried <code>__cdecl</code>:</p>
  
  <pre><code>// TestDLL.cpp
  
  extern "C" __declspec(dllexport) int __cdecl Add(int a, int b) {
  return(a + b);
  }
  </code></pre>
  
  <p>.</p>
  
  <pre><code>// MainForm.cs
  
  ...
  
  [DllImport("TestDLL.dll",
                          EntryPoint="_Add",
                          ExactSpelling = true,
                          CallingConvention = CallingConvention.Cdecl)]
              public static extern int Add(int a, int b);
  
  ...
  </code></pre>
  
  <p>Perhaps I'm misunderstanding the calling conventions.  Any help would be very appreciated.  Thank you.</p>