JNA non-const String member of struct -


i have c function expects struct contains non-const string.

typedef struct _a {   char* str; } a;  void myfunc(a* aptr) { ... } 

i've tried long time pass thing via jna didn't manage far.

public class extends structure {   public string str;   protected list getfieldorder() { ... } 

doesn't work because string turned const char* instead of char* need.

public class extends structure {   public byte[] str;   protected list getfieldorder() { ... } 

doesn't work because byte array inside struct gets turned contiguous memory , not pointer.

i know can using memory , copying string over. can't imagine preferred way it.

i tried like

public class nonconststringmember extends structure {   public static class byreference extends nonconststringmember implements structure.byreference {}           public byte[] stringmember;   protected list getfieldorder() { ... }  public class extends structure {   public nonconststringmember.byreference str; } 

but doesn't work either, maybe because of alignment issues.

what preferred way jna?

assuming want point arbitrary buffer may write to, use pointer (and assign memory value if you're allocating buffer).

you use pointer.getstring(0) or pointer.setstring(0, s) manipulate buffer contents.

edit windows libraries have ascii or unicode version of functions. while in cases can define single structure use in both cases, need provide little additional handling ensure correct types used. since 2 modes rarely, if ever, used simultaneously, jna sets options default 1 or other (automatically mapping right function suffix, , automatically mapping string either native const char* or const wchar_t* depending on mode).

use setwidestring() if using unicode mode , setstring() otherwise, or make accessors on structure handle automatically. example:

class mystructure extends structure {     private static final boolean ascii = boolean.getboolean("w32.ascii");     public pointer stringbuffer = new memory(x);     void setstringbuffer(string s) {         stringbuffer.setstring(0, s, ascii);     }     string getstringbuffer() {         return stringbuffer.getstring(0, ascii);     } } 

edit please note in cases should use string field type , rely on typemapper use wstring behind scenes appropriate (e.g. user32 , w32apioptions.default_options).


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -