title: stdcall calling convention and using pinvoke in C#
body: <p>I created a DLL file which includes two empty functions below.</p>
  
  <pre><code>extern "C" __declspec(dllexport) void __stdcall myFunc1() {
      // just empty function
  }
  
  extern "C" __declspec(dllexport) void __cdecl myFunc2() {
      // just empty function
  }
  </code></pre>
  
  <p>In C#, I could call the functions using <code>DLLImport</code> attribute like below.</p>
  
  <pre><code>[DllImport("myDLL", CallingConvention=CallingConvention.StdCall)]
  private extern static void myFunc1();
  
  [DllImport("myDLL", CallingConvention=CallingConvention.Cdecl)]
  private extern static void myFunc2();
  </code></pre>
  
  <p>So I tried again directly with <code>LoadLibrary()</code> of kernel32.dll instead of <code>DllImport</code> attribute.</p>
  
  <pre><code>[UnmanagedFunctionPointer(CallingConvention.StdCall)]
  private delegate void MyFunc1();
  
  [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  private delegate void MyFunc2();
  </code></pre>
  
  <p>However a runtime error occurs when I call MyFunc1() <strong>where MyFunc2() works</strong>.</p>
  
  <p>So I replaced <code>__stdcall</code> with <code>__cdecl</code> in C++, recompiled the DLL and then called MyFunc1() again in C#.</p>
  
  <p>And.. It worked.</p>
  
  <p><strong>Why on earth doesn't __stdcall calling convention work with pinvoke in C#?</strong></p>