Thread: 2D SafeArrays- input data

  1. #1
    Registered User
    Join Date
    May 2006

    2D SafeArrays- input data

    I have a 2d safearray obtained from a variant, that needs to be populated with long values
    I access the data:
    hr = SafeArrayAccessData(pData, (void HUGEP* FAR*)&pDbl);
    Here I need to input the data at it's correct position, but I cannot get this to work properly. The function
     pDbl[i*j] = val
    runs but I get random results (the values I get in the output resemble very loosely what is expected).

    And I unaccess the data:

    hr = SafeArrayUnaccessData(pData);
    Please help, my thesis in fact hinges on this working!


  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Well, either you're accessing the wrong part of the array or the array wasn't allocated properly (which is most likely).

    Post some more code. We can't make much out from what you have.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    May 2006
    Ok, here's some more. I get a blank variant array from a pixelblock object (ArcGIS API) and want to populate it using values from another pixelblock.

    VARIANT vOutPixelArray;
    ipOutputBlock3->put_PixelType(0, outPixelType);
    // Get the SafeArray associated with the first band of output
    ipOutputBlock3->get_PixelData(0, &vOutPixelArray);
    SAFEARRAY *pData = (vOutPixelArray.parray); 
    VARIANT v, vOut, vInNodatavalue, vOutNodatavalue;
    long HUGEP *pDbl;
    // QI RasterProps for output for NoData handling
    IRasterPropsPtr ipOutputRasProps; 
    ipOutputRasProps = ipOutBand;
    //Loop through the SafeArray and calculate each pixel value according to the neighborhood-notation
    long A, i, j;
    long ij[2];
    double getvalM;
    double dVal;
    	//parses nodata variant
    double dInNodatavalue = VDouble(vInNodatavalue, iPT);
    hr = SafeArrayAccessData(pData, (void HUGEP* FAR*)&pDbl);
    for (i=0; i<(lHeight - 1); i++)
          for (j=0; j<(lWidth - 1); j++)
          ipInputBlock3->GetVal(0, i, j, &v);
    		  //extracts value from variant, discriminating between types
    	   	dVal = VDouble(v, iPT);
          if (dVal == dInNodatavalue)
    			hr = SafeArrayPutElement(vOutPixelArray.parray, ij, &vOutNodatavalue);
    				//pDbl[i*j] = dVal;
    			if ((j + 1) < (lWidth))
    					//getvalS = ipInputBlock->GetVal(0, i, j, &v);
    					dVal = dVal + 10;
    					//vOut.vt = VT_I4
    					vOut.vt = VT_I4;
    					vOut.lVal = dVal;
    					//getvalM = vOut.lVal;
    					long lVal = dVal;
    					pDbl[i*j] = lVal;
    				//	hr = SafeArrayPutElement(vOutPixelArray.parray, ij, &vOut);
    	hr = SafeArrayUnaccessData(pData);

  4. #4
    train spotter
    Join Date
    Aug 2001
    near a computer
    have you tried using as an index

    iCurrentHeight*iTotalWidth + iCurrentWidth
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    May 2006
    I finally found the answer. For some reason I have to get a pointer to a pointer of a safearray, then use indirection to get a pointer?!
    long *pMod
    SAFEARRAY *pData = *(vOutPixelArray.pparray); 
    hr = SafeArrayLock(pData);
    //iterate through image(code removed)
    		hr = SafeArrayPtrOfIndex(pData, ij, (void **)&pMod);
    		if (FAILED(hr)) AfxMessageBox("nope");
    		(*pMod) = dVal;//gives a pixel a value
    hr = SafeArrayUnlock(pData);//unlocks the safearray
    So simple! when you decode the MSDN "documentation".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. C diamonds and perls :)
    By Carlos in forum A Brief History of
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM