title: C++ COM interface callback in C#
body: <p>The issue I'm facing is from this code:
  <a href="https://stackoverflow.com/questions/8063202/callback-from-a-c-com-dll-to-a-c-sharp-application">Callback from a C++ COM DLL to a C# application</a></p>
  
  <p>Notice that in the answer (in the C# code portion),  </p>
  
  <pre><code>internal class EVRPresenterCallback : EmideeMediaFoundationLib.IEVRPresenterCallback
  {
      public void PresentSurfaceCB(uint pSurface)
      {
      }
  }
  </code></pre>
  
  <p><code>PresentSurfaceCB</code> is declared with a void return type.</p>
  
  <p>However, in the IDL, I've declared it as <code>HRESULT PresentSurfaceCB(...)</code>.</p>
  
  <p>With this mismatch, the code actually works. <em>But</em> if I were to change the return type from <code>void</code> to <code>int</code> in C#'s interface declaration, I would end up with Access Violation in x86 mode but not x64 (x64 ABI does not pop anything from stack thus will not crash when return types mismatch). i.e.</p>
  
  <pre><code>internal class EVRPresenterCallback : EmideeMediaFoundationLib.IEVRPresenterCallback
  {
      public int PresentSurfaceCB(uint pSurface)
      {
          // this crashes with AV after the callback is called!
          // WHY?
      }
  }
  </code></pre>
  
  <p>I also found that I don't actually have to declare its guid for the interface to work.
  Why not? I suspect the VTable is understood from the interface declaration? In which case, I should also be able to restructure my C++ code to move the IDL code back into C++?</p>
  
  <pre><code>// [ComVisible(true), ComImport, Guid("B92D8991-6C42-4e51-B942-E61CB8696FCB")] -- this is unnecessary - why?
  [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  [SuppressUnmanagedCodeSecurity]
  internal interface IEVRPresenterCallback
  {
      [PreserveSig]
      int PresentSurfaceCB(IntPtr pSurface);
  }
  </code></pre>