title: JNA, Mapping and pointers references
body: <p>I need to use DLL inside my Java application. DLL is exporting some set of functions, authors called it "Direct DLL API". I'm trying to define in java equivalent of following function declaration:</p>
  
  <pre><code>int XcCompress( HXCEEDCMP hComp, const BYTE* pcSource, DWORD dwSourceSize, BYTE** ppcCompressed, DWORD* pdwCompressedSize, BOOL bEndOfData );
  </code></pre>
  
  <p>Inside my interface that extends Library I declared it as follows: </p>
  
  <pre><code>int XcCompress(WString hComp, Pointer pcSource, int dwSourceSize, Pointer[] ppcCompressed, IntByReference pdwCompressedSize, boolean bEndOfData);
  </code></pre>
  
  <p>Problem is everytime I get an error:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.Error: Invalid memory access</p>
  </blockquote>
  
  <p>So basically I'm stuck at this point. </p>
  
  <p><code>HXCEEDCMP hComp</code> - is suppose to store handler to the function, and works fine as WString for init DLL / destroying DLL functions so I kept it like this. </p>
  
  <p>The header reference "creature" is:</p>
  
  <pre><code>typedef HXCEEDCMP ( XCD_WINAPI *LPFNXCCREATEXCEEDCOMPRESSIONW )( const WCHAR* );
  </code></pre>
  
  <p><code>const BYTE* pcSource</code> - is the source data for compression, inside my code I instantiate it this way:</p>
  
  <pre><code>private static Pointer setByteArrayPointer(String dataToCompress) {
    Pointer pointer = new Memory(1024);
    pointer.write(0, dataToCompress.getBytes(), 0, 
    dataToCompress.getBytes().length);
  
    return pointer;
  }
  </code></pre>
  
  <p><code>DWORD dwSourceSize</code> - for this im getting reserved Memory size in this way:</p>
  
  <pre><code>String testData = "ABCDABCDABCDAAD";
  Pointer source = setByteArrayPointer(testData);
  
  (int) ((Memory)source).size()
  </code></pre>
  
  <p><code>BYTE** ppcCompressed</code> - function should populate ppcCompressed reference after work is done. I assume I made a mistake there, by doing it in this way:</p>
  
  <pre><code>Pointer[] compressed = {new Pointer(1024), new Pointer(1024)};
  </code></pre>
  
  <p><code>DWORD* pdwCompressedSize</code> - returned by function size of compressed data. I map it in this way:</p>
  
  <pre><code>IntByReference intByReference = new IntByReference();
  </code></pre>
  
  <p>Not sure if it is good idea aswell..</p>
  
  <p><code>BOOL bEndOfData</code> - i need to set it to true.</p>
  
  <p>So finally my method call, which returns an error looks like this:</p>
  
  <pre><code>xceedApiDll.XcCompress(handle, source, (int) ((Memory)source).size(), compressed, intByReference, true);
  </code></pre>
  
  <p>Any help will be appreciated. Thank you. </p>