title: Is [ComImport] considered P/Invoke?
body: <p>What <strong><em>is</em></strong> Platform Invoke (P/Invoke)?</p>
  
  <hr>
  
  <p>What does it mean to be performing a P/Invoke? Is it <a href="http://msdn.microsoft.com/en-us/library/aa446536.aspx" rel="nofollow">calling an external dll</a>? e.g.:</p>
  
  <pre><code>[DllImport("coredll.dll", SetLastError=true)]
  private static extern bool SHGetSpecialFolderPath(
        int hwndOwner, 
        string lpszPath,
        ceFolders nFolder,
        bool fCreate);
  </code></pre>
  
  <p>Is that what P/Invoke means: to use the <code>[DllImport]</code> attribute?</p>
  
  <p>Is there anything else that can be considered a <code>P/Invoke</code>?</p>
  
  <p>What about <code>[ComImport]</code>? e.g.:</p>
  
  <pre><code>[System.Runtime.InteropServices.ComImport]
  [Guid("F8383852-FCD3-11d1-A6B9-006097DF5BD4")]
  public class ProgressDialog
  {
  }
  </code></pre>
  
  <p><strong>Note</strong>: This COM class (F8383852-FCD3-11d1-A6B9-006097DF5BD4) can be found in </p>
  
  <pre><code>HKEY_CLASSES_ROOT\CLSID\{F8383852-FCD3-11d1-A6B9-006097DF5BD4}
        (default)       %SystemRoot%\system32\shell32.dll
        ThreadingModel  Both
  </code></pre>
  
  <p>i could also construct a native <code>ADO Recordset</code> object with the code:</p>
  
  <pre><code>[System.Runtime.InteropServices.ComImport]
  [Guid("00000535-0000-0010-8000-00AA006D2EA4")]
  public class Recordset
  {
  }
  
  Object rs= new Recordset();
  </code></pre>
  
  <p>Is that considered P/Invoke?</p>
  
  <p>If we choose to say "P/Invoke is bad", is ComImport as "bad" as DllImport?</p>
  
  <p>What <strong><em>is</em></strong> Platform Invoke (P/Invoke)?</p>
  
  <hr>
  
  <p><strong>Update</strong>: From MSDN:</p>
  
  <blockquote>
    <h2><a href="http://msdn.microsoft.com/en-us/library/aa288468%28v=vs.71%29.aspx" rel="nofollow">Platform Invoke Tutorial</a></h2>
    
    <p>Platform Invocation Services (PInvoke) allows managed code to call
    unmanaged functions that are implemented in a DLL.</p>
    
    <p>There are two ways that C# code can directly call unmanaged code:</p>
    
    <ul>
    <li>Directly call a function exported from a DLL.</li>
    <li>Call an interface method on a COM object (for more information, see C<a href="http://msdn.microsoft.com/en-us/library/aa645736%28v=vs.71%29.aspx" rel="nofollow">OM Interop Part 1: C# Client Tutorial</a>).</li>
    </ul>
  </blockquote>
  
  <p>i think i may have answered my own question.</p>
  
  <hr>
  
  <p>It's a year and a half later. Now that nobody is paying attention to the question, and nobody has it favorited, i can say that the answer i accepted is wrong. <code>P/Invoke</code> is short for <code>Platform Invoke</code>. It is a mechanism where managed code operating inside the CLR can call unmanaged native (i.e. platform) code. This is nearly always done by calling code that resides in a native dll. COM dlls <em>are</em> native code; they just follow a strict structure to allow many different compilers to call them.</p>
  
  <p>And Platform Invoke is bad. It bypasses all garbage collection, and depends on the platform (i.e. my 32-bit CLR process cannot load a 64-bit dll, my application written for Android cannot work on Windows, my application written for functionality on Windows 8 won't work on Windows XP).</p>