-1

How can I put VARIANT into COleSafeArray? I tried

Variant vara;
COleSafeArray force= vara;

and got "debug assertion failed"

I used a library to convert voltage value to Force value. In Library, there is below function:

VARIANT _Calibration::ConvertToFT(VARIANT* Voltages, LPCTSTR ForceUnits, LPCTSTR TorqueUnits)
{
    VARIANT result;
    static BYTE parms[] =
        VTS_PVARIANT VTS_BSTR VTS_BSTR;
    InvokeHelper(0x60030040, DISPATCH_METHOD, VT_VARIANT, (void*)&result, parms,
        Voltages, ForceUnits, TorqueUnits);
    return result;
}

So in my main program:

COleSafeArray voltagesk;
double voltages[6];
_read-voltage(voltages);
long y;
    for (y = 0; y<6; y++) {
        // COleSafeArrays use pass-by-reference methods to read or write
        voltagesk.PutElement(&y, &(voltages[y]));
    }
COleSafeArray forces = GetActiveCalibration().ConvertToFT(voltagesk, L"lb", L"in-lb");
for (y = 0; y<6; y++) {
        // COleSafeArrays use pass-by-reference methods to read or write
        forces.GetElement(&y, &(voltages[y]));

I tried deleting this line"COleSafeArray forces = GetActiveCalibration().ConvertToFT(voltagesk, L"lb", L"in-lb");", my program ran well, but cannot convert voltage into force so I think that converting from VARIANT to COleSafeArray, has problem

abcd
  • 11
  • 5
  • What kind of data is in `vara`? `COleSafeArray` constructor only takes a variant that itself wraps a safearray (in which case, the safearray is copied over). If instead you want to add the variant as an element to the new array, you'd need something like `COleSafeArray force; force.CreateOneDim(VT_VARIANT, someSize); long index = 0; force.PutElement(&index, &vara);` – Igor Tandetnik Jan 01 '17 at 17:11
  • *"Debug assertion failed"* wasn't all you got. The [assertion failed dialog box](https://msdn.microsoft.com/en-us/library/05d5fakw.aspx) also offered to break into the source code (by pressing *"Retriy"*). Doing so will take you right to the expression, that failed to evaluate to true. You also get the entire call stack leading up to that point. Don't ignore this helpful information, and include in your question instead. – IInspectable Jan 01 '17 at 17:40
  • Igor Tandetnik I edited my question with more info about my code. I also tried your recommendation. IInspectable, callstack showed that "Frames below maybe incorrect/ ormissing" – abcd Jan 02 '17 at 04:05
  • That's because you don't have debug symbols. Go get them. See [Debugging with Symbols](https://msdn.microsoft.com/en-us/library/windows/desktop/ee416588.aspx) for instructions. – IInspectable Jan 02 '17 at 20:23
  • Have you actually tried what @IgorTandetnik told you? You are not creating your COleSafeArray correctly and he has clearly outlined how in his comment. – Andrew Truckle Jan 17 '17 at 15:32
  • I don't see how your code could possibly work. `voltagesk.PutElement()` should be failing already, as `voltagesk` is empty - any index would be out of bounds. The code you actually run must be different from what you show. – Igor Tandetnik Jan 17 '17 at 15:55

1 Answers1

0

Thanks for your opinions. Actually, this code for purpose converting from voltage into measurement value. voltagesk is not empty because _read-voltage(voltages) put values into voltages. "GetActiveCalibration().ConvertToFT" read calibration info from calibration file and convert voltage into desired value ( following rule of calibration file) I changed my code following:

long y;

Double tempc[6];

_read (voltages);

for (y = 0; y<7; y++) {

    voltagesk.PutElement(&y, &(voltages[y]));
}
VARIANT vara= GetActiveCalibration().ConvertToFT(voltagesk, L"N", L"N-m");

 //returned value type of convertToFT is VARIANT
  COleSafeArray forces;
forces.Create(VT_R8, 1, numElements);
for (y = 0; y<6; y++) 
    {
        forces.PutElement(&y, &vara);
    }
for (y = 0; y<6; y++) {

    forces.GetElement(&y, &tempc[y]); // tempc is the converted value 

    } 

my program ran well, however the converted value is very low ( 2.3333E-1233). I think that type"VT_R8" has a problem in QT. I changed method. Instead using code read calibration file, I read and understand it by myself. After that, I re-wrote my program well and simply for converting purpose without COleSafeArray

abcd
  • 11
  • 5