Convert VARIANT to string array

There's a lot of answers lying around for visual basic and C# regarding this but I need one for C++.

Basically I am calling a function which passes by reference a VARIANT and an array of strings gets stored to this. Once I get this VARIANT stored in var, I call VariantInit(&var)... and I'm kind of stuck how to convert this to a simple std::string array. I want to read the strings that are stored in here. Thanks!
KanivorousAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Could you be a bit more specific about the type of the VARIANT? If it is a VT_BSTR, you could just use a '_bstr_t' (http://msdn2.microsoft.com/en-us/library/zthfhkd6(VS.80).aspx, "_bstr_t Class") just like


#include <comdef.h>

//

VARIANT vt = GettingVariantFromSomewhere();

_bstr_t b (vt);

char* pStr = (char*) b;
wchar_t* pwStr = (wchar_t*) b;

That code uses the operators

operator const wchar_t*( ) const throw( );
operator wchar_t*( ) const throw( );
operator const char*( ) const;
operator char*( ) const;
 
that '_bstr_t()' already provides.
0
KanivorousAuthor Commented:
Okay I found out how to do it.. Basically I assumed VARIANT was an array of strings, not only a string like you mentioned. I'm still very new to VARIANTs so still trying to learn... Just as a reference, what I ended up doing was (very roughly):


   
VARIANT var;
VARIANT vtBstr;
VariantInit(&var);
VariantInit(&vtBstr);
myGetArrayOfStrings(&var);
if(var.vt==VT_EMPTY)
    goto end;
long lCount = var.parray->rgsabound[0].cElements;
for(long i=0; i<lCount; i++)
{
    SafeArrayGetElement(var.parray, &i, &vtBstr);
    std::string sSomeString = _com_util::ConvertStringToBSTR(vtBstr.bstrVal);
    // Do whatever with string
}
 
end:
VariantClear(&var);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.