I need to call a function with a signature like this:
size_t findDevices(devStruct_t **devs[]);
Some example C code uses the function like this:
devStruct_t **arrOfPointers;
size_t size;
size = findDevices(&arrOfPointers);
for(size_t i = 0; i < size; i++) {
printf("devId:%d\n", arrOfPointers[i]->id);
}
How would I replicate the above code in Java using JNA?
I think the JNA function signature should look like this:
NativeLong findDevices(PointerByReference devs);
I can run this without crashing:
NativeLong size;
PointerByReference stdevs = new PointerByReference();
size = libstlink.stlink_probe_usb(stdevs);
But, I don't know how to access the structures.
function definition, as you can see in source code is
size_t stlink_probe_usb(stlink_t **stdevs[]);
That type is defined into this source code
struct _stlink {
struct _stlink_backend *backend;
void *backend_data;
// Room for the command header
unsigned char c_buf[C_BUF_LEN];
// Data transferred from or to device
unsigned char q_buf[Q_BUF_LEN];
int q_len;
// transport layer verboseness: 0 for no debug info, 10 for lots
int verbose;
uint32_t core_id;
uint32_t chip_id;
int core_stat;
char serial[16];
int serial_size;
enum stlink_flash_type flash_type;
stm32_addr_t flash_base;
size_t flash_size;
size_t flash_pgsz;
/* sram settings */
stm32_addr_t sram_base;
size_t sram_size;
// bootloader
stm32_addr_t sys_base;
size_t sys_size;
struct stlink_version_ version;
};
You have the address of the start of the array in your "returned" pointer ( stdevs.getValue()
). Use that to extract the array of pointers, then initialize your structures from that.
Pointer[] ptrs = stdevs.getValue().getPointerArray(0, size.intValue());
DevStruct[] array = new DevStruct[size.intValue()];
for (int i=0;i < array.length;i++) {
array[i] = new DevStruct(ptrs[i]);
}
public class DevStruct extends Structure {
public DevStruct(Pointer p) {
super(p);
read();
}
}
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.