Visual C++의 SAFEARRAY에 대한 정확한 정보를 알려주십시오.

주요한 소프트웨어: Measurement Studio Add-ons
주요한 소프트웨어 버전: 1.0
문제가 해결된 소프트웨어 버전: N/A
부차적인 소프트웨어:

문제점: ComponentWorks CWAI를 사용하고 있는데 AcquiredData 이벤트에서 넘겨지는 VARIANTs 타입의 변수에 대해서 정확히 알고 싶습니다.

솔루션: 아래 예제는 VARIANTs 타입의 변수의 정보를 얻을 수 있도록 C++ 코드로 작성되었습니다. (이 코드는 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];
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)
ix[0] = 1;
vNull.vt = VT_NULL;
SafeArrayPutElement(psa, ix, &vNull);

// Create VARIANT to contain psa:

// 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)) // first Signal is a SafeArray of shorts
short *temparray = (short *) 0; // Buffer to store extracted values in
short *iptr; // keeps current position buffer being copied
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;

관련 링크:


리포트 날짜: 12/06/2000
마지막 업데이트: 11/19/2003
문서 번호: 24599KQT