ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.



¿Cómo Extraigo Información de un SAFEARRAY en Microsoft Visual C++?



Software Primario: Measurement Studio>>Visual C++ Support
Versión de Software Primario: 7.0
Versión de Software Primario Corregido: N/A
Software Secundario: N/A

Problema:
Estoy utilizando un objeto CWAI de Measurement Studio y quiero extraer los datos desde el VARIANT que se pasa al evento AcquiredData. ¿Cómo hago esto?


Solución:
El siguiente ejemplo, escrito en Visual C++ muestra como extraer la información del VARIANT.
El texto también se encuentra en el archivo anexo SAFext.c.
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: 
 // VARIANT type is a SAFEARRAY of VARIANTS 
 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  
   SafeArrayUnaccessData(firstSignal.parray);   
  
   // When finished, release memory used by temparray.  
   delete temparray; 
  }
}



Ligas Relacionadas:

Archivos Adjuntos:


SAFEext.c - SAFEext.c


Día del Reporte: 12/06/2000
Última Actualización: 01/07/2010
Identificación del Documento: 24599KQT