2D SafeArrays- input data

This is a discussion on 2D SafeArrays- input data within the Windows Programming forums, part of the Platform Specific Boards category; I have a 2d safearray obtained from a variant, that needs to be populated with long values I access the ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    10

    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:
    Code:
    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
    Code:
     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:

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

    Will

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    10
    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.

    Code:
    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;
    ipInRasProps->get_NoDataValue(&vInNodatavalue);
    ipOutputRasProps->get_NoDataValue(&vOutNodatavalue);
    
    	//parses nodata variant
    double dInNodatavalue = VDouble(vInNodatavalue, iPT);
    hr = SafeArrayAccessData(pData, (void HUGEP* FAR*)&pDbl);
    
    for (i=0; i<(lHeight - 1); i++)
    {
    ij[0]=i;
          for (j=0; j<(lWidth - 1); j++)
    		  {
    		  ij[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;
    			}
    			else
    			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
    Location
    near a computer
    Posts
    3,856
    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
    Posts
    10
    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?!
    Code:
    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, 03:09 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  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 Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21