title: COM: Create a VT_ARRAY with VT_BSTR values
body: <p>I'm a COM newbie and I think what I have is correct, but the runtime doesn't like it. Any help is much appreciated.</p>
  
  <p>I need to invoke a COM function that takes in a single dimensional array of BSTRs. Specifically, the documentation says the parameter must be:</p>
  
  <blockquote>
    <p>Function: AddFiles  (  [in] VARIANT *  filePaths )</p>
    
    <p><em>filePaths</em>  The single-dimensioned array of full paths to each file or
    folder. filePaths can be of type
    <code>VT_ARRAY|VT_VARIANT</code>, where each entry
    is a <code>VT_BSTR</code>, or <code>VT_ARRAY|VT_BSTR</code>.</p>
  </blockquote>
  
  <p>I have a <code>vector&lt;wstring&gt; myPaths</code> of paths which I want to pass into the function that takes the parameter above. Here's the code I wrote. Calling AddFiles on myComObject results in an AV (myComObject is not null, and I can invoke other methods on it):</p>
  
  <pre><code>        ...
          VARIANT filePaths;
          VariantInit( &amp;filePaths );
          filePaths.vt = VT_ARRAY|VT_VARIANT;
          filePaths.parray = SafeArrayCreateVector( VT_BSTR, 0, (unsigned int) myPaths.size() );
  
          long i = 0;
          for( vector&lt;wstring&gt;::iterator it = myPaths.begin();
              it != myPaths.end();
              it++, i++ )
          {
              BSTR myPath= SysAllocString(it-&gt;c_str());
              SafeArrayPutElement( filePaths.parray, &amp;i, myPath);
          }
  
          myComObject-&gt;AddFiles( &amp;filePaths );
          ...
  </code></pre>
  
  <p>The COM object isn't my code and I can't debug into it, but I suspect I'm not creating that array properly - based on the requirement of the AddFiles function and the code I have, anyone have ideas about what I might be doing wrong?</p>