简体   繁体   English

使用JNA将C数组返回给Java

[英]Returning C array to Java using JNA

I am not too familiar with C, but I need to use a C library in my java code. 我对C不太熟悉,但我需要在我的java代码中使用C库。 I have created the DLL and am able to access it just fine, but I am attempting to return an array of ints from the C code to the java code. 我已经创建了DLL并且能够很好地访问它,但我试图从C代码返回一个int数组到java代码。

In CI thought you could simply return a pointer to the array, but it's not working like I expect in my Java code. 在CI中,你可以简单地返回一个指向数组的指针,但它在我的Java代码中并没有像我期望的那样工作。 Here's the C code: 这是C代码:

int * getConusXY(double latitude, double longitude) {
    maparam stcprm;
    double reflat = 25, reflon = -95,
            lat1 = 20.191999, lon1 = -121.54001,
            x1 = 0, y1 = 0, x2 = 1073, y2 = 689,
            gsize = 5.079, scaLat = 25, scaLon = -95, orient = 0;
    double x, y;
    int* xy;

    xy = malloc(2 * sizeof *xy);

    stlmbr(&stcprm, reflat, reflon);
    stcm1p(&stcprm, x1, y1, lat1, lon1, scaLat, scaLon, gsize, orient);
    cll2xy(&stcprm, latitude, longitude, &x, &y);

    xy[0] = (int) x;
    xy[1] = (int) y;

    return xy;
}

If I test this in C++ by doing 如果我用C ++在C ++中测试它

int* xy = getConusXY(33.92, -84.33);
cout << xy[0] << " " << xy[1] << endl;

then it works fine and I get the values 739, 255 like expected. 然后它工作正常,我得到的值739,255像预期的那样。

I try using it in Java with the JNA package like so (but this gives me 739, -16777214): 我尝试在Java中使用它与JNA包这样(但这给了我739,-16777214):

public class DmapFDllLibrary {
    interface DmapFLibrary extends Library {
        DmapFLibrary INSTANCE = (DmapFLibrary) Native.loadLibrary("DmapFDll",
                DmapFLibrary.class);

        IntByReference getConusXY(double latitude, double longitude);
    }

    public static void main(String... strings) {
        IntByReference xy_ref = DmapFLibrary.INSTANCE.getConusXY(33.92, -84.33);
        Pointer p = xy_ref.getPointer();
        System.out.println(p.getInt(0) + " " + p.getInt(1));
    }
}

In the JNA documentation it says primitive arrays such as int *buf would be mapped to int[] buf in Java, but when I try changing the return type from IntByReference to int[] then I get an illegalArgumentException. 在JNA文档中,它说原始数组(如int *buf将映射到Java中的int[] buf ,但是当我尝试将返回类型从IntByReferenceint[]我得到一个illegalArgumentException。

I don't know if I'm returning the array correctly from C or if I'm just not accessing it correctly in Java. 我不知道我是否正确地从C返回数组,或者我是不是在Java中正确访问它。 Any help would be appreciated. 任何帮助,将不胜感激。

The C code is fine should be better (see other comments), however, the main issue is that I was misusing the java getInt() method for the pointer. C代码是罚款应该会更好(见其他意见),然而,主要的问题是,我被滥用的Java getInt()为指针的方法。 It seems I should've been using the getIntArray() . 看来我应该一直在使用getIntArray()

I would not use such a function, because the returned array will never be freed/deleted. 我不会使用这样的函数,因为永远不会释放/删除返回的数组。 I would rather change the C function if I can to: 如果我能这样做,我宁愿改变C函数:

void myFunc(Pointer resuls, int numBytes, byte const * returnArray)

The longer and perhaps more clear explanation is that you prototype your Java function to receive Pointer (or a successor of Pointer) - 更长,也许更清晰的解释是你将Java函数原型化为接收指针(或指针的后继者) -

byte[] myFunc(Pointer result, int numBytes);

When creating the callback itself, you use getByteArray(int), or one of the other getArrays. 在创建回调本身时,使用getByteArray(int)或其他getArrays之一。

byte[] myFunc(Pointer resuls, int numBytes)
{
     return Pointer.getByteArray(numBytes);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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