java - JNA how do I go from a C int* to a JNA struct -


i have following c code trying map java:

int32 query( void* qrybuffer,              uint32_t qrylength) {   static int8_t  sendbuffer[max_request_size];   message*  querymessage; ...   querymessage = (message*)sendbuffer;    memcpy( &sendbuffer[0], &qrylength, sizeof(qrylength) );   memcpy( &sendbuffer[sizeof(qrylength)], qrybuffer, qrylength ); ...   query_ex(querymessage); }  typedef struct {                                    uint32      length_u;        } message; 

the jna equivalent message struct is:

public static class message extends structure {     public int length_u;     public message() {         super();     }     protected list getfieldorder() {         return arrays.aslist("length_u");     }     public message(int length_u) {         super();         this.length_u = length_u;     }     public static class byreference extends omni_message implements structure.byreference {     };     public static class byvalue extends omni_message implements structure.byvalue {     }; }; 

i know doing in c code, cannot figure out how on java side. need have java message class contain length , array of parameters passed in

please help!

update: have created own copy of message struct holds byte[]:

    public static class messageex extends structure {         public byte[] buffer; ...     } 

i going try , pass struct in oppose other one, cannot test until tomorrow.

int size = struct.size(); byte[] buffer = new byte[size]; bytebuffer bb = bytebuffer.wrap(buffer); bb.putint(size); bb.put(struct.getpointer().getbytearray(0, size)); messageex msg = new messageex(buffer); query_ex(msg); // call native world 

you don't need complication of structures here, given buffer pointer can explicitly write need. native code doing writing buffer's length followed contents buffer:

byte[] buffer; int length; pointer msg_buffer = new memory(buffer.length + 4);  msg_buffer.setint(0, length); msg_buffer.setbytearray(4, buffer, buffer.length); 

of course, can pre-allocate fixed buffer instead of creating new 1 every time (which native code does).


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -