COM:使用VT_BSTR值创建VT_ARRAY [英] COM: Create a VT_ARRAY with VT_BSTR values
问题描述
我是一个COM新手,我认为我有正确的,但运行时不喜欢它。任何帮助是非常感激。
我需要调用一个接收一个一维数组的BSTR的COM函数。具体来说,文档说参数必须是:
功能:AddFiles([in] VARIANT * filePaths)
filePaths 每个文件或
文件夹的完整路径的单维数组。 filePaths可以是类型
VT_ARRAY | VT_VARIANT
,其中每个条目
是VT_BSTR
或VT_ARRAY | VT_BSTR
。
我有一个 vector< wstring> myPaths
的路径,我想传递到接受上面的参数的函数。这里是我写的代码。在myComObject上调用AddFiles导致一个AV(myComObject不为null,我可以调用其他方法):
.. 。
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);
...
COM对象不是我的代码,但是我怀疑我没有正确地创建这个数组 - 根据AddFiles函数和代码我的需求,任何人都有想法我可能会做错了什么?
VARIANT myPath;
VariantInit(& myPath);
myPath.vt = VT_BSTR;
myPath.bstrVal = SysAllocString(it-> c_str());
SafeArrayPutElement(filePaths.parray,& i,& myPath);
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 aVT_BSTR
, orVT_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?
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);
这篇关于COM:使用VT_BSTR值创建VT_ARRAY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!