简体   繁体   中英

Passing 2D Array argument with JACOB

I have a COM method I'm trying to invoke, where there's an argument of type 'object' which must be a 2D double safe array, a collection of lat/long points. How can I create a SafeArray in JACOB to send through the COM interface?

I've tried just passing a 2D array as an object in the object list. The method doesn't return error, but I do not see the results I expect in FalconView (rendering of the polygon).

    double polyPoints[][] = new double[5][2];
    polyPoints[0][0] = 75.3;
    polyPoints[0][1] = 4.5;     
    polyPoints[1][0] = 3.8;
    polyPoints[1][1] = 4.8;
    polyPoints[2][0] = 2.3;
    polyPoints[2][1] = 2.5;
    polyPoints[3][0] = 5.3;
    polyPoints[3][1] = 6.5;
    polyPoints[4][0] = 0.3;
    polyPoints[4][1] = -1.5;

// Can't recreate Variant or SafeArray from double[x][y] array;

    Object[] polygonArgs = new Object[] {m_mainLayerHandle, polyPoints, 1};
    Variant returnAddPolygon = Dispatch.invoke(mainLayerDispatch, "AddPolygon", Dispatch.Method, polygonArgs,  new int[1]);
    System.out.println("Polygon Handle: " + returnAddPolygon.getInt());

    Object[] refreshArgs = new Object[] {m_mainLayerHandle};
    Variant refreshVariant = Dispatch.invoke(mainLayerDispatch, "Refresh", Dispatch.Method, refreshArgs,  new int[1]);

The second arument documentation:

lat_lon_array a two dimensional SAFEARRAY of doubles. The first dimension contains the latitude values. The second dimension contains the longitude values

It seems that SafeArray supports 1 Dimensional, 2 Dimensional, and N-Dimensional arrays using some somewhat unclear constructors. Given the 2D double array I created above, I was able to copy the data in to a 2D Double Safe Array. It would certainly be more efficient to create the double[][] up front, but I'm doing this in some prototype code. There may be ways to copy entire arrays in to the safe array... I am not sure.

// 2D array of type double. First dimension size 5, second dimemnsion size 2.
SafeArray safeArray = new SafeArray(Variant.VariantDouble, 5, 2);
for(int i = 0; i < 5; i++) {
    for (int j = 0; j < 2; j++) {
            // set the value of safearray[i][j] to value polyPoints[i][j]
        safeArray.setDouble(i, j, polyPoints[i][j]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM