How to obtain a new Pointer in Java?

前端 未结 3 1815
闹比i
闹比i 2021-01-18 00:07

How can I call a method with this method signature in C from JNA?

int open_device(context *ctx, device **dev, int index);

The last two line

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-18 00:21

    Even better answer. You may want to allocate (malloc etc) depending upon Java String length. Example below is unit test from JNA project.

    public void testGetSetStringWithDefaultEncoding() throws Exception {
        final String ENCODING = Native.DEFAULT_ENCODING;
        String VALUE = getName();
        int size = VALUE.getBytes(ENCODING).length+1;
        Memory m = new Memory(size);
        m.setString(0, VALUE);
        assertEquals("Wrong decoded value", VALUE, m.getString(0));
    }
    

提交回复
热议问题