locked
How to access data inside the SAFEARRAY RRS feed

  • Question

  • hi, while I'm doing my program, I come across to receive a SAFEARRAY variable, and I read some article about it, but I still couldn't figure out how to access the data inside the SAFEARRAY. Here is my code

    SAFEARRAY *someSafeArray = NULL;
    HRESULT hr = piInterfaceType_A->get_SafeArray(&someSafeArray);
    if(SUCCEEDED(hr))
    {
        hr = SafeArrayAccessData(someSafeArray, (void**) &piInterfaceType_B);
        if(SUCCEEDED(hr))
        {
            piInterfaceType_B->getProperty_1(); // got access violation here
            piInterfaceType_B->getProperty_2(); // got access violation here
            ........
        }


    I come across the problem with getProperty_1 and getProperty_2, which I got access violation.

    Can anyone help me please.
    Bo Chen Wu
    Friday, September 5, 2008 12:45 PM

Answers

  • to access the data from a safe array:
    1. you must know the number of dimentions it has
    use SafeArrayGetDim()

    int iDim = SafeArrayGetDim(someSafeArray);

    2. You have to know bounds, lets read them into the minIndex and maxIndex,
    lets assume that your array is 1 dimentional array

    long minIndex, maxIndex;
    SafeArrayGetUBound
    (someSafeArray, 0, &maxIndex);
    SafeArrayGetLBound(someSafeArray, 0, &minIndex);

    3. Lets get the data type of the element in the array
    VARTYPE vt;
    SafeArrayGetVartype(someSafeArray, &vt);

    4. Lets assume yours is an array of ULONG
    ULONG *pData;
    if(vt ==VT_UI4)
    {
        SafeArrayAccessData
    (someSafeArray, &pData);
        for (int i = minIndex; i < maxIndex; i++)
       {
           printf("%d", pData[i]);
       }
      SafeArrayUnaccessData(someSafeArray);
    }


    Regards,
    Pratap



    • Proposed as answer by Raja Pratap Reddy Friday, September 5, 2008 1:00 PM
    • Marked as answer by Yan-Fei Wei Tuesday, September 9, 2008 9:03 AM
    Friday, September 5, 2008 1:00 PM