Skip to content

Instantly share code, notes, and snippets.

@netmindz
Created March 3, 2011 18:49
Show Gist options
  • Save netmindz/853267 to your computer and use it in GitHub Desktop.
Save netmindz/853267 to your computer and use it in GitHub Desktop.
Buffer JNA issue
// C++ header
extern "C" __declspec(dllexport) int __stdcall API_USBMF_Request(
HANDLE commHandle, int DeviceAddress,
unsigned char inf_mode, unsigned char *Buffer);
// Java - Returns a single byte, which I think is a valid result
public int API_USBMF_Request(HANDLE commHandle, int DeviceAddress, char inf_mode, ByteByReference Buffer);
// Java - No change to byte array
public int API_USBMF_Request(HANDLE commHandle, int DeviceAddress, char inf_mode, byte[] Buffer);
@chirino
Copy link

chirino commented Mar 3, 2011

Not sure about JNA, but if I was mapping the function with HawtJNI it would look like this:

public static final native int API_USBMF_Request(
        @JniArg(cast="HANDLE", flags={POINTER_ARG}) long commHandle, 
        int DeviceAddress,
        char inf_mode,
        @JniArg(cast="char *") byte [] Buffer);

Data get copied out the java objects and then back by default. But if you know that the Buffer is write only, the you can optimize by adding the NO_IN flag:

@JniArg(cast="char *", flags={NO_IN}) byte [] Buffer

@netmindz
Copy link
Author

netmindz commented Mar 3, 2011

I just tried using PointerByReference, and that appears to kind of give me something. If i toString the Pointer i get a nice native@0x123456 kinda thing, so i just need to work out how to to the Pointer equivalent of Native.toString(byte[] blah)

@netmindz
Copy link
Author

netmindz commented Mar 3, 2011

Not sure what the jna ByReference does, but byte[] definitely doesn't work

@netmindz
Copy link
Author

netmindz commented Mar 3, 2011

Any ideas why using Pointer.getString() failed with access exception, weridly using IntByReferernce returns values, though quite what this number is when what I want is a String I'm not sure. Some native thing ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment