Link to home
Start Free TrialLog in
Avatar of Brian Dumas
Brian DumasFlag for United States of America

asked on

Passing a VARIANT to a COM function and returning it.

I have a C++ class that needs to get information from an oscilloscope for analysis. I have the code that issues the command to the o'scope and can see the o'scope getting the info. Here is that code:

void CbeLecroy::GetScaledWF(LCHAN channel, int numpoints, VARIANT* rvals)
{
	waveform.Attach(activeDSO->GetScaledWaveform(bCHANNEL[channel], numpoints, 0));
	rvals->parray = waveform.parray;
	float* pfloatVals;
	HRESULT hr;
	hr = SafeArrayAccessData(rvals->parray, (void**)&pfloatVals); // direct access to SA memory

	if (SUCCEEDED(hr))
	{
		long lowerBound, upperBound;  // get array bounds
		SafeArrayGetLBound(rvals->parray, 1, &lowerBound);
		SafeArrayGetUBound(rvals->parray, 1, &upperBound);

		fvec.clear();

		long cnt_elements = upperBound - lowerBound + 1;

		for (int i = 0; i < cnt_elements; ++i)  // iterate through returned values
			fvec.push_back(pfloatVals[i]);

		SafeArrayUnaccessData(rvals->parray);
	}
//	SafeArrayDestroy(psa);
}

Open in new window


The calling function looks like this:
    VARIANT rvals;
	comLecroy->GetScaledWF(channel, numpoints, &rvals);

Open in new window


When I breakpoint in the top function, I can see valid data in the loop:
		for (int i = 0; i < cnt_elements; ++i)  // iterate through returned values
			fvec.push_back(pfloatVals[i]);

Open in new window


However, when I exit the called function and return to the calling function, I get an exception. Any ideas why?

Thanks, Brian
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial