title: Calling a C++ dll method from Java using JNA and avoiding Method Name Mangling
body: <p>I have been going through links on StackOverflow on how to resolve the Method name mangling but did not find any solution with a real time example.</p>
  
  <p>Scenario-A C++ Ex.dll file is provided by client. I need to access the Ex.dll and call the methods in the same through Java.  </p>
  
  <p>Restrictions- Cannot modify the Ex.dll, i can only access the same.  </p>
  
  <p>Issue Faced- Getting the below exception when i access the Ex.dll through JNA</p>
  
  <pre><code>Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'getCPUSpeed': The specified procedure could not be found.
  at com.sun.jna.Function.&lt;init&gt;(Function.java:134)
  at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:336)
  at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:316)
  at com.sun.jna.Library$Handler.invoke(Library.java:203)
  at $Proxy0.getCPUSpeed(Unknown Source)
  at cpp.java.JnaTest.main(JnaTest.java:16)
  </code></pre>
  
  <p>Googled a lot and found that its due to method name Mangling, but again could not find any good sample code with the solution.</p>
  
  <p>This is code i used-</p>
  
  <pre><code>import com.sun.jna.Native;
  class JnaTest
  {
  
  public static void main(String args[])
  {
      try 
      {
          JnaInterface jInterface = (JnaInterface) Native.loadLibrary("Ex", JnaInterface.class);
  
          System.out.println("Calling C++ DLL method");
          System.out.println("========================");
          System.out.println("getCPUSpeed() -- "+jInterface.getCPUSpeed());
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  }
  
  
  
  package cpp.java;
  import com.sun.jna.Library;
  public interface JnaInterface extends Library{  
  public int getCPUSpeed();
  } 
  </code></pre>
  
  <p>Update 1: <strong><em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>*</strong></p>
  
  <p>Below mentioned is the actual functions i get when i browse the DBMM.dll through dependency walker- </p>
  
  <p>DBMM DLL functions-</p>
  
  <p>??0cDbmmInterfaceCache@@QAE@ABV0@@Z</p>
  
  <p>??0cDbmmInterfaceCache@@QAE@XZ</p>
  
  <p>??0cDbmmInterfaceControl@@QAE@ABV0@@Z</p>
  
  <p>??0cDbmmInterfaceControl@@QAE@XZ</p>
  
  <p>??0cDbmmInterfaceEcon@@QAE@ABV0@@Z</p>
  
  <p>??0cDbmmInterfaceEcon@@QAE@XZ</p>
  
  <p>??0cDbmmInterfaceKnob@@QAE@XZ</p>
  
  <p>??0cDbmmInterfaceOutput@@QAE@ABV0@@Z</p>
  
  <p>??0cDbmmInterfaceOutput@@QAE@H@Z</p>
  
  <p>??0cDbmmInterfacePoolLoan@@QAE@ABV0@@Z</p>
  
  <p>??0cDbmmInterfacePoolLoan@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z</p>
  
  <p>??0cDbmmMacroEcon@@QAE@ABV0@@Z</p>
  
  <p>??0cDbmmMacroEcon@@QAE@ABVcDbmmInterfaceEcon@@_N@Z</p>
  
  <p>??0cDbmmMtgBasisConstSpreadModel@@IAE@XZ</p>
  
  <p>??0cDbmmMtgBasisConstSpreadModel@@QAE@ABV0@@Z</p>
  
  <p>??0cDbmmMtgBasisConstSpreadModel@@QAE@PBD@Z</p>
  
  <p>??0cDbmmMtgBasisModel@@QAE@ABV0@@Z</p>
  
  <p>??0cDbmmMtgBasisModel@@QAE@XZ</p>
  
  <p>??0cScaleFieldsSubSum@@QAE@NN@Z</p>
  
  <p>??1cDbmmInterfaceCache@@QAE@XZ</p>
  
  <p>??1cDbmmInterfaceControl@@QAE@XZ</p>
  
  <p>??1cDbmmInterfaceEcon@@QAE@XZ</p>
  
  <p>??1cDbmmInterfaceKnob@@QAE@XZ</p>
  
  <p>??1cDbmmInterfaceOutput@@QAE@XZ</p>
  
  <p>??1cDbmmInterfacePoolLoan@@QAE@XZ</p>
  
  <p>??1cDbmmMacroEcon@@QAE@XZ</p>
  
  <p>??1cDbmmMtgBasisConstSpreadModel@@UAE@XZ</p>
  
  <p>??1cDbmmMtgBasisModel@@UAE@XZ</p>
  
  <p>??1cScaleFieldsSubSum@@QAE@XZ</p>
  
  <p>??4cDbmmInterface@@QAEAAV0@ABV0@@Z</p>
  
  <p>??4cDbmmInterfaceCache@@QAEAAV0@ABV0@@Z</p>
  
  <p>??4cDbmmInterfaceControl@@QAEAAV0@ABV0@@Z</p>
  
  <p>??4cDbmmInterfaceEcon@@QAEAAV0@ABV0@@Z</p>
  
  <p>??4cDbmmInterfaceKnob@@QAEAAV0@ABV0@@Z</p>
  
  <p>??4cDbmmInterfaceOutput@@QAEAAV0@ABV0@@Z</p>
  
  <p>??4cDbmmInterfacePoolLoan@@QAEAAV0@ABV0@@Z</p>
  
  <p>??4cDbmmMacroEcon@@QAEAAV0@ABV0@@Z</p>
  
  <p>??4cDbmmMtgBasisConstSpreadModel@@QAEAAV0@ABV0@@Z</p>
  
  <p>??4cDbmmMtgBasisModel@@QAEAAV0@ABV0@@Z</p>
  
  <p>??4cScaleFieldsSubSum@@QAEAAV0@ABV0@@Z</p>
  
  <p>??_7cDbmmMtgBasisConstSpreadModel@@6B@</p>
  
  <p>??_7cDbmmMtgBasisModel@@6B@</p>
  
  <p>??_FcDbmmInterfaceOutput@@QAEXXZ</p>
  
  <p>??_FcDbmmInterfacePoolLoan@@QAEXXZ</p>
  
  <p>??_FcScaleFieldsSubSum@@QAEXXZ</p>
  
  <p>?Add@cScaleFieldsSubSum@@QAEXNN@Z</p>
  
  <p>?InitSubsum@cScaleFieldsSubSum@@QAEXNN@Z</p>
  
  <p>?ReInit@cDbmmMacroEcon@@QAEX_N@Z</p>
  
  <hr>
  
  <p>Not sure how can i call these functions through Java.</p>
  
  <p>Appreciate if somebody could provide me a solution from Java end, with a sample code please :)</p>