// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package m2n;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;

public class TestJ2 {
    public static void main(String[] args) {
        int a = 123;
        int b = 456;
        int c = TestJNA.INSTANCE.std_add(a, b);
        System.out.println(c);
        //
        int[] six = { 1, 2, 3, 4, 5, 6 };
        int allsix = TestJNA.INSTANCE.std_sum(six.length, six);
        System.out.println(allsix);
        //
        String s = "ABC";                                                              
        Pointer p = TestJNA.INSTANCE.std_dup(s);
        String s2 = p.getString(0);
        TestJNA.INSTANCE.std_free(p);
        System.out.println(s2);
        //
        int[] iv = new int[1];
        String[] sv = new String[1];
        Pointer[] pa = new Pointer[1];
        TestJNA.INSTANCE.std_get_out(iv, pa);
        sv[0] = pa[0].getString(0);
        TestJNA.INSTANCE.std_free(pa[0]);
        System.out.println(iv[0] + " " + sv[0]);
        // DOES NOT WORK
        Pointer ps = TestJNA.INSTANCE.std_get_ret();
        JNAData1 d1 = Structure.newInstance(JNAData1.class, ps);
        TestJNA.INSTANCE.std_free(ps);
        System.out.println(d1.iv + " " + Native.toString(d1.sv));
        // DOES NOT WORK
        JNAData2 d2 = new JNAData2();
        d2.iv1 = 123;
        d2.sv1 = Native.toByteArray("ABC");
        TestJNA.INSTANCE.std_chg(d2);
        System.out.println(d2.iv2 + " " + Native.toString(d2.sv2));
    }
}
