This Question is Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
2 Replies Last post: Nov 20, 2009 8:29 PM by Steven Drinovsky  
Steven Drinovsky   2 posts since
Nov 20, 2009
Currently Being Moderated

Nov 20, 2009 5:26 PM

converting double[] to variant

Hello, I am trying to replace my home grown ocx wrapper code with JNIWrapper.

Most is working but I can not figure out how to do the following.

 

Here is my current jni code which is working:

 

JNIEXPORT void JNICALL Java_...
  (JNIEnv *env, jobject, jlong ref, jdoubleArray param)
{
    double *array = (double *)env->GetPrimitiveArrayCritical(param, 0);

 

    VARIANT v;
    v.vt=VT_R8 | VT_BYREF;
    v.pdblVal = array;

 

    ...

    pass to ocx

    ...

 

    env->ReleasePrimitiveArrayCritical(param, array, JNI_ABORT);
}

 

When I run the ocx through the generator it says the params are DoubleFloat. for example:

 

    /**
     *
     */
    Int32 addPolymarker(
        Int32 /*[in]*/ numPoints,
        DoubleFloat /*[in,out]*/ xyPoints);

 

In Java that DoubleFloat is really a double[].

 

Also, I know it is not a SafeArray, I already tried that with my original code.

 

Thanks.

Steven

Serge Piletsky TeamDev Ltd. 1,066 posts since
Apr 24, 2006
Currently Being Moderated
1. Nov 20, 2009 8:15 PM in response to: Steven Drinovsky
Re: converting double[] to variant

Hi Steven,

 

Actually it’s not possible to create a DoubleFloat object form array of double values directly. One of possible ways to convert double[] array to the appropriate pointer is to use PrimitiveArray of DoubleFloat objects, for example:

double array[] = {1.0, 2.0, 3.0};
PrimitiveArray arrayParameter = new PrimitiveArray(DoubleFloat.class, array.length);
for (int i = 0; i < array.length; i++) {
    arrayParameter.setElement(i, new DoubleFloat(array[i]));
}
 
Pointer arrayPtr = new Pointer(arrayParameter);

It’s also possible to set this array pointer to a Variant value, for example:

Variant variant = new Variant();
arrayPtr.castTo(variant.getPdblVal());
variant.getVt().setValue(VarType.VT_R8 | VarType.VT_BYREF);

The concern in this case is that Variant type does not seem to be designed for passing of arrays in such way. Our guess is that such approach could probably work only in one case – when you pass this pointer to an inproc component which shares the same address space. But if this Variant goes out of scope of a process then COM marshaler will leave only a first element of array.

               
-Serge

More Like This

  • Retrieving data ...