Academic Company Events Community Support Solutions Products & Services Contact NI MyNI
8 ratings:
 3 out of 5   

How Do I Extract Information from a SAFEARRAY in Microsoft Visual C++?

Primary Software: Measurement Studio>>Measurement Studio for Visual C++
Primary Software Version: 7.0
Primary Software Fixed Version: N/A
Secondary Software: N/A

I am using the Measurement Studio CWAI object and I want to extract data from the VARIANT passed to the AcquiredData event. How do I do this?

The following example, written in Visual C++, shows how to extract information from the VARIANT.
This text is also in the attached SAFext.c file linked below.
void CMultiContAIinVCDlg::OnAcquiredDataCwai1(VARIANT FAR* ScaledData, 
             VARIANT FAR* BinaryCodes)

 /* vOptional is used when we dont want to pass 
  anything to an optional argument */ 
 COleVariant vOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);   
 /* We're going to use the CWArray.IndexArray function 
  from ComponentWorks to extract Channel 0 from BinaryCodes */ 
 VARIANT firstSignal;    // Channel 0 extracted from BinaryCodes 
 /* To use IndexArray, you must specify which part of the 2D array 
  to extract from BinaryCodes (or ScaledData). You do this by
  creating a 1D SafeArray of two elements- the first is the first
  dimension element to extract, the second is the second dimension
  element to extract.  Specifying NULL causes an entire  
  dimension (row or column in a 2D array) to be extracted */
 VARIANT Array;    // VARIANT to use as a parameter for IndexArray 
 SAFEARRAY* psa;   // SAFEARRAY to specify extraction range 
 SAFEARRAYBOUND rgsabound[1]; 
 VARIANT vNull; 
 long ix[1];  
 /* Create 'psa': a 1D, 2 element array for specifying which part
  of BinaryCodes (or ScaledData) to extract: */ 
 rgsabound[0].lLbound = 0; 
 rgsabound[0].cElements = 2; 
 psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound); 
 // Specify Extraction: 
 // first dimension = channel 0 
 ix[0] = 0; 
 SafeArrayPutElement(psa, ix, COleVariant((long)0)); 
 // second dimension = VT_NULL (extract all scans of channel 0)
 x[0] = 1; 
 vNull.vt = VT_NULL; 
 SafeArrayPutElement(psa, ix, &vNull);  
 // Create VARIANT to contain psa: 
 Array.vt = VT_ARRAY | VT_VARIANT;         
 // Use psa for the VARIANT's SAFEARRAY 
 Array.parray = psa;  
 // Call IndexArray to extract the information: 
 firstSignal = m_CWArray.IndexArray(*BinaryCodes,Array);  
 /* Extract elements from SAFEARRAY into an array of short integers:  
  Make sure 'firstSignal' is a 1D array of short integers
  (for BinaryCodes) or // a 1D array of floats (for ScaledData): */ 

 if(firstSignal.parray->cDims == 1 && firstSignal.vt == (VT_ARRAY | VT_I2))    

   // Buffer to store extracted values in
   short *temparray = (short *) 0;  
   // keeps current position buffer being copied
   short *iptr;        
   int i;     
  	// Find size of firstSignal:  
   int numElements = firstSignal.parray->rgsabound[0].cElements
         - firstSignal.parray->rgsabound[0].lLbound;    
   // initialize array:  
   temparray = new short[numElements];    
   // Get pointer to buffer to copy:  
   SafeArrayAccessData(firstSignal.parray, (void **) &iptr);    
   // Copy Buffer:  
   for(i = 0; iptr &&  i < numElements; i++)   
    temparray[i] = *(iptr+i);    
   // Release pointer to buffer  
   // When finished, release memory used by temparray.  
   delete temparray; 

Related Links:
Developers Zone Tutorial: Working with Variants in Visual Basic

Microsoft SAFEARRAY structure



Report Date: 12/06/2000
Last Updated: 11/29/2011
Document ID: 24599KQT

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit