{"id":1376,"date":"2016-01-02T08:55:43","date_gmt":"2016-01-02T13:55:43","guid":{"rendered":"http:\/\/codinggorilla.domemtech.com\/?p=1376"},"modified":"2016-01-02T08:55:43","modified_gmt":"2016-01-02T13:55:43","slug":"a-short-practical-review-of-foreign-function-calls-in-java-c-c","status":"publish","type":"post","link":"http:\/\/165.227.223.229\/index.php\/2016\/01\/02\/a-short-practical-review-of-foreign-function-calls-in-java-c-c\/","title":{"rendered":"A Short, Practical Review of Foreign Function Calls in Java, C#, C++"},"content":{"rendered":"<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Android_(operating_system)\" target=\"_blank\">Android<\/a> is a popular platform for smartphones, with ten of thousands of applications developed for it every year. For coding an Android app, there are a number of programming languages, but Java is the most popular. However,\u00c2\u00a0<a style=\"line-height: 1.6471;\" href=\"https:\/\/xamarin.com\/\" target=\"_blank\">Xamarin<\/a><span style=\"line-height: 1.6471;\"> provides tools for writing applications in C# if you prefer. If you use Xamarin, but sometimes want to use an open-source library written in Java, you still can. The state of currently used, practical\u00c2\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Foreign_function_interface\" target=\"_blank\">Foreign Function Interfaces<\/a>\u00c2\u00a0(FFI) on most platforms is similar. Of course, the difficulty is in the details.<\/span><\/p>\n<p>Generally, there are two ways for code in one language to call another: use a native function to call the foreign function; or, use <a href=\"https:\/\/en.wikipedia.org\/wiki\/Reflection_(computer_programming)\" target=\"_blank\">reflection<\/a> to call the foreign function. A\u00c2\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Language_binding\" target=\"_blank\">bindings interface<\/a>\u00c2\u00a0offers native functions to the whole foreign API.<\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Java_Native_Interface\" target=\"_blank\">Java Native Interface (JNI)<\/a> provides a native interface to call non-Java code from Java. It also provides an API for use in C++ to invoke Java code, structured like Java&#8217;s reflection API.<\/p>\n<p>There are a number of tutorials on how to use JNI, but they are somewhat out of date. This article brings it forward&#8211;if only to\u00c2\u00a0a small degree, with &#8220;how to&#8221; explanations.<\/p>\n<p><!--more--><\/p>\n<h1>Java Native Interface on Windows<\/h1>\n<h2>Java to C# Call Using JNI<\/h2>\n<p>JNI provides a native interface for C++ functions in Java. For Java to call C#, a C++\/CLI wrapper layer must be created containing calls to C# code.<\/p>\n<ol>\n<li>Create a Java file (e.g., <em>Program.java<\/em>) with a class.<\/li>\n<\/ol>\n<ul>\n<li>Add native methods (e.g., <em>static native void\u00c2\u00a0greet()<\/em>) to the class corresponding to entry points in a C++\/CLI DLL. These methods have no body in the Java code; the bindings to the C++\/CLI code are assigned at runtime when the class is created.<\/li>\n<li>Add a static initializer (e.g., <em>{ System.load(&#8220;&#8230;&#8221;); }<\/em>) in the class to load\u00c2\u00a0the C++\/CLI DLL defined in step 5. The purpose of the initializer is to load the C++\/CLI DLL code and bind them to the native methods in the class. The C++\/CLI DLL contains entry points that JNI will bind with the native methods in this class.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><strong>Program.java<\/strong><\/span><\/p>\n\n<ol start=\"2\">\n<li>Compile the Java class, e.g., <em>javac Program.java<\/em>.<\/li>\n<li>Generate a C-style header file from the Java class, e.g., <em>javah Program<\/em>.<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline;\"><strong>Program.h<\/strong><\/span><\/p>\n\n<ol start=\"4\">\n<li>Create a C# DLL project, e.g., <em>ClassLibrary1<\/em>.<\/li>\n<\/ol>\n<ul>\n<li>In the C# library, add a class, e.g., <em>Class1<\/em>.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><strong>Class1.cs<\/strong><\/span><\/p>\n\n<ol start=\"5\">\n<li>Create a C++\/CLI DLL project, e.g., <em>ClassLIbrary2<\/em>. This library functions as the &#8220;glue&#8221; between JNI and C#.<\/li>\n<\/ol>\n<ul>\n<li>In the C++\/CLI project, add or modify a C++\/CLI source file, e.g., Entries.cpp.<\/li>\n<li>In the source file, add #include &lt;jni.h&gt;, #include &lt;&gt;.<\/li>\n<li>Add a #include directive for the JNI-generated (via <em>javah<\/em>) C-style header in the source file, e.g., <em>Program.h<\/em>.<\/li>\n<li>Add code to adjust search path for DLLs, otherwise when the Java program is ran, the\u00c2\u00a0program will crash.<\/li>\n<li>Add code for entry points defined in the generated header file.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><strong>Entries.cpp<\/strong><\/span><\/p>\n\n<ol start=\"6\">\n<li>Compile, build, and execute.<\/li>\n<\/ol>\n<p>For a basic introduction on JNI Java to C++ calling, see <a href=\"http:\/\/www.codeproject.com\/Articles\/13093\/C-method-calls-within-a-Java-program\" target=\"_blank\">http:\/\/www.codeproject.com\/Articles\/13093\/C-method-calls-within-a-Java-program<\/a>. However, the introduction is out of date and the example will not compile, e.g., the keyword __gc is not valid in the Visual C++ compiler since 2005!<\/p>\n<p>For another introduction, see the following article in Software Practice and Experience: <a href=\"http:\/\/onlinelibrary.wiley.com\/doi\/10.1002\/cpe.858\/abstract\" target=\"_blank\">http:\/\/onlinelibrary.wiley.com\/doi\/10.1002\/cpe.858\/abstract<\/a><\/p>\n<h2>C# to Java Call Using JNI<\/h2>\n<p>JNI provides an interface for C++ to call Java code. \u00c2\u00a0However, the interface is not native for C#. Instead, JNI is a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Reflection_(computer_programming)\" target=\"_blank\">reflection API<\/a>\u00c2\u00a0for use in\u00c2\u00a0C++. A C++\/CLI library must act as an intermediary between C# and JNI.<\/p>\n<ol>\n<li>Create a Windows C# application. Insert calls to a wrapper function containing the JNI code.<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline;\"><strong>CSharpProgram.cs<\/strong><\/span><\/p>\n\n<ol start=\"2\">\n<li>Create a C++\/CLI DLL project for wrapping the calls to JNI\/Java.<\/li>\n<\/ol>\n<ul>\n<li>In the C++\/CLI project, add or modify a C++\/CLI source file for code that will contain the JNI calls, e.g., Wrapper.cpp.<\/li>\n<li>Add a #include directives for JNI, e.g., #include &lt;jni.h&gt;, #include &lt;win32\/jni_md.h&gt;.<\/li>\n<li>Add code for JNI_CreateJavaVM. This function call loads the Java Virtual Machine (JVM) and returns handle to it.<\/li>\n<li>For each function you want to call in your Java API, add code for\u00c2\u00a0FindClass,\u00c2\u00a0GetStaticMethodID,\u00c2\u00a0CallStaticVoidMethod, etc. These functions get handles to JNI objects, and call the Java API.<\/li>\n<\/ul>\n<p>Wrapper.cpp<\/p>\n\n<ol start=\"3\">\n<li>Create a .JAR file or directory structure with the Java API you want to implement. Make sure to set up the class in a package with the appropriate directory structure for the JAR, otherwise, JNI will not be able to find the API.<\/li>\n<\/ol>\n<p><strong>com\/domemtech\/examples\/Example.java<\/strong><\/p>\n\n<ol start=\"4\">\n<li>Compile, build, and execute.<\/li>\n<\/ol>\n<p>For another introduction on C++ calling Java, see\u00c2\u00a0<a href=\"http:\/\/www.codeproject.com\/Articles\/993067\/Calling-Java-from-Cplusplus-with-JNI\" target=\"_blank\">http:\/\/www.codeproject.com\/Articles\/993067\/Calling-Java-from-Cplusplus-with-JNI<\/a>.<\/p>\n<h1>FFI in\u00c2\u00a0Xamarin<\/h1>\n<h2>C# to Java Calling\u00c2\u00a0using a Binding Library<\/h2>\n<p>The details of how to create a Java Binding Library for a Java Jar library is given by Xamarin (<a href=\"https:\/\/developer.xamarin.com\/guides\/android\/advanced_topics\/java_integration_overview\/binding-a-java-library\/binding-a-jar\/\">https:\/\/developer.xamarin.com\/guides\/android\/advanced_topics\/java_integration_overview\/binding-a-java-library\/binding-a-jar\/<\/a>). As outlined there, you must create a library that belongs to a package, such as \u00e2\u20ac\u009ccom.domemtech.examples.\u00e2\u20ac\u009d Otherwise, this won\u00e2\u20ac\u2122t work. To do that, create your source in a directory tree corresponding to the package (e.g., \u00e2\u20ac\u009ccom\/domemtech\/examples\/Example.java\u00e2\u20ac\u009d). Compile the java code (\u00e2\u20ac\u009cjavac com\u00e2\u20ac\u009d), then create a Jar file (\u00e2\u20ac\u009cjar cvf example.jar com\u00e2\u20ac\u009d).<\/p>\n<h2>C# to Java Calling\u00c2\u00a0using JNI<\/h2>\n<p>The details of how to use JNI is given by Xamarin (<a href=\"https:\/\/developer.xamarin.com\/guides\/android\/advanced_topics\/java_integration_overview\/working_with_jni\/\">https:\/\/developer.xamarin.com\/guides\/android\/advanced_topics\/java_integration_overview\/working_with_jni\/<\/a>). There doesn\u00e2\u20ac\u2122t seem to be a discussion of where to place the JAR files, but it works if you create a binding library that embeds the jar.<\/p>\n<h1>Generating Interfaces<\/h1>\n<h1>JNI4Net<\/h1>\n<p>JNI4Net is an open-source tool that generates native interfaces for Java and C#. After generating the proxy code, you have to add functionality to work as a wrapper. A good introduction is here: <a href=\"http:\/\/www.c-sharpcorner.com\/UploadFile\/ajyadav123\/invoking-java-code-in-C-Sharp-net\/\">http:\/\/www.c-sharpcorner.com\/UploadFile\/ajyadav123\/invoking-java-code-in-C-Sharp-net\/<\/a><\/p>\n<h2>Simplified Wrapper and Interface Generator (SWIG)<\/h2>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/SWIG\" target=\"_blank\">SWIG<\/a> is an open-source tool that generates native interfaces from a description specification. Complete functionality of the wrapper must be added by the user.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android is a popular platform for smartphones, with ten of thousands of applications developed for it every year. For coding an Android app, there are a number of programming languages, but Java is the most popular. However,\u00c2\u00a0Xamarin provides tools for writing applications in C# if you prefer. If you use Xamarin, but sometimes want to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/165.227.223.229\/index.php\/2016\/01\/02\/a-short-practical-review-of-foreign-function-calls-in-java-c-c\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;A Short, Practical Review of Foreign Function Calls in Java, C#, C++&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts\/1376"}],"collection":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/comments?post=1376"}],"version-history":[{"count":0,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts\/1376\/revisions"}],"wp:attachment":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/media?parent=1376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/categories?post=1376"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/tags?post=1376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}