COM: Create a VT_ARRAY with VT_BSTR values

不羁岁月 提交于 2019-12-06 06:41:20

问题


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?


回答1:


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);



回答2:


Don't you want:

filePaths.vt = VT_ARRAY|VT_BSTR;

Since you're creating a SafeArray of BSTRs?



来源:https://stackoverflow.com/questions/1456400/com-create-a-vt-array-with-vt-bstr-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!