title: How to pass int as a C++ function parameter from Java using JNA
body: <p>I'm currently working on a project which requires developing a native DLL (in C++) to be accessed by a Java application. I have chosen JNA for the bridging job and I am facing problems passing correct int values from Java to C++ functions.</p>
  
  <p>Simply put, I have a function that accepts an int value as parameter in C++:
  (code is stripped and methods are renamed to maintain confidentiality)</p>
  
  <pre><code>JAVALINK_EXPORT SomeStructure WINAPI GetSomeStructureFromIndex(int index) {
      std::string debugMsg("Received index of ");
      debugMsg.append(toString(index));
      OutputDebugString(debugMsg.c_str());
      SomeStructure result = defaultStructure;
      if (index &gt;= 0 &amp;&amp; index &lt; structListSize)
          result = structList[index];
  
      return result;
  }
  </code></pre>
  
  <p><code>toString</code> is a simple method that converts a value of any data type to <code>std::string</code> using a <code>std::stringstream</code>. The implementation is as follows:</p>
  
  <pre><code>template &lt;class T&gt;
  inline std::string toString (const T&amp; t) {
      std::stringstream ss;
      ss &lt;&lt; t;
      return ss.str();
  }
  </code></pre>
  
  <p><code>SomeStructure</code> is renamed from an actual struct I'm using in the code. <code>structList</code> is an array of <code>SomeStructure</code>. <code>structListSize</code> and <code>structList</code> are all global variables in shared memory.</p>
  
  <p>This is the method signature in the Java interface for the DLL:</p>
  
  <pre><code>SomeStructure.ByValue GetSomeStructureFromIndex(int index);
  </code></pre>
  
  <p>This is how I use the method in Java for the test case:</p>
  
  <pre><code>SomeStructure.ByValue received = library.GetSomeStructureFromIndex(1);
  </code></pre>
  
  <p><code>library</code> is an instance of the interface for the DLL file (subclass of <code>StdCallLibrary</code>) generated using <code>Native.loadLibrary</code>. When the above code is executed in Java, I get something like the following output in my Windows debug output:</p>
  
  <pre><code>Received index of 86701080
  </code></pre>
  
  <p>(The program will then proceed to encounter an access violation error if I have omitted the check for <code>index</code> with the line <code>if (index &gt;= 0 &amp;&amp; index &lt; structListSize)</code> before obtaining the structure from the array)</p>
  
  <p><code>86701080</code> can be any arbitrary value. I realized it changes according to the signature of the exported function. Am I missing something here? The function properly receives an expected value of <code>1</code> had the function signature been <code>void PrintIndex(int index)</code></p>
  
  <p>EDIT(0): I have modified the sample code to match the actual code more closely. </p>
  
  <p>EDIT(1): According to @technomage's pointers I have started using <code>ByValue</code> for all method signatures and variables collecting returned structures.</p>
  
  <p>EDIT(2): The Java class <code>SomeStructure</code> has an extra variable and a Java method compared to the <code>SomeStructure</code> struct in C++. I'm currently testing if that is the cause of the discrepancy.</p>
  
  <p><strong>PROBLEM SOLVED</strong></p>
  
  <p>@technomage explained that for a C++ function to interpret its arguments and return values as it is expected to, the size of the structure used as return type (and also those used as function parameters) should not be different from its Java counterpart. This can be checked, in the case of <code>SomeStructure</code>, in C++ with <code>sizeof(SomeStructure)</code> and in Java with <code>SomeStructure.size()</code>.</p>
  
  <p>Basically, what happened was that the <code>SomeStructure</code> struct has a different size than its Java representation. <code>SomeStructure</code> contained a fixed-length array as illustrated in the code below:</p>
  
  <pre><code>#define MAX_LIST_SIZE 256
  typedef struct {
      int list[MAX_LIST_SIZE];
      int length;
  } SomeStructure;
  </code></pre>
  
  <p>However, the Java representation did not specify the size of the fixed-length array. <code>list</code> was initialized to contain a single value of <code>0</code>.</p>
  
  <pre><code>package model;
  
  import com.sun.jna.Structure;
  
  public class SomeStructure extends Structure {
      public static class ByValue extends SomeStructure implements Structure.ByValue { }
      public int[] list = {0};
      public int length = 0;
  }
  </code></pre>
  
  <p>I solved the problem by replacing the faulty initialization statement with the following instead:</p>
  
  <pre><code>private static final int MAX_LIST_SIZE = 256;
  public int[] list = new int[MAX_LIST_SIZE];
  </code></pre>
  
  <p><strong>Note</strong>: The integer constant <code>MAX_LIST_SIZE</code> is declared <code>private</code> to keep it Java-only.</p>
  
  <p>After making all these amendments, my code is working fine and no longer runs into access violations.</p>