title: Allocate Memory for Functions in com.sun.jna.Library (Functions from C# DLL)
body: <p>Good day, Everyone!</p>
  <p>This is my first time posting here, so I apologize in advance for the newbie mistakes in posting.</p>
  <p>I am supposed to call a C# method in Java program. I am using RGiesecke.DllExport(1.2.7) to export my C# static method and com.sun.jna(4.2.2) to load the DLL in Java.</p>
  <p>However, it always results to</p>
  <pre><code>Invalid memory access
  java.lang.Error: Invalid memory access
      at com.sun.jna.Native.invokePointer(Native Method)
      at com.sun.jna.Function.invokePointer(Function.java:477)
      at com.sun.jna.Function.invokeString(Function.java:658)
      at com.sun.jna.Function.invoke(Function.java:402)
      at com.sun.jna.Function.invoke(Function.java:323)
      at com.sun.jna.Library$Handler.invoke(Library.java:236)
      at com.sun.proxy.$Proxy13.Parse(Unknown Source)
  </code></pre>
  <p>I figured that it is caused by creating new C# objects in the called method because I have not yet allocated memory for the function.</p>
  <p>I would like to confirm/ask if there is a way I can allocate memory for function in  <code>com.sun.jna.Library</code> (loaded C# DLL).</p>
  <p>Shown are the codes below:</p>
  <p><strong>Java (Interface for class):</strong></p>
  <pre><code>package com.example.mydllclass
  
  import com.sun.jna.Library;
  
  public interface IMyDllClass extends Library {
  
      String Parse(String path);
  }
  </code></pre>
  <p><strong>Java (Loading dll and invoking method)</strong></p>
  <pre><code>public class MyClassProcess {
  
      private final String DLL_PATH = &quot;.\\MyClass.dll&quot;
  
      public String processFile(String path){
  
          IMyDllClass INSTANCE =(IMyDllClass)Native.loadLibrary(DLL_PATH,IMyDllClass.class);
  
          return INSTANCE.Parse(path);
      }
  }
  </code></pre>
  <p><strong>C# Method</strong></p>
  <pre><code>public class MyDllClass{
  
      [DllExport(&quot;Parse&quot;, CallingConvention = CallingConvention.Cdecl)]
      public static string Parse(string path)
      {
          String result = &quot;&quot;;
  
          ProcessResultCalculator processResultCalculator = new ProcessResultCalculator(); // I need to create this
          result = processResultCalculator.execute(path);
  
          return result;
      }
  }
  </code></pre>
  <p>Thank you!</p>