JNA - 如何将 `char **devices` PointerByReferences 转换为 `char **devices` PointerByReference
JNA - How to turn `char ***devices` PointerByReferences to `char **devices` PointerByReference
这里是c头文件:
idevice_error_t idevice_get_device_list(char ***devices, int *count);
idevice_error_t idevice_device_list_free(char **devices);
这是 JNAerator 为我生成的 JNA:
int idevice_get_device_list(PointerByReference devices, IntBuffer count);
int idevice_device_list_free(PointerByReference devices);
下面是我的使用方法。它在 Kotlin 中,但它也等同于您在 Java 中所做的:
fun getDeviceList(): List<String> {
val deviceUuidsPbr = PointerByReference()
val deviceSizeBuffer = IntBuffer.wrap(IntArray(1))
val resultInt = LibIMobileDeviceLibrary.INSTANCE.idevice_get_device_list(deviceUuidsPbr, deviceSizeBuffer)
val size = deviceSizeBuffer.get()
logger.v {"getDeviceList $resultInt, Size: $size" }
val stringArrayP = deviceUuidsPbr.value
val devices = stringArrayP
.getStringArray(0, size)
.toList()
logger.v { "Devices: $devices" }
LibIMobileDeviceLibrary.INSTANCE.idevice_device_list_free(deviceUuidsPbr)
return devices
}
当我释放内存时,一切都崩溃了:
LibIMobileDeviceLibrary.INSTANCE.idevice_device_list_free(deviceUuidsPbr)
免费想要 char **devices
,但我提交 char ***devices
。我应该如何将 PointerByReference
转换为正确的格式?
A PointerByReference
只是一个指针(指向一个指针)。被映射的两个 C 函数只是具有额外的间接级别。
考虑这个函数定义:
device_get_device_list(char ***devices, int *count);
idevice_device_list_free(char **devices);
现在考虑 foo
定义为 *devices
并且原始函数简化为:
device_get_device_list(char **foo, int *count);
idevice_device_list_free(char *foo);
如果您将 bar
定义为 *foo
,您将得到:
device_get_device_list(char *bar, int *count);
idevice_device_list_free(char bar);
所以你不能将你从 device_get_device_list
(*bar
) 收到的 PointerByReference
直接传递给 idevice_device_list_free
(需要 bar
);你传递它的指向值(恰好是指向另一个指针的指针,但这并不重要。)
在 Java 中,您只需将 idevice_device_list_free()
调用中的参数从 deviceUuidsPbr
更改为 deviceUuidsPbr.getValue()
。
我不是 Kotlin 用户,但它似乎基于您需要的其他代码deviceUuidsPbr.value
。
这里是c头文件:
idevice_error_t idevice_get_device_list(char ***devices, int *count);
idevice_error_t idevice_device_list_free(char **devices);
这是 JNAerator 为我生成的 JNA:
int idevice_get_device_list(PointerByReference devices, IntBuffer count);
int idevice_device_list_free(PointerByReference devices);
下面是我的使用方法。它在 Kotlin 中,但它也等同于您在 Java 中所做的:
fun getDeviceList(): List<String> {
val deviceUuidsPbr = PointerByReference()
val deviceSizeBuffer = IntBuffer.wrap(IntArray(1))
val resultInt = LibIMobileDeviceLibrary.INSTANCE.idevice_get_device_list(deviceUuidsPbr, deviceSizeBuffer)
val size = deviceSizeBuffer.get()
logger.v {"getDeviceList $resultInt, Size: $size" }
val stringArrayP = deviceUuidsPbr.value
val devices = stringArrayP
.getStringArray(0, size)
.toList()
logger.v { "Devices: $devices" }
LibIMobileDeviceLibrary.INSTANCE.idevice_device_list_free(deviceUuidsPbr)
return devices
}
当我释放内存时,一切都崩溃了:
LibIMobileDeviceLibrary.INSTANCE.idevice_device_list_free(deviceUuidsPbr)
免费想要 char **devices
,但我提交 char ***devices
。我应该如何将 PointerByReference
转换为正确的格式?
A PointerByReference
只是一个指针(指向一个指针)。被映射的两个 C 函数只是具有额外的间接级别。
考虑这个函数定义:
device_get_device_list(char ***devices, int *count);
idevice_device_list_free(char **devices);
现在考虑 foo
定义为 *devices
并且原始函数简化为:
device_get_device_list(char **foo, int *count);
idevice_device_list_free(char *foo);
如果您将 bar
定义为 *foo
,您将得到:
device_get_device_list(char *bar, int *count);
idevice_device_list_free(char bar);
所以你不能将你从 device_get_device_list
(*bar
) 收到的 PointerByReference
直接传递给 idevice_device_list_free
(需要 bar
);你传递它的指向值(恰好是指向另一个指针的指针,但这并不重要。)
在 Java 中,您只需将 idevice_device_list_free()
调用中的参数从 deviceUuidsPbr
更改为 deviceUuidsPbr.getValue()
。
我不是 Kotlin 用户,但它似乎基于您需要的其他代码deviceUuidsPbr.value
。