通过BSTR的VARIANT / SAFEARRAY迭代以在C ++中分配值和打印到控制台

时间:2014-07-28 21:08:49

标签: c++ printing variant bstr safearray

我正在尝试使用值“test”填充BSTR类型的10个索引的SAFEARRAY,并打印出来以在分配SAFEARRAY以确认正确性之后控制SAFEARRAY的每个索引的值。我运行了调试器,并为前5个索引获得了以下值(我的SAFEARRAY称为sa)。不知怎的,我正在不正确地迭代SAFEARRAY或使用错误的类型,每个索引都应该是“test”。关于我做错什么的任何建议?

sa[0] = "test"
sa[1] = "est"
sa[2] = "st"
sa[3] = "t"
sa[4] = ""

...

#include <iostream>
#include <string>
#include <Windows.h>
#include <atlbase.h>
#include <comutil.h>
#include <string.h>
#include <stdio.h>

using namespace std;

void fillVariant(VARIANT& varIn, BSTR &srcArray);

int main()
{

    BSTR *theArray = new BSTR[10];
    for(int i = 0 ; i < 10; i++)
    {

        theArray[i] = SysAllocString(L"test");
    }

    VARIANT variantArray;
    fillVariant(variantArray, *theArray);


    return 0;
}

void fillVariant(VARIANT& varIn, BSTR &srcArray)
{
    VARIANT *variantArray = &varIn;
    VariantInit(variantArray);
    variantArray->vt = VT_ARRAY|VT_BSTR;

    SAFEARRAY* sa;
    SAFEARRAYBOUND aDim[1]; 
    aDim[0].lLbound = 0; 
    aDim[0].cElements = 10;

    sa = SafeArrayCreate(VT_BSTR, 1, aDim);

    BSTR* dwArray = NULL;
    SafeArrayAccessData(sa, (void**)&dwArray);

    for(int i = 0; i < 10; i++)
    {
        dwArray[i] = &srcArray[i];

        BSTR tmp = (BSTR) dwArray[i];
        std::wstring ws(tmp);
        //std::wstring ws(*dwArray[i], SysStringLen(dwArray[i]));
        std::wcout << ws << endl;

    }
    SafeArrayUnaccessData(sa);
    variantArray->parray = sa;
}

2 个答案:

答案 0 :(得分:2)

&srcArray[i]没有按照您的想法行事。 srcArray是一个BSTR,它是WCHAR*的typedef。 不是 BSTR s的数组。 srcArray[i]是指i中的theArray[0]个字符,&srcArray[i]是该字符的地址。这就是你如何“测试”,“估计”等等。

fillVariantBSTR*作为其第二个参数,然后传递theArray,而不是*theArray

在不相关的说明中,您的程序会泄漏一堆BSTRSAFEARRAY

答案 1 :(得分:2)

您没有正确填写VARIANT。试试这个:

#include <iostream>
#include <string>
#include <Windows.h>
#include <atlbase.h>
#include <comutil.h>
#include <string.h>
#include <stdio.h>

using namespace std;

void fillVariant(VARIANT& varIn, BSTR *srcArray, int srcArrayLen);

int main()
{
    BSTR *theArray = new BSTR[10];
    for(int i = 0 ; i < 10; i++)
    {
        theArray[i] = SysAllocString(L"test");
    }

    VARIANT variantArray;
    fillVariant(variantArray, theArray, 10);

    // don't forget to free memory when done!
    // note: the VARIANT owns the BSTRs, so DON'T free them!
    VariantClear(&variantArray);
    delete[] theArray;

    return 0;
}

void fillVariant(VARIANT& varIn, BSTR *srcArray, int srcArrayLen)
{
    VARIANT *variantArray = &varIn;
    VariantInit(variantArray);

    SAFEARRAYBOUND aDim[1]; 
    aDim[0].lLbound = 0; 
    aDim[0].cElements = srcArrayLen;

    SAFEARRAY* sa = SafeArrayCreate(VT_BSTR, 1, aDim);
    if (sa)
    {    
        BSTR* dwArray = NULL;
        SafeArrayAccessData(sa, (void**)&dwArray);

        for(int i = 0; i < srcArrayLen; i++)
        {
            // note: passing ownership, NOT making a copy
            dwArray[i] = srcArray[i];

            //std::wstring ws(dwArray[i], SysStringLen(dwArray[i]));
            std::wcout << dwArray[i] << endl;
        }

        SafeArrayUnaccessData(sa);

        variantArray->vt = VT_ARRAY|VT_BSTR;
        variantArray->parray = sa;
    }
}

可替换地:

#include <iostream>
#include <string>
#include <Windows.h>
#include <atlbase.h>
#include <comutil.h>
#include <string.h>
#include <stdio.h>

using namespace std;

void fillVariant(VARIANT& varIn, BSTR *srcArray, int srcArrayLen);

int main()
{
    BSTR *theArray = new BSTR[10];
    for(int i = 0 ; i < 10; i++)
    {
        theArray[i] = SysAllocString(L"test");
    }

    VARIANT variantArray;
    fillVariant(variantArray, theArray, 10);

    // don't forget to free memory when done!

    VariantClear(&variantArray);

    // note: the VARIANT DOES NOT own the BSTRs, so DO free them!
    for(int i = 0 ; i < 10; i++)
    {
        SysFreeString(theArray[i]);
    }
    delete[] theArray;

    return 0;
}

void fillVariant(VARIANT& varIn, BSTR *srcArray, int srcArrayLen)
{
    VARIANT *variantArray = &varIn;
    VariantInit(variantArray);

    SAFEARRAYBOUND aDim[1]; 
    aDim[0].lLbound = 0; 
    aDim[0].cElements = srcArrayLen;

    SAFEARRAY* sa = SafeArrayCreate(VT_BSTR, 1, aDim);
    if (sa)
    {    
        for(LONG i = 0; i < srcArrayLen; i++)
        {
            // note: makes a copy, DOES NOT pass ownership!
            SafeArrayPutElement(sa, &i, srcArray[i]);

            //std::wstring ws(srcArray[i], SysStringLen(srcArray[i]));
            std::wcout << srcArray[i] << endl;
        }

        variantArray->vt = VT_ARRAY|VT_BSTR;
        variantArray->parray = sa;
    }
}