tags:

views:

535

answers:

2

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.

I need to invoke a COM function that takes in a single dimensional array of BSTRs. Specifically, the documentation says the parameter must be:

Function: AddFiles ( [in] VARIANT * filePaths )

filePaths The single-dimensioned array of full paths to each file or folder. filePaths can be of type VT_ARRAY|VT_VARIANT, where each entry is a VT_BSTR, or VT_ARRAY|VT_BSTR.

I have a vector<wstring> myPaths 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):

        ...
        VARIANT filePaths;
        VariantInit( &filePaths );
        filePaths.vt = VT_ARRAY|VT_VARIANT;
        filePaths.parray = SafeArrayCreateVector( VT_BSTR, 0, (unsigned int) myPaths.size() );

        long i = 0;
        for( vector<wstring>::iterator it = myPaths.begin();
            it != myPaths.end();
            it++, i++ )
        {
            BSTR myPath= SysAllocString(it->c_str());
            SafeArrayPutElement( filePaths.parray, &i, myPath);
        }

        myComObject->AddFiles( &filePaths );
        ...

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?

+3  A: 

Don't you want:

filePaths.vt = VT_ARRAY|VT_BSTR;

Since you're creating a SafeArray of BSTRs?

jeffamaphone
Doesn't make a difference...
psychotik
So, try leaving it as `VT_ARRAY|VT_VARIANT` and then pack each string into a variant instead in the loop as suggested by taichicat below.
Kim Gräsman
+1  A: 

If myComObject->AddFiles can only deal with VT_ARRAY|VT_VARIANT, the following should work too.

VARIANT myPath;
VariantInit(&myPath);

myPath.vt = VT_BSTR;
myPath.bstrVal = SysAllocString(it->c_str());

SafeArrayPutElement(filePaths.parray, &i, &myPath);
taichicat
If so, you also need to say VT_VARIANT when creating the SAFEARRAY with SafeArrayCreateVector.
Kim Gräsman