3

I am facing problem in retrieving array data from a WMI class using c++.

In the following link, http://msdn.microsoft.com/en-us/library/aa390423(v=vs.85).aspx, step 7 of the example shows us how to retrieve a string value of the wmi query ran. However, I am currently facing an issue when the value returned from the query is an uint16 array.

eg.
Query: "Select ChassisTypes from Win32_SystemEnclosure"


Result:
_GENUS : 2
_CLASS : Win32_SystemEnclosure
_SUPERCLASS:
_DYNASTY:
_RELPATH:
_PROPERTY_COUNT: 1
_DERIVATION: {}
_SERVER:
_NAMESPACE:
_PATH:
ChassisTypes: {3}

May I ask how do I actually process the result to retrieve the integer value of ChassisType from the array in this case? Thank you!

Brumble
  • 71
  • 1
  • 6

1 Answers1

6

To access the array values from a WMI property you can use the SafeArrayGetElement, SafeArrayGetLBound and SafeArrayGetUBound methods.

Try this sample

VARIANT vtProp;
hr = pclsObj->Get(L"ChassisTypes", 0, &vtProp, 0, 0);// Uint16
if (!FAILED(hr))
{
    if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY))
        wcout << "ChassisTypes : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl;
    else
        if ((vtProp.vt & VT_ARRAY))
        {
            wcout << "ChassisTypes : "  << endl;
            long lLower, lUpper; 
            UINT32 Element = NULL;
            SAFEARRAY *pSafeArray = vtProp.parray; 
            SafeArrayGetLBound(pSafeArray, 1, &lLower);
            SafeArrayGetUBound(pSafeArray, 1, &lUpper);

            for (long i = lLower; i <= lUpper; i++) 
            {
                hres = SafeArrayGetElement(pSafeArray, &i, &Element);
                wcout << Element<< endl;
            }

            SafeArrayDestroy(pSafeArray);                 
        }
        VariantClear(&vtProp);
        pclsObj->Release();
        pclsObj=NULL;
}
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • Thank you RRUZ! Now I have to find how to convert that unsigned interger variable "Element" to string properly. – Brumble Mar 24 '14 at 01:50