title: When is it necessary/appropriate to use InAttribute and OutAttribute for COM Interop
body: <p>I am trying to go through the mess of COM interop definitions we have scattered across various projects and collect them into a single, known-good location from which the whole dev team can benefit. Part of this effort involves cleaning up the definitions that have been accumulated over the years. </p>
  
  <p>Some of these are borrowed from other source code, some were copied verbatim from pinvoke.net, and some look to be directly translated from the SDK headers. One thing I notice is that there is no consistency about when to use the various marshalling attributes, (even among the pinvoke.net examples this is pretty hit-or-miss). Part of the problem is, I don't think anyone here (myself included) fully understands when the various attributes are needed or not, or what they actually do. Up to this point, getting these right seems to be a combination of guesswork and random changes until the COMExceptions stop happening, but I'd much rather the translations be correct because someone actually looked at them and declared them so.</p>
  
  <p>So, I'm starting with <code>[In]</code> and <code>[Out]</code>. I know what those two attributes do conceptually: they inform the marshaller which direction the data has to go. I <em>assume</em> the marshaller, for example, won't bother copying <code>[In]</code> data back to the caller, or knows that <code>[Out]</code> data might need to be freed on the callee side, etc. The things I don't know are:</p>
  
  <ol>
  <li>When is it <strong>necessary</strong> to use these attributes? That is, when is the default marshalling behavior wrong such that the attributes make it correct?</li>
  <li>When is it <strong>safe</strong> to use these attributes? That is, will specifying what should already be the default behavior change the way the marshaller works?</li>
  <li>When is it <strong>dangerous</strong> to use these attributes? I assume that explicitly marking an output parameter <code>[In]</code> is bad, but is marking an input parameter <code>[In, Out]</code> actually going to break anything?</li>
  </ol>
  
  <p>So, given a hypothetical COM Interface method whose IDL looks like this:</p>
  
  <pre><code>HRESULT Foo(
      [in] ULONG a,
      [out] ULONG * b
      [in, out] ULONG * c);
  </code></pre>
  
  <p>I might see this translated as any of the following:</p>
  
  <pre><code>void Foo(
    uint cb,
    out uint b,
    ref uint c);
  
  void Foo(
    uint cb,
    [Out] out uint b,
    [In, Out] ref uint c);
  
  void Foo(
    [In] uint cb,
    [Out] out uint b,
    [In, Out] ref uint c);
  </code></pre>
  
  <p>Is there any functional difference between those three? Is any of them considered "better" than the others for reasons besides technical correctness?</p>