How to map fixed char array in C struct to Java String of specified encoding by extending DefaultTypeConverter (to native and from native)

490 views
Skip to first unread message

Łukasz Matuszewski

unread,
Feb 8, 2017, 7:56:57 AM2/8/17
to Java Native Access
I hava a C structure like this:

#ifndef JNATEST_LIBRARY_H
#define JNATEST_LIBRARY_H

#pragma pack(1)
struct JNATest {
    char user[32];
    char password[32];
    struct Station {
        long long status;
        char smsCommand[64]; // UTF-16BE
    } station;
};

#endif

which is mapped Java class:


import com.sun.jna.Structure;

import java.util.Arrays;
import java.util.List;

public class JNATest extends Structure {

    /**
     * C type : char[32]
     */
    public byte[] user = new byte[32];
    /**
     * C type : char[32]
     */
    public byte[] password = new byte[32];
    /**
     * C type : Station
     */
    public Station station;

    /**
     * <i>native declaration : line 14</i>
     */
    public static class Station extends Structure {

        public long status;
        /**
         * UTF-16BE<br>
         * C type : char[64]
         */
        public byte[] smsCommand = new byte[64];

        public Station() {
            super(ALIGN_NONE, JNATestMain.tm);
        }

        protected List<String> getFieldOrder() {
            return Arrays.asList("status", "smsCommand");
        }

        public static class ByReference extends Station implements Structure.ByReference {

        }

        public static class ByValue extends Station implements Structure.ByValue {

        }

    }

    public JNATest() {
        super(ALIGN_NONE, JNATestMain.tm);
    }

    protected List<String> getFieldOrder() {
        return Arrays.asList("user", "password", "station");
    }

    public static class ByReference extends JNATest implements Structure.ByReference {

    }

    public static class ByValue extends JNATest implements Structure.ByValue {

    }

}

My question is to make user field a String which is originally in C represented by char user[32] by using TypeMapper.
TypeMapper is necessary for specifying encoding for java String (probably by using custom Java annotation detected
by TypeMapper on structure field user).

JNATestMain.tm is such TypeMapper by please provide me a TypeMapper to make such conversion from and to native 
C struct. FromNative must extract 32 bytes of structure to Java String and ToNative will convert existing String 
to char user[32] bytes.

Timothy Wall

unread,
Feb 8, 2017, 1:49:20 PM2/8/17
to jna-...@googlegroups.com
Why not just add a getter/settee function for those fields?

--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Łukasz Matuszewski

unread,
Feb 8, 2017, 1:52:59 PM2/8/17
to Java Native Access
Thanks i will consider this.

Reply all
Reply to author
Forward
0 new messages