title: Marshaling a ComVisible component vararg array parameter
body: <p>I'm looking at migrating some legacy COM code written in VB6 to .NET, however this needs to generate a typelib that's fairly close to the original.</p>
  
  <p>I've bumped into a problem passing in an array of parameters when early bound to other VB6 components. In the original VB6, the signature looked like this:</p>
  
  <pre><code>Public Function ExecSPReturnRS(ByVal strProcAs String, _
              ParamArray varParams() As Variant) As Recordset
  </code></pre>
  
  <p>and generated MIDL that looks like this:</p>
  
  <pre><code>[id(0x60030009), vararg]
  HRESULT ExecSPReturnRS([in] BSTR strProc,
                         [in, out] SAFEARRAY(VARIANT)* varParams,
                         [out, retval] _Recordset** );
  </code></pre>
  
  <p>With C#, I can't determine the right declaration to generate the same MIDL. I'm either missing the <code>vararg</code> declaration, or the varParams parameter is declared as <code>SAFEARRAY(VARIANT)</code> rather than <code>SAFEARRAY(VARIANT)*</code>.</p>
  
  <p>So if in C# I declare as:</p>
  
  <pre><code>Recordset ExecSPReturnRS(string storedProc, ref object[] arguments);
  </code></pre>
  
  <p>...I get <code>SAFEARRAY(VARIANT)*</code>, but no <code>vararg</code>. However if I declare as</p>
  
  <pre><code>Recordset ExecSPReturnRS(string storedProc, params object[] arguments);
  </code></pre>
  
  <p>...then I get the <code>vararg</code> but the SAFEARRAY is not declared as by reference.</p>
  
  <p>I expect that <code>MarshalAsAttribute</code> may be the way to go, but the best I've been able to come up with so far is:</p>
  
  <pre><code>Recordset ExecSPReturnRS(string storedProc, 
                           [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT)]
                           object[] arguments);
  </code></pre>
  
  <p>But this gives me the same MIDL as if I declared as <code>params</code> without the MarshalAs.</p>
  
  <p>In essence, I want the MIDL to reflect as if I'd specified both <code>ref</code> and <code>params</code>, which is illegal under C#. What's the incantation I'm missing?</p>
  
  <p><hr>
  <strong>Edit:</strong> it seems there's some confusion on what the ultimate goal is. In essence we have an application that consists of many legacy VB6 components. In order to remove the legacy debt, we need to be able to move the VB components over to .NET bit by bit. Where that component is a dependency on other components, the .NET needs to be usable with existing VB6 and classic ASP, ideally with no code change. Some legacy components will be completely refactored and end up with no COM dependency. Many of the VB6 components use early binding.</p>
  
  <p>As it stands at the moment, when I use the <code>param</code> declaration in .NET to take the place of a VB6 ParamArray, building VB6 components against that object result in a <em>Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic</em> error in VB. If I use <code>ref</code> instead, I get the build error <em>Type mismatch: array or user-defined type expected.</em></p>
  
  <p>Calls in VB6 to the particular component I'm looking at look something like:</p>
  
  <pre><code>Set rs = dbconn.ExecSPReturnRS("dbo.StoredProc", _
      Array("@param1", value), _ 
      Array("@param2", value))
  </code></pre>
  
  <p>I've assumed the issue is a typelib issue, as my understanding is that VB will be using that at build time to validate the calls when early binding. However I've since discovered that if I late bind everything, then VB builds are successful and calls appear to work. This however will require source code changes of the legacy VB6, and other than a loss of auto-complete when late binding, I expect I'm also losing build-time validation of parameters. But it might be the best option we have.</p>
  
  <p>One final note: currently this is research/PoC to find out what's required and what isn't. Ultimately, this is <strong>not</strong> intended to be a line-by-line conversion of all objects. Some objects will need to expose a COM interface, others won't, and others will be refactored or dropped. However there will be places we will need to sustain a COM compatible interface for backwards compatibility, at least for the short to medium term whilst the migration proceeds.</p>