CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2010
    Posts
    42

    Question How to determine if VARIANT contains an Array?

    Hi all,

    I have a problem regarding VARIANTS. What I want to do is how to determine if a VARIANT contains an array. I want to do this because I am accessing a WMI variable and sometimes, the returned variable is an array, which will then be put into the VARIANT.

    I tried doing this:

    if(V_VT(&vtProp) == VT_ARRAY)
    cout << "VTPROP IS AN ARRAY" << endl;
    else if(V_VT(&vtProp) == VT_BSTR)
    cout << "VT PROP IS AN BSTR" << endl;

    But it does not correctly identify if the VARIANT contains an array. However, it does correctly identify if the VARIANT is a BSTR.

    Can anyone help me?

    Thank you all!

  2. #2
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: How to determine if VARIANT contains an Array?

    Try:

    Code:
    if(V_VT(&vtProp)&VT_ARRAY)
    VT_ARRAY is ORed with any other value.

  3. #3
    Join Date
    Nov 2003
    Posts
    1,902

    Re: How to determine if VARIANT contains an Array?

    VARTYPE is a bit-field. So an array of BSTR's would be "VT_ARRAY | VT_BSTR" etc...
    http://msdn.microsoft.com/en-us/library/ms221627.aspx

    gg

  4. #4
    Join Date
    Mar 2010
    Posts
    42

    Re: How to determine if VARIANT contains an Array?

    Hi egawtry and Codeplug, thanks for replying!

    @Codeplug: Thanks. I'll take a look at the link.

    @egawtry: I used a similar code to the one that you proposed. I used:

    V_VT(&vtProp) & VT_ARRAY) != 0

    And I think that it worked fine.


    Additional question, is this the correct way to get the contents of the array?

    cout << "VALUE: " << vtProp.bstrVal[0] << endl;

    This printed out "VALUE: 1".


    I also tried the type of elements which you posted and I used it like this:

    cout << "TYPE OF ELEMENTS: " << (V_VT(&vtProp) & VT_TYPEMASK) << endl;

    This printed out "TYPE OF ELEMENTS: 3". Does 3 here mean that there are 3 elements in the array?


    Thank you very much!

  5. #5
    Join Date
    Nov 2003
    Posts
    1,902

    Re: How to determine if VARIANT contains an Array?

    >> Additional question, is this the correct way to get the contents of the array?
    No. You access the SAFEARRAY member of the variant using V_ARRAY(). Then use SAFEARRAY api's - http://msdn.microsoft.com/en-us/library/ms221145.aspx

    >> V_VT(&vtProp) & VT_TYPEMASK == 3
    3 == VT_I4, or "4 byte signed int"

    gg

  6. #6
    Join Date
    Mar 2010
    Posts
    42

    Re: How to determine if VARIANT contains an Array?

    Hi Codeplug, thanks again for replying.

    If I get your suggestion, I need to convert the VARIANT to SAFEARRAY? I think I saw something like vtProp.pArray, which returns a SAFEARRAY.

    Also, does the "4 byte signed int" mean that the array contains data of data type "4 byte signed int"? If so, is it possible to just create an integer array to store those data?

    Thank you again!

  7. #7
    Join Date
    Nov 2003
    Posts
    1,902

    Re: How to determine if VARIANT contains an Array?

    >> I need to convert the VARIANT to SAFEARRAY?
    V_ARRAY() is a macro that accesses the "parray" member of the VARIANT - just like V_VT() accesses the "vt" member.

    If the vt member is "VT_I4 | VT_ARRAY", then the VARIANT represents a SAFEARRAY of VT_I4 values.

    >> is it possible to just create an integer array to store those data?
    Yes, using the SAFEARRAY API's you can access the data like a normal C/C++ array.
    http://msdn.microsoft.com/en-us/library/ms221620.aspx
    http://msdn.microsoft.com/en-us/library/ms221145.aspx

    gg

  8. #8
    Join Date
    Mar 2010
    Posts
    42

    Re: How to determine if VARIANT contains an Array?

    Hi Codeplug, thanks again for replying!

    Thank you for the links and your reply, it really helped me with SAFEARRAYS. I think that I have one working now.

    Something quite interests me though, when I tried to access the SAFEARRAY element 0, it returns the same value that vtProp.bstrVal[0] returns, which are both "1". In ChassisTypes, "1" is considered as "Other". I think that this is correct since I am running it a Windows PE VM.

    Does this mean that it is possible to access the Array using vtProp.bstrVal[0]?

    Here is my code that I used for SAFEARRAYS. Since it is a VT_14 (Long integer type), I statically stored it to a long data type.

    SAFEARRAY * psa;
    SAFEARRAYBOUND rgsabound[1];

    rgsabound[0].lLbound = 0;
    rgsabound[0].cElements = 5;
    psa = SafeArrayCreate(VT_I4, 1, rgsabound);

    if(psa == NULL)
    cout << "OUT OF MEMORY" << endl;

    cout << "NO ERROR" << endl;

    VariantInit(&vtProp);

    hr = pclsObj->Get(varProperty.bstrVal, 0, &vtProp, 0, 0);

    long ix = 0;
    long toput = 0;

    HRESULT hresult = SafeArrayGetElement(V_ARRAY(&vtProp), &ix, &toput);

    if(FAILED(hresult))
    cout << "CONSIDER IT FAILED" << endl;

    cout << "TO PUT IS: " << toput << endl;

    Do you have any comments regarding this case?

    Thank you for taking the time to help!

  9. #9
    Join Date
    Nov 2003
    Posts
    1,902

    Re: How to determine if VARIANT contains an Array?

    >> Does this mean that it is possible ...
    Anything is possible when you don't use structures/API's correctly. Only access "bstrVal" if "vt" is VT_BSTR.

    The call to SafeArrayGetElement() looks fine, assuming vtProp.vt == VT_ARRAY | VT_I4.

    gg

  10. #10
    Join Date
    Mar 2010
    Posts
    42

    Re: How to determine if VARIANT contains an Array?

    Hi Codeplug,

    Thanks for all the help! It really helped me solved my problem.

    Thanks also for those that took the time to reply!

  11. #11
    Join Date
    Mar 2010
    Posts
    42

    Re: How to determine if VARIANT contains an Array?

    Hi again,

    I'm just wondering; Is it possible to just check if it is an array and then convert it to a string without knowing what type it is? Because it can be of type VT_I4, VT_BSTR, etc. Is it possible that without knowing these types, I can convert the elements of the returned array to string?

    Thank you!

  12. #12
    Join Date
    Nov 2003
    Posts
    1,902

    Re: How to determine if VARIANT contains an Array?

    For a SAFEARRAY variant, the SAFEARRAY API's are what you have to work with. So you'll have to use those API's to process it into what ever representation yourself.

    gg

  13. #13
    Join Date
    Mar 2010
    Posts
    42

    Re: How to determine if VARIANT contains an Array?

    Hi Codeplug,

    Yes. I think I understand what you mean. But sorry, I must have phrased my question in a weird way.

    Currently, what I am doing now is finding out the Variable Type of the elements stored in the SAFEARRAY returned.

    So for example, I got a SAFEARRAY with VT_I4. I will first store the elements I need in a integer variables. Then using these integer variables, I will convert them to string.

    I am wondering if it is possible that I have identified that the VARIANT contains a SAFEARRAY. I do not know the Variable Types that are stored in that array (i.e Long integers, BSTR, etc). Is it possible to directly convert them to string? Something like storing them in a Generic Variable then converting that generic variable to a string?

    Thank you!

  14. #14
    Join Date
    Nov 2003
    Posts
    1,902

    Re: How to determine if VARIANT contains an Array?

    >> Something like storing them in a Generic Variable then converting that generic variable to a string?
    The closest thing to it would be to store each of the SAFEARRAY members into a VARIANT (which requires you to set the "vt" member and the appropriate data member based on that type). Then use VariantChangeType() to convert it into a VT_BSTR (if it isn't already).

    In this way, you can convert each element of a SAFEARRAY into a VT_BSTR VARIANT - giving you a string representation of that SAFEARRAY element.

    gg

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured