You can use JNIWrapper to map a C structure to Java and call a function from a DLL.
The shell for this structure will look as shown below (values ββfor the size constants need to be changed):
import com.jniwrapper.*; public class IpjIriDevice extends Structure { private static final int IPJ_RECEIVE_BUFFER_SIZE = 0; private static final int IPJ_TRANSMIT_BUFFER_SIZE = 0; private Pointer.Void reader_context = new Pointer.Void(); private Pointer.Void reader_identifier = new Pointer.Void(); private UInt32 receive_timeout_ms = new UInt32(); private UInt8 sync_state = new UInt8(); private Bool wait_for_response = new Bool(); private UInt32 frame_length = new UInt32(); private UInt32 receive_index = new UInt32(); private PrimitiveArray receive_buffer = new PrimitiveArray(UInt8.class, IPJ_RECEIVE_BUFFER_SIZE); private PrimitiveArray transmit_buffer = new PrimitiveArray(UInt8.class, IPJ_TRANSMIT_BUFFER_SIZE); public IpjIriDevice() { init(new Parameter[] { reader_context, reader_identifier, receive_timeout_ms, sync_state, wait_for_response, frame_length, receive_index, receive_buffer, transmit_buffer }); } public long getReaderContext() { return reader_context.getValue(); } public long getReaderIdentifier() { return reader_identifier.getValue(); } public long getReceiveTimeoutMs() { return receive_timeout_ms.getValue(); } public void setReceiveTimeoutMs(long value) { receive_timeout_ms.setValue(value); } public long getSyncState() { return sync_state.getValue(); } public void setSyncState(long value) { sync_state.setValue(value); } public boolean getWaitForResponse() { return wait_for_response.getValue(); } public void setWaitForResponse(boolean value) { wait_for_response.setValue(value); } public long getFrameLength() { return frame_length.getValue(); } public void setFrameLength(long value) { frame_length.setValue(value); } public long getReceiveIndex() { return receive_index.getValue(); } public void setReceiveIndex(long value) { receive_index.setValue(value); } public PrimitiveArray getReceiveBuffer() { return receive_buffer; } public PrimitiveArray getTransmitBuffer() { return transmit_buffer; } public Object clone() { IpjIriDevice result = new IpjIriDevice(); result.initFrom(this); return result; } }
If you need a pointer to an instance of the structure, you must create an instance of the pointer class:
IpjIriDevice structureInstance = new IpjIriDevice(); Pointer structurePtr = new Pointer(structureInstance);
After that, you can use the pointer instance to pass the parameter to the function. The following code demonstrates how to load a library and call a function from it:
DefaultLibraryLoader.getInstance().addPath(LIB_PATH); Library library = new Library(LIB_NAME); Function function = library.getFunction(FUNCTION_NAME); long errorCode = function.invoke(returnValue, structurePtr);
If the structure is changed after the call, all changes will be available in the structureInstance object.
As you can see, in this case you do not need to write additional native code.
More information on using JNIWrapper can be found in the programmer's guide . In addition, there is a JNIWrapper forum containing answers to many popular questions.
Anna Dolbina
source share